Skip to content

Commit 3767a68

Browse files
committed
Merge branch 'view-this-leak'
2 parents ce63f82 + a9010ba commit 3767a68

File tree

8 files changed

+84
-21
lines changed

8 files changed

+84
-21
lines changed

src/Illuminate/Filesystem/Filesystem.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,22 @@ public function sharedGet($path)
8686
* Get the returned value of a file.
8787
*
8888
* @param string $path
89+
* @param array $data
8990
* @return mixed
9091
*
9192
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
9293
*/
93-
public function getRequire($path)
94+
public function getRequire($path, array $data = [])
9495
{
9596
if ($this->isFile($path)) {
96-
return require $path;
97+
$__path = $path;
98+
$__data = $data;
99+
100+
return (static function () use ($__path, $__data) {
101+
extract($__data, EXTR_SKIP);
102+
103+
return require $__path;
104+
})();
97105
}
98106

99107
throw new FileNotFoundException("File does not exist at path {$path}.");
@@ -102,12 +110,24 @@ public function getRequire($path)
102110
/**
103111
* Require the given file once.
104112
*
105-
* @param string $file
113+
* @param string $path
114+
* @param array $data
106115
* @return mixed
107116
*/
108-
public function requireOnce($file)
117+
public function requireOnce($path, array $data = [])
109118
{
110-
require_once $file;
119+
if ($this->isFile($path)) {
120+
$__path = $path;
121+
$__data = $data;
122+
123+
return (static function () use ($__path, $__data) {
124+
extract($__data, EXTR_SKIP);
125+
126+
return require_once $__path;
127+
})();
128+
}
129+
130+
throw new FileNotFoundException("File does not exist at path {$path}.");
111131
}
112132

113133
/**

src/Illuminate/View/Engines/CompilerEngine.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\View\Engines;
44

55
use ErrorException;
6+
use Illuminate\Filesystem\Filesystem;
67
use Illuminate\View\Compilers\CompilerInterface;
78
use Throwable;
89

@@ -23,13 +24,16 @@ class CompilerEngine extends PhpEngine
2324
protected $lastCompiled = [];
2425

2526
/**
26-
* Create a new Blade view engine instance.
27+
* Create a new compiler engine instance.
2728
*
2829
* @param \Illuminate\View\Compilers\CompilerInterface $compiler
30+
* @param \Illuminate\Filesystem\Filesystem $files
2931
* @return void
3032
*/
31-
public function __construct(CompilerInterface $compiler)
33+
public function __construct(CompilerInterface $compiler, Filesystem $files)
3234
{
35+
parent::__construct($files);
36+
3337
$this->compiler = $compiler;
3438
}
3539

src/Illuminate/View/Engines/FileEngine.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,28 @@
33
namespace Illuminate\View\Engines;
44

55
use Illuminate\Contracts\View\Engine;
6+
use Illuminate\Filesystem\Filesystem;
67

78
class FileEngine implements Engine
89
{
10+
/**
11+
* The filesystem instance.
12+
*
13+
* @var \Illuminate\Filesystem\Filesystem
14+
*/
15+
protected $files;
16+
17+
/**
18+
* Create a new file engine instance.
19+
*
20+
* @param \Illuminate\Filesystem\Filesystem $files
21+
* @return void
22+
*/
23+
public function __construct(Filesystem $files)
24+
{
25+
$this->files = $files;
26+
}
27+
928
/**
1029
* Get the evaluated contents of the view.
1130
*
@@ -15,6 +34,6 @@ class FileEngine implements Engine
1534
*/
1635
public function get($path, array $data = [])
1736
{
18-
return file_get_contents($path);
37+
return $this->files->get($path);
1938
}
2039
}

src/Illuminate/View/Engines/PhpEngine.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,29 @@
33
namespace Illuminate\View\Engines;
44

55
use Illuminate\Contracts\View\Engine;
6+
use Illuminate\Filesystem\Filesystem;
67
use Throwable;
78

89
class PhpEngine implements Engine
910
{
11+
/**
12+
* The filesystem instance.
13+
*
14+
* @var \Illuminate\Filesystem\Filesystem
15+
*/
16+
protected $files;
17+
18+
/**
19+
* Create a new file engine instance.
20+
*
21+
* @param \Illuminate\Filesystem\Filesystem $files
22+
* @return void
23+
*/
24+
public function __construct(Filesystem $files)
25+
{
26+
$this->files = $files;
27+
}
28+
1029
/**
1130
* Get the evaluated contents of the view.
1231
*
@@ -22,23 +41,21 @@ public function get($path, array $data = [])
2241
/**
2342
* Get the evaluated contents of the view at the given path.
2443
*
25-
* @param string $__path
26-
* @param array $__data
44+
* @param string $path
45+
* @param array $data
2746
* @return string
2847
*/
29-
protected function evaluatePath($__path, $__data)
48+
protected function evaluatePath($path, $data)
3049
{
3150
$obLevel = ob_get_level();
3251

3352
ob_start();
3453

35-
extract($__data, EXTR_SKIP);
36-
3754
// We'll evaluate the contents of the view inside a try/catch block so we can
3855
// flush out any stray output that might get out before an error occurs or
3956
// an exception is thrown. This prevents any partial views from leaking.
4057
try {
41-
include $__path;
58+
$this->files->getRequire($path, $data);
4259
} catch (Throwable $e) {
4360
$this->handleViewException($e, $obLevel);
4461
}

src/Illuminate/View/ViewServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function registerEngineResolver()
121121
public function registerFileEngine($resolver)
122122
{
123123
$resolver->register('file', function () {
124-
return new FileEngine;
124+
return new FileEngine($this->app['files']);
125125
});
126126
}
127127

@@ -134,7 +134,7 @@ public function registerFileEngine($resolver)
134134
public function registerPhpEngine($resolver)
135135
{
136136
$resolver->register('php', function () {
137-
return new PhpEngine;
137+
return new PhpEngine($this->app['files']);
138138
});
139139
}
140140

@@ -147,7 +147,7 @@ public function registerPhpEngine($resolver)
147147
public function registerBladeEngine($resolver)
148148
{
149149
$resolver->register('blade', function () {
150-
return new CompilerEngine($this->app['blade.compiler']);
150+
return new CompilerEngine($this->app['blade.compiler'], $this->app['files']);
151151
});
152152
}
153153
}

tests/View/ViewCompilerEngineTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\View;
44

5+
use Illuminate\Filesystem\Filesystem;
56
use Illuminate\View\Compilers\CompilerInterface;
67
use Illuminate\View\Engines\CompilerEngine;
78
use Mockery as m;
@@ -40,6 +41,6 @@ public function testViewsAreNotRecompiledIfTheyAreNotExpired()
4041

4142
protected function getEngine()
4243
{
43-
return new CompilerEngine(m::mock(CompilerInterface::class));
44+
return new CompilerEngine(m::mock(CompilerInterface::class), new Filesystem);
4445
}
4546
}

tests/View/ViewFactoryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
99
use Illuminate\Contracts\View\Engine;
1010
use Illuminate\Events\Dispatcher;
11+
use Illuminate\Filesystem\Filesystem;
1112
use Illuminate\View\Compilers\CompilerInterface;
1213
use Illuminate\View\Engines\CompilerEngine;
1314
use Illuminate\View\Engines\EngineResolver;
@@ -337,7 +338,7 @@ public function testComponentHandling()
337338
{
338339
$factory = $this->getFactory();
339340
$factory->getFinder()->shouldReceive('find')->andReturn(__DIR__.'/fixtures/component.php');
340-
$factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new PhpEngine);
341+
$factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new PhpEngine(new Filesystem));
341342
$factory->getDispatcher()->shouldReceive('dispatch');
342343
$factory->startComponent('component', ['name' => 'Taylor']);
343344
$factory->slot('title');
@@ -488,7 +489,7 @@ public function testExceptionsInSectionsAreThrown()
488489
$this->expectException(ErrorException::class);
489490
$this->expectExceptionMessage('section exception message');
490491

491-
$engine = new CompilerEngine(m::mock(CompilerInterface::class));
492+
$engine = new CompilerEngine(m::mock(CompilerInterface::class), new Filesystem);
492493
$engine->getCompiler()->shouldReceive('getCompiledPath')->andReturnUsing(function ($path) {
493494
return $path;
494495
});

tests/View/ViewPhpEngineTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\View;
44

5+
use Illuminate\Filesystem\Filesystem;
56
use Illuminate\View\Engines\PhpEngine;
67
use Mockery as m;
78
use PHPUnit\Framework\TestCase;
@@ -15,7 +16,7 @@ protected function tearDown(): void
1516

1617
public function testViewsMayBeProperlyRendered()
1718
{
18-
$engine = new PhpEngine;
19+
$engine = new PhpEngine(new Filesystem);
1920
$this->assertSame('Hello World
2021
', $engine->get(__DIR__.'/fixtures/basic.php'));
2122
}

0 commit comments

Comments
 (0)