Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. FileProfilerStorage.php

function FileProfilerStorage::write

Throws

\RuntimeException

Overrides ProfilerStorageInterface::write

File

vendor/symfony/http-kernel/Profiler/FileProfilerStorage.php, line 131

Class

FileProfilerStorage
Storage for profiler using files.

Namespace

Symfony\Component\HttpKernel\Profiler

Code

public function write(Profile $profile) : bool {
    $file = $this->getFilename($profile->getToken());
    $profileIndexed = is_file($file);
    if (!$profileIndexed) {
        // Create directory
        $dir = \dirname($file);
        if (!is_dir($dir) && false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
            throw new \RuntimeException(\sprintf('Unable to create the storage directory (%s).', $dir));
        }
    }
    $profileToken = $profile->getToken();
    // when there are errors in sub-requests, the parent and/or children tokens
    // may equal the profile token, resulting in infinite loops
    $parentToken = $profile->getParentToken() !== $profileToken ? $profile->getParentToken() : null;
    $childrenToken = array_filter(array_map(fn(Profile $p) => $profileToken !== $p->getToken() ? $p->getToken() : null, $profile->getChildren()));
    // Store profile
    $data = [
        'token' => $profileToken,
        'parent' => $parentToken,
        'children' => $childrenToken,
        'data' => $profile->getCollectors(),
        'ip' => $profile->getIp(),
        'method' => $profile->getMethod(),
        'url' => $profile->getUrl(),
        'time' => $profile->getTime(),
        'status_code' => $profile->getStatusCode(),
        'virtual_type' => $profile->getVirtualType() ?? 'request',
    ];
    $data = serialize($data);
    if (\function_exists('gzencode')) {
        $data = gzencode($data, 3);
    }
    if (false === file_put_contents($file, $data, \LOCK_EX)) {
        return false;
    }
    if (!$profileIndexed) {
        // Add to index
        if (false === ($file = fopen($this->getIndexFilename(), 'a'))) {
            return false;
        }
        fputcsv($file, [
            $profile->getToken(),
            $profile->getIp(),
            $profile->getMethod(),
            $profile->getUrl(),
            $profile->getTime() ?: time(),
            $profile->getParentToken(),
            $profile->getStatusCode(),
            $profile->getVirtualType() ?? 'request',
        ], ',', '"', '\\');
        fclose($file);
        if (1 === mt_rand(1, 10)) {
            $this->removeExpiredProfiles();
        }
    }
    return true;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal