兼容php5.2

pull/109/head
midoks 5 years ago
parent d988286eba
commit 9110b52b65
  1. 181
      plugins/xhprof/lib/xhprof_lib/utils/xhprof_runs.php

@ -31,31 +31,30 @@
*/ */
interface iXHProfRuns { interface iXHProfRuns {
/** /**
* Returns XHProf data given a run id ($run) of a given * Returns XHProf data given a run id ($run) of a given
* type ($type). * type ($type).
* *
* Also, a brief description of the run is returned via the * Also, a brief description of the run is returned via the
* $run_desc out parameter. * $run_desc out parameter.
*/ */
public function get_run($run_id, $type, &$run_desc); public function get_run($run_id, $type, &$run_desc);
/** /**
* Save XHProf data for a profiler run of specified type * Save XHProf data for a profiler run of specified type
* ($type). * ($type).
* *
* The caller may optionally pass in run_id (which they * The caller may optionally pass in run_id (which they
* promise to be unique). If a run_id is not passed in, * promise to be unique). If a run_id is not passed in,
* the implementation of this method must generated a * the implementation of this method must generated a
* unique run id for this saved XHProf run. * unique run id for this saved XHProf run.
* *
* Returns the run id for the saved XHProf run. * Returns the run id for the saved XHProf run.
* *
*/ */
public function save_run($xhprof_data, $type, $run_id = null); public function save_run($xhprof_data, $type, $run_id = null);
} }
/** /**
* XHProfRuns_Default is the default implementation of the * XHProfRuns_Default is the default implementation of the
* iXHProfRuns interface for saving/fetching XHProf runs. * iXHProfRuns interface for saving/fetching XHProf runs.
@ -67,98 +66,102 @@ interface iXHProfRuns {
*/ */
class XHProfRuns_Default implements iXHProfRuns { class XHProfRuns_Default implements iXHProfRuns {
private $dir = ''; private $dir = '';
private $suffix = 'xhprof'; private $suffix = 'xhprof';
private function gen_run_id($type) { private function gen_run_id($type) {
return uniqid(); return uniqid();
} }
private function file_name($run_id, $type) { private function file_name($run_id, $type) {
$file = "$run_id.$type." . $this->suffix; $file = "$run_id.$type." . $this->suffix;
if (!empty($this->dir)) { if (!empty($this->dir)) {
$file = $this->dir . "/" . $file; $file = $this->dir . "/" . $file;
}
return $file;
} }
return $file;
}
public function __construct($dir = null) { public function __construct($dir = null) {
// if user hasn't passed a directory location, // if user hasn't passed a directory location,
// we use the xhprof.output_dir ini setting // we use the xhprof.output_dir ini setting
// if specified, else we default to the directory // if specified, else we default to the directory
// in which the error_log file resides. // in which the error_log file resides.
if (empty($dir)) { if (empty($dir)) {
$dir = ini_get("xhprof.output_dir"); $dir = ini_get("xhprof.output_dir");
if (empty($dir)) { if (empty($dir)) {
$dir = sys_get_temp_dir(); $dir = sys_get_temp_dir();
xhprof_error("Warning: Must specify directory location for XHProf runs. ". xhprof_error("Warning: Must specify directory location for XHProf runs. " .
"Trying {$dir} as default. You can either pass the " . "Trying {$dir} as default. You can either pass the " .
"directory location as an argument to the constructor ". "directory location as an argument to the constructor " .
"for XHProfRuns_Default() or set xhprof.output_dir ". "for XHProfRuns_Default() or set xhprof.output_dir " .
"ini param."); "ini param.");
} }
}
$this->dir = $dir;
} }
$this->dir = $dir;
}
public function get_run($run_id, $type, &$run_desc) { public function get_run($run_id, $type, &$run_desc) {
$file_name = $this->file_name($run_id, $type); $file_name = $this->file_name($run_id, $type);
if (!file_exists($file_name)) {
xhprof_error("Could not find file $file_name");
$run_desc = "Invalid Run Id = $run_id";
return null;
}
if (!file_exists($file_name)) { $contents = file_get_contents($file_name);
xhprof_error("Could not find file $file_name"); $run_desc = "XHProf Run (Namespace=$type)";
$run_desc = "Invalid Run Id = $run_id"; return unserialize($contents);
return null;
} }
$contents = file_get_contents($file_name); public function save_run($xhprof_data, $type, $run_id = null) {
$run_desc = "XHProf Run (Namespace=$type)";
return unserialize($contents);
}
public function save_run($xhprof_data, $type, $run_id = null) { // Use PHP serialize function to store the XHProf's
// raw profiler data.
$xhprof_data = serialize($xhprof_data);
// Use PHP serialize function to store the XHProf's if ($run_id === null) {
// raw profiler data. $run_id = $this->gen_run_id($type);
$xhprof_data = serialize($xhprof_data); }
if ($run_id === null) { $file_name = $this->file_name($run_id, $type);
$run_id = $this->gen_run_id($type); $file = fopen($file_name, 'w');
}
$file_name = $this->file_name($run_id, $type); if ($file) {
$file = fopen($file_name, 'w'); fwrite($file, $xhprof_data);
fclose($file);
} else {
xhprof_error("Could not open $file_name\n");
}
// echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n";
return $run_id;
}
if ($file) { function list_runs_callback($a, $b) {
fwrite($file, $xhprof_data); return filemtime($b) - filemtime($a);
fclose($file);
} else {
xhprof_error("Could not open $file_name\n");
} }
// echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n"; function list_runs() {
return $run_id; if (is_dir($this->dir)) {
} echo "<hr/>Existing runs:\n<ul>\n";
$files = glob("{$this->dir}/*.{$this->suffix}");
function list_runs() { usort($files, 'list_runs_callback');
if (is_dir($this->dir)) { foreach ($files as $file) {
echo "<hr/>Existing runs:\n<ul>\n"; list($run, $source) = explode('.', basename($file));
$files = glob("{$this->dir}/*.{$this->suffix}"); echo '<li><a href="' . htmlentities($_SERVER['SCRIPT_NAME'])
usort($files, function($a, $b) {return filemtime($b) - filemtime($a);});
foreach ($files as $file) {
list($run,$source) = explode('.', basename($file));
echo '<li><a href="' . htmlentities($_SERVER['SCRIPT_NAME'])
. '?run=' . htmlentities($run) . '&source=' . '?run=' . htmlentities($run) . '&source='
. htmlentities($source) . '">' . htmlentities($source) . '">'
. htmlentities(basename($file)) . "</a><small> " . htmlentities(basename($file)) . "</a><small> "
. date("Y-m-d H:i:s", filemtime($file)) . "</small></li>\n"; . date("Y-m-d H:i:s", filemtime($file)) . "</small></li>\n";
}
echo "</ul>\n";
} }
echo "</ul>\n";
} }
}
} }

Loading…
Cancel
Save