class FileStorage in XHProf 8
Provides file storage backend for xhprof runs.
Hierarchy
- class \Drupal\xhprof\XHProfLib\Storage\FileStorage implements StorageInterface
Expanded class hierarchy of FileStorage
1 file declares its use of FileStorage
- Aggregator.php in src/
XHProfLib/ Aggregator.php
1 string reference to 'FileStorage'
1 service uses FileStorage
File
- src/
XHProfLib/ Storage/ FileStorage.php, line 10
Namespace
Drupal\xhprof\XHProfLib\StorageView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FileStorage:: |
private | property | ||
FileStorage:: |
private | property | ||
FileStorage:: |
private | function | ||
FileStorage:: |
public | function |
Returns run name. Overrides StorageInterface:: |
|
FileStorage:: |
public | function |
Loads run. Overrides StorageInterface:: |
|
FileStorage:: |
public | function |
Returns a list of stored runs. Overrides StorageInterface:: |
|
FileStorage:: |
public | function |
Saves run data. Overrides StorageInterface:: |
|
FileStorage:: |
private | function | ||
FileStorage:: |
public | function |