Skip to content

Commit

Permalink
Add includes on ViewCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed May 7, 2022
1 parent e44c5c5 commit 1a169b6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
46 changes: 35 additions & 11 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,22 @@ public function render(string $view, array $data = []) : string
$contents = $this->render($layout, $data);
}
if ($debug) {
$this->setDebugData($view, $start, $this->currentView, 'Render');
$this->setDebugData($view, $start, 'Render');
$contents = '<!-- Render start: ' . $view . ' -->'
. \PHP_EOL . $contents . \PHP_EOL
. '<!-- Render end: ' . $view . ' -->';
}
return $contents;
}

protected function setDebugData(string $file, float $start, string $filepath, string $type) : static
protected function setDebugData(string $file, float $start, string $type) : static
{
$end = \microtime(true);
$this->debugCollector->addData([
'start' => $start,
'end' => $end,
'file' => $file,
'filepath' => $filepath,
'filepath' => $this->getFilepath($file),
'type' => $type,
]);
return $this;
Expand Down Expand Up @@ -288,13 +288,10 @@ public function currentBlock() : ?string
public function include(string $view, array $data = []) : string
{
$view = $this->getIncludePrefix() . $view;
$this->inInclude = true;
$contents = $this->getContents($view, $data);
$this->inInclude = false;
if (isset($this->debugCollector)) {
return $this->involveInclude($view, $contents);
return $this->getIncludeContentsWithDebug($view, $data);
}
return $contents;
return $this->getIncludeContents($view, $data);
}

protected function involveInclude(string $view, string $contents) : string
Expand All @@ -311,13 +308,40 @@ protected function involveInclude(string $view, string $contents) : string
* @return string
*/
public function includeWithoutPrefix(string $view, array $data = []) : string
{
if (isset($this->debugCollector)) {
return $this->getIncludeContentsWithDebug($view, $data);
}
return $this->getIncludeContents($view, $data);
}

/**
* @param string $view
* @param array<string,mixed> $data
*
* @return string
*/
protected function getIncludeContentsWithDebug(string $view, array $data = []) : string
{
$start = \microtime(true);
$this->inInclude = true;
$contents = $this->getContents($view, $data);
$this->inInclude = false;
$this->setDebugData($view, $start, 'include');
return $this->involveInclude($view, $contents);
}

/**
* @param string $view
* @param array<string,mixed> $data
*
* @return string
*/
protected function getIncludeContents(string $view, array $data = []) : string
{
$this->inInclude = true;
$contents = $this->getContents($view, $data);
$this->inInclude = false;
if (isset($this->debugCollector)) {
return $this->involveInclude($view, $contents);
}
return $contents;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Debug/ViewCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ public function testRenderedViews() : void
self::assertStringContainsString('Rendered Views', $contents);
self::assertStringNotContainsString('No view has been rendered', $contents);
self::assertStringContainsString('home/index', $contents);
self::assertStringContainsString('_includes/footer', $contents);
self::assertStringContainsString('_layouts/default', $contents);
self::assertStringContainsString(
'Total of 2 rendered view files',
'Total of 3 rendered view files',
$contents
);
}
Expand Down

0 comments on commit 1a169b6

Please sign in to comment.