Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/CollectorProviders/ViewsCollectorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
},
);
}
Expand Down
35 changes: 34 additions & 1 deletion src/DataCollector/ViewCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class ViewCollector extends TemplateCollector
{
protected ?int $currentRenderCount = null;
protected array $callGraph = [];

public function getName(): string
{
return 'views';
Expand All @@ -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;
Expand Down Expand Up @@ -59,13 +62,43 @@ 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);
$this->timeCollector->addMeasure('View: ' . $name, $time, $time, [], 'views', 'View');
}
}

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 '<pre>' . implode("\n", $this->callGraph) . '</pre>';
}

return null;
}

private function getRenderSource(string $name, ?string $path): ?array
{
$backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 20);
Expand Down