Skip to content

Commit

Permalink
Merge pull request #503 from barryvdh/view-from-hash
Browse files Browse the repository at this point in the history
Find View name from hash
  • Loading branch information
barryvdh committed May 18, 2016
2 parents 97e8f60 + 9d7f49f commit 6f10831
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/DataCollector/QueryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class QueryCollector extends PDOCollector
protected $explainQuery = false;
protected $explainTypes = array('SELECT'); // array('SELECT', 'INSERT', 'UPDATE', 'DELETE'); for MySQL 5.6.3+
protected $showHints = false;
protected $reflection = [];

/**
* @param TimeDataCollector $timeCollector
Expand Down Expand Up @@ -211,7 +212,13 @@ protected function findSource()
if (isset($trace['object']) && is_a($trace['object'], 'Twig_Template')) {
list($file, $line) = $this->getTwigInfo($trace);
} elseif (strpos($trace['file'], storage_path()) !== false) {
return 'Template file';
$hash = pathinfo($trace['file'], PATHINFO_FILENAME);
$line = isset($trace['line']) ? $trace['line'] : '?';

if ($name = $this->findViewFromHash($hash)) {
return 'view::' . $name . ':' . $line;
}
return 'view::' . $hash . ':' . $line;
} else {
$file = $trace['file'];
$line = isset($trace['line']) ? $trace['line'] : '?';
Expand All @@ -224,6 +231,32 @@ protected function findSource()
}
}

/**
* Find the template name from the hash.
*
* @param string $hash
* @return null|string
*/
protected function findViewFromHash($hash)
{
$finder = app('view')->getFinder();

if (isset($this->reflection['viewfinderViews'])) {
$property = $this->reflection['viewfinderViews'];
} else {
$reflection = new \ReflectionClass($finder);
$property = $reflection->getProperty('views');
$property->setAccessible(true);
$this->reflection['viewfinderViews'] = $property;
}

foreach ($property->getValue($finder) as $name => $path){
if (sha1($path) == $hash || md5($path) == $hash) {
return $name;
}
}
}

/**
* Get the filename/line from a Twig template trace
*
Expand Down

0 comments on commit 6f10831

Please sign in to comment.