diff --git a/src/CollectorProviders/ViewsCollectorProvider.php b/src/CollectorProviders/ViewsCollectorProvider.php index 170d4303e..30ba891ea 100644 --- a/src/CollectorProviders/ViewsCollectorProvider.php +++ b/src/CollectorProviders/ViewsCollectorProvider.php @@ -7,6 +7,7 @@ use Barryvdh\Debugbar\DataCollector\ViewCollector; use DebugBar\DataCollector\TimeDataCollector; use Illuminate\Events\Dispatcher; +use Illuminate\View\View; class ViewsCollectorProvider extends AbstractCollectorProvider { @@ -28,7 +29,17 @@ public function __invoke(Dispatcher $events, array $options): void $events->listen( 'composing:*', function ($event, $params) use ($viewCollector) { - $viewCollector->addView($params[0]); + /** @var View $view */ + $view = $params[0]; + $factory = $view->getFactory(); + + $renderCount = null; + if (property_exists($factory, 'renderCount')) { + $ref = new \ReflectionProperty($factory, 'renderCount'); + $renderCount = $ref->getValue($factory); + } + + $viewCollector->addView($view, $renderCount); }, ); } diff --git a/src/DataCollector/ViewCollector.php b/src/DataCollector/ViewCollector.php index cd464cf5e..891e8fcca 100644 --- a/src/DataCollector/ViewCollector.php +++ b/src/DataCollector/ViewCollector.php @@ -10,6 +10,9 @@ class ViewCollector extends TemplateCollector { + protected ?int $currentRenderCount = null; + protected array $callGraph = []; + public function getName(): string { return 'views'; @@ -18,7 +21,7 @@ public function getName(): string /** * Add a View instance to the Collector */ - public function addView(View $view): void + public function addView(View $view, ?int $renderCount = null): void { $name = $view->getName(); $type = null; @@ -59,6 +62,9 @@ public function addView(View $view): void } $this->addTemplate($name, $data, $type, $path); + if ($renderCount !== null) { + $this->addTemplateRender($name, $renderCount); + } if ($this->timeCollector !== null) { $time = microtime(true); @@ -66,6 +72,33 @@ public function addView(View $view): void } } + protected function addTemplateRender(string $name, int $renderCount): void + { + debug($renderCount, $this->currentRenderCount); + $this->callGraph[] = str_repeat(' ', $renderCount - 1) . (($renderCount > $this->currentRenderCount && $renderCount > 1) ? '└' : ' ') . $name; + $this->currentRenderCount = $renderCount; + } + + public function collect(): array + { + $data = parent::collect(); + + if ($callGraph = $this->getHtmlCallgraph()) { + $data['callgraph'] = $callGraph; + } + + return $data; + } + + public function getHtmlCallgraph(): ?string + { + if ($this->callGraph) { + return '
' . implode("\n", $this->callGraph) . '
'; + } + + return null; + } + private function getRenderSource(string $name, ?string $path): ?array { $backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 20);