You are here

public function FileProfilerStorage::write in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-kernel/Profiler/FileProfilerStorage.php \Symfony\Component\HttpKernel\Profiler\FileProfilerStorage::write()

Saves a Profile.

Parameters

Profile $profile A Profile instance:

Return value

bool Write operation successful

Overrides ProfilerStorageInterface::write

File

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

Class

FileProfilerStorage
Storage for profiler using files.

Namespace

Symfony\Component\HttpKernel\Profiler

Code

public function write(Profile $profile) {
  $file = $this
    ->getFilename($profile
    ->getToken());
  $profileIndexed = is_file($file);
  if (!$profileIndexed) {

    // Create directory
    $dir = dirname($file);
    if (!is_dir($dir)) {
      mkdir($dir, 0777, true);
    }
  }

  // Store profile
  $data = array(
    'token' => $profile
      ->getToken(),
    'parent' => $profile
      ->getParentToken(),
    'children' => array_map(function ($p) {
      return $p
        ->getToken();
    }, $profile
      ->getChildren()),
    'data' => $profile
      ->getCollectors(),
    'ip' => $profile
      ->getIp(),
    'method' => $profile
      ->getMethod(),
    'url' => $profile
      ->getUrl(),
    'time' => $profile
      ->getTime(),
  );
  if (false === file_put_contents($file, serialize($data))) {
    return false;
  }
  if (!$profileIndexed) {

    // Add to index
    if (false === ($file = fopen($this
      ->getIndexFilename(), 'a'))) {
      return false;
    }
    fputcsv($file, array(
      $profile
        ->getToken(),
      $profile
        ->getIp(),
      $profile
        ->getMethod(),
      $profile
        ->getUrl(),
      $profile
        ->getTime(),
      $profile
        ->getParentToken(),
      $profile
        ->getStatusCode(),
    ));
    fclose($file);
  }
  return true;
}