You are here

class FileStorage in XHProf 8

Provides file storage backend for xhprof runs.

Hierarchy

Expanded class hierarchy of FileStorage

1 file declares its use of FileStorage
Aggregator.php in src/XHProfLib/Aggregator.php
1 string reference to 'FileStorage'
xhprof.services.yml in ./xhprof.services.yml
xhprof.services.yml
1 service uses FileStorage
xhprof.file_storage in ./xhprof.services.yml
Drupal\xhprof\XHProfLib\Storage\FileStorage

File

src/XHProfLib/Storage/FileStorage.php, line 10

Namespace

Drupal\xhprof\XHProfLib\Storage
View source
class FileStorage implements StorageInterface {

  /**
   * @var string
   */
  private $dir;

  /**
   * @var string
   */
  private $suffix;

  /**
   * @param string $dir
   */
  public function __construct($dir = NULL) {
    if ($dir) {
      $this->dir = $dir;
    }
    else {
      $this->dir = ini_get("xhprof.output_dir") ?: sys_get_temp_dir();
    }
    $factory = \Drupal::configFactory();
    $extension = $factory
      ->get('xhprof.config')
      ->get('extension');
    if ($extension == 'uprofiler') {
      $this->suffix = 'uprofiler';
    }
    else {
      $this->suffix = 'xhprof';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'File Storage';
  }

  /**
   * {@inheritdoc}
   */
  public function getRun($run_id, $namespace) {
    $file_name = $this
      ->fileName($run_id, $namespace);
    if (!file_exists($file_name)) {
      throw new \RuntimeException("Could not find file {$file_name}");
    }
    $serialized_contents = file_get_contents($file_name);
    $contents = @unserialize($serialized_contents);
    if ($contents === FALSE) {
      throw new \UnexpectedValueException("Unable to unserialize {$file_name}!");
    }
    return new Run($run_id, $namespace, $contents);
  }

  /**
   * {@inheritdoc}
   */
  public function getRuns($namespace = NULL) {
    $files = $this
      ->scanXHProfDir($this->dir, $namespace);
    $files = array_map(function ($f) {
      $f['date'] = strtotime($f['date']);
      return $f;
    }, $files);
    return $files;
  }

  /**
   * {@inheritdoc}
   */
  public function saveRun($data, $namespace, $run_id) {

    // Use PHP serialize function to store the XHProf's
    // raw profiler data.
    $data = serialize($data);
    $file_name = $this
      ->fileName($run_id, $namespace);
    $file = fopen($file_name, 'w');
    if ($file) {
      fwrite($file, $data);
      fclose($file);
    }
    else {
      throw new \Exception("Could not open {$file_name}\n");
    }
    return $run_id;
  }

  /**
   * @param string $dir
   * @param string $namespace
   *
   * @return array
   */
  private function scanXHProfDir($dir, $namespace = NULL) {
    $runs = [];
    if (is_dir($dir)) {
      foreach (glob("{$this->dir}/*.{$this->suffix}") as $file) {
        preg_match("/(?:(?<run>\\w+)\\.)(?:(?<namespace>[^.]+)\\.)(?<ext>[\\w.]+)/", basename($file), $matches);
        $runs[] = [
          'run_id' => $matches['run'],
          'namespace' => $matches['namespace'],
          'basename' => htmlentities(basename($file)),
          'date' => date("Y-m-d H:i:s", filemtime($file)),
          'size' => filesize($file),
        ];
      }
    }
    return array_reverse($runs);
  }

  /**
   * @param string $run_id
   * @param string $namespace
   *
   * @return string
   */
  private function fileName($run_id, $namespace) {
    $file = implode('.', [
      $run_id,
      $namespace,
      $this->suffix,
    ]);
    if (!empty($this->dir)) {
      $file = $this->dir . "/" . $file;
    }
    return $file;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileStorage::$dir private property
FileStorage::$suffix private property
FileStorage::fileName private function
FileStorage::getName public function Returns run name. Overrides StorageInterface::getName
FileStorage::getRun public function Loads run. Overrides StorageInterface::getRun
FileStorage::getRuns public function Returns a list of stored runs. Overrides StorageInterface::getRuns
FileStorage::saveRun public function Saves run data. Overrides StorageInterface::saveRun
FileStorage::scanXHProfDir private function
FileStorage::__construct public function