Skip to content

Commit

Permalink
Wrap view cache in callable which makes it memoizable
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed Apr 19, 2024
1 parent 22f4864 commit c325cee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Filesystem
{
use Conditionable, Macroable;

private array $cachedCallables = [];

/**
* Determine if a file or directory exists.
*
Expand Down Expand Up @@ -102,6 +104,31 @@ public function sharedGet($path)
return $contents;
}

/**
* Get the returned callable of a file.
*
* @param string $path
* @param array $data
* @return mixed
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function getRequireCallable($path, array $data = [])
{
if ($this->isFile($path)) {
$__path = $path;
$__data = $data;

if (! isset($this->cachedCallables[$__path])) {
$this->cachedCallables[$__path] = require_once $__path;
}

return $this->cachedCallables[$__path]($__data);
}

throw new FileNotFoundException("File does not exist at path {$path}.");
}

/**
* Get the returned value of a file.
*
Expand Down
6 changes: 6 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ public function compile($path = null)
if (! is_null($this->cachePath)) {
$contents = $this->compileString($this->files->get($this->getPath()));

$contents = implode("\n", [
'<?php return function ($data) { extract($data, EXTR_SKIP); ?>',
$contents,
'<?php } ?>',
]);

if (! empty($this->getPath())) {
$contents = $this->appendFilePath($contents);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/View/Engines/PhpEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function evaluatePath($path, $data)
// flush out any stray output that might get out before an error occurs or
// an exception is thrown. This prevents any partial views from leaking.
try {
$this->files->getRequire($path, $data);
$this->files->getRequireCallable($path, $data);
} catch (Throwable $e) {
$this->handleViewException($e, $obLevel);
}
Expand Down

0 comments on commit c325cee

Please sign in to comment.