1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 08:17:12 +02:00

Add profiler to TemplateFile and PageRender

This commit is contained in:
Ryan Cramer
2016-10-20 07:02:24 -04:00
parent 4060934bae
commit 5542b77440
2 changed files with 53 additions and 3 deletions

View File

@@ -71,6 +71,20 @@ class TemplateFile extends WireData {
*/ */
protected $halt = false; protected $halt = false;
/**
* Last tracked profile event
*
* @var mixed
*
*/
protected $profilerEvent = null;
/**
* @var WireProfilerInterface|null
*
*/
protected $profiler = null;
/** /**
* Variables that will be applied globally to this and all other TemplateFile instances * Variables that will be applied globally to this and all other TemplateFile instances
* *
@@ -180,6 +194,27 @@ class TemplateFile extends WireData {
self::$globals[$name] = $value; self::$globals[$name] = $value;
} }
/**
* Start profiling a render
*
* @param string $filename
*
*/
protected function start($filename) {
if($this->profiler) {
$f = str_replace($this->wire('config')->paths->root, '/', $filename);
$this->profilerEvent = $this->profiler->start($f, $this);
}
}
/**
* Stop profiling a render
*
*/
protected function stop() {
if($this->profilerEvent) $this->profiler->stop($this->profilerEvent);
}
/** /**
* Render the template -- execute it and return it's output * Render the template -- execute it and return it's output
* *
@@ -201,6 +236,7 @@ class TemplateFile extends WireData {
$this->savedInstance = ProcessWire::getCurrentInstance(); $this->savedInstance = ProcessWire::getCurrentInstance();
ProcessWire::setCurrentInstance($this->wire()); ProcessWire::setCurrentInstance($this->wire());
$this->profiler = $this->wire('profiler');
$this->savedDir = getcwd(); $this->savedDir = getcwd();
if($this->chdir) { if($this->chdir) {
@@ -208,19 +244,29 @@ class TemplateFile extends WireData {
} else { } else {
chdir(dirname($this->filename)); chdir(dirname($this->filename));
} }
$fuel = array_merge($this->getArray(), self::$globals); // so that script can foreach all vars to see what's there
$fuel = array_merge($this->getArray(), self::$globals); // so that script can foreach all vars to see what's there
extract($fuel); extract($fuel);
ob_start(); ob_start();
foreach($this->prependFilename as $_filename) { foreach($this->prependFilename as $_filename) {
if($this->halt) break; if($this->halt) break;
if($this->profiler) $this->start($_filename);
require($_filename); require($_filename);
if($this->profiler) $this->stop();
} }
if($this->profiler) $this->start($this->filename);
if(!$this->halt) $returnValue = require($this->filename); if(!$this->halt) $returnValue = require($this->filename);
if($this->profiler) $this->stop();
foreach($this->appendFilename as $_filename) { foreach($this->appendFilename as $_filename) {
if($this->halt) break; if($this->halt) break;
if($this->profiler) $this->start($_filename);
require($_filename); require($_filename);
if($this->profiler) $this->stop();
} }
$out = "\n" . ob_get_contents() . "\n"; $out = "\n" . ob_get_contents() . "\n";
ob_end_clean(); ob_end_clean();
@@ -229,6 +275,7 @@ class TemplateFile extends WireData {
$out = trim($out); $out = trim($out);
if(!strlen($out) && !$this->halt && $returnValue && $returnValue !== 1) return $returnValue; if(!strlen($out) && !$this->halt && $returnValue && $returnValue !== 1) return $returnValue;
return $out; return $out;
} }

View File

@@ -500,7 +500,10 @@ class PageRender extends WireData implements Module, ConfigurableModule {
// own additional variables in it if they want to // own additional variables in it if they want to
$output->set('options', $options); $output->set('options', $options);
$profiler = $this->wire('profiler');
$profilerEvent = $profiler ? $profiler->start($page->path, $this, array('page' => $page)) : null;
$data = $output->render(); $data = $output->render();
if($profilerEvent) $profiler->stop($profilerEvent);
} }
if($data && $cacheAllowed && $cacheFile) $cacheFile->save($data); if($data && $cacheAllowed && $cacheFile) $cacheFile->save($data);