Skip to content

Commit c325cee

Browse files
committed
Wrap view cache in callable which makes it memoizable
1 parent 22f4864 commit c325cee

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/Illuminate/Filesystem/Filesystem.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class Filesystem
1818
{
1919
use Conditionable, Macroable;
2020

21+
private array $cachedCallables = [];
22+
2123
/**
2224
* Determine if a file or directory exists.
2325
*
@@ -102,6 +104,31 @@ public function sharedGet($path)
102104
return $contents;
103105
}
104106

107+
/**
108+
* Get the returned callable of a file.
109+
*
110+
* @param string $path
111+
* @param array $data
112+
* @return mixed
113+
*
114+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
115+
*/
116+
public function getRequireCallable($path, array $data = [])
117+
{
118+
if ($this->isFile($path)) {
119+
$__path = $path;
120+
$__data = $data;
121+
122+
if (! isset($this->cachedCallables[$__path])) {
123+
$this->cachedCallables[$__path] = require_once $__path;
124+
}
125+
126+
return $this->cachedCallables[$__path]($__data);
127+
}
128+
129+
throw new FileNotFoundException("File does not exist at path {$path}.");
130+
}
131+
105132
/**
106133
* Get the returned value of a file.
107134
*

src/Illuminate/View/Compilers/BladeCompiler.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ public function compile($path = null)
183183
if (! is_null($this->cachePath)) {
184184
$contents = $this->compileString($this->files->get($this->getPath()));
185185

186+
$contents = implode("\n", [
187+
'<?php return function ($data) { extract($data, EXTR_SKIP); ?>',
188+
$contents,
189+
'<?php } ?>',
190+
]);
191+
186192
if (! empty($this->getPath())) {
187193
$contents = $this->appendFilePath($contents);
188194
}

src/Illuminate/View/Engines/PhpEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function evaluatePath($path, $data)
5555
// flush out any stray output that might get out before an error occurs or
5656
// an exception is thrown. This prevents any partial views from leaking.
5757
try {
58-
$this->files->getRequire($path, $data);
58+
$this->files->getRequireCallable($path, $data);
5959
} catch (Throwable $e) {
6060
$this->handleViewException($e, $obLevel);
6161
}

0 commit comments

Comments
 (0)