Skip to content

Commit b817416

Browse files
[6.x] Fix parent call (#39908)
* Fix parent call * Apply fixes from StyleCI Co-authored-by: Taylor Otwell <[email protected]>
1 parent 2d72d1f commit b817416

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

src/Illuminate/View/Compilers/Compiler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct(Filesystem $files, $cachePath)
4848
*/
4949
public function getCompiledPath($path)
5050
{
51-
return $this->cachePath.'/'.sha1($path).'.php';
51+
return $this->cachePath.'/'.sha1('v2'.$path).'.php';
5252
}
5353

5454
/**

src/Illuminate/View/Compilers/Concerns/CompilesLayouts.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Illuminate\View\Compilers\Concerns;
44

5-
use Illuminate\View\Factory as ViewFactory;
6-
75
trait CompilesLayouts
86
{
97
/**
@@ -50,7 +48,9 @@ protected function compileSection($expression)
5048
*/
5149
protected function compileParent()
5250
{
53-
return ViewFactory::parentPlaceholder($this->lastSection ?: '');
51+
$escapedLastSection = strtr($this->lastSection, ['\\' => '\\\\', "'" => "\\'"]);
52+
53+
return "<?php echo \Illuminate\View\Factory::parentPlaceholder('{$escapedLastSection}'); ?>";
5454
}
5555

5656
/**

src/Illuminate/View/Concerns/ManagesLayouts.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\View\Concerns;
44

55
use Illuminate\Contracts\View\View;
6+
use Illuminate\Support\Str;
67
use InvalidArgumentException;
78

89
trait ManagesLayouts
@@ -28,6 +29,13 @@ trait ManagesLayouts
2829
*/
2930
protected static $parentPlaceholder = [];
3031

32+
/**
33+
* The parent placeholder salt for the request.
34+
*
35+
* @var string
36+
*/
37+
protected static $parentPlaceholderSalt;
38+
3139
/**
3240
* Start injecting content into a section.
3341
*
@@ -168,12 +176,28 @@ public function yieldContent($section, $default = '')
168176
public static function parentPlaceholder($section = '')
169177
{
170178
if (! isset(static::$parentPlaceholder[$section])) {
171-
static::$parentPlaceholder[$section] = '##parent-placeholder-'.sha1($section).'##';
179+
$salt = static::parentPlaceholderSalt();
180+
181+
static::$parentPlaceholder[$section] = '##parent-placeholder-'.sha1($salt.$section).'##';
172182
}
173183

174184
return static::$parentPlaceholder[$section];
175185
}
176186

187+
/**
188+
* Get the parent placeholder salt.
189+
*
190+
* @return string
191+
*/
192+
protected static function parentPlaceholderSalt()
193+
{
194+
if (! static::$parentPlaceholderSalt) {
195+
return static::$parentPlaceholderSalt = Str::random(40);
196+
}
197+
198+
return static::$parentPlaceholderSalt;
199+
}
200+
177201
/**
178202
* Check if section exists.
179203
*

tests/View/ViewBladeCompilerTest.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected function tearDown(): void
1818
public function testIsExpiredReturnsTrueIfCompiledFileDoesntExist()
1919
{
2020
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
21-
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('foo').'.php')->andReturn(false);
21+
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(false);
2222
$this->assertTrue($compiler->isExpired('foo'));
2323
}
2424

@@ -33,31 +33,31 @@ public function testCannotConstructWithBadCachePath()
3333
public function testIsExpiredReturnsTrueWhenModificationTimesWarrant()
3434
{
3535
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
36-
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('foo').'.php')->andReturn(true);
36+
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(true);
3737
$files->shouldReceive('lastModified')->once()->with('foo')->andReturn(100);
38-
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('foo').'.php')->andReturn(0);
38+
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(0);
3939
$this->assertTrue($compiler->isExpired('foo'));
4040
}
4141

4242
public function testCompilePathIsProperlyCreated()
4343
{
4444
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
45-
$this->assertEquals(__DIR__.'/'.sha1('foo').'.php', $compiler->getCompiledPath('foo'));
45+
$this->assertEquals(__DIR__.'/'.sha1('v2foo').'.php', $compiler->getCompiledPath('foo'));
4646
}
4747

4848
public function testCompileCompilesFileAndReturnsContents()
4949
{
5050
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
5151
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
52-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
52+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
5353
$compiler->compile('foo');
5454
}
5555

5656
public function testCompileCompilesAndGetThePath()
5757
{
5858
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
5959
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
60-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
60+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
6161
$compiler->compile('foo');
6262
$this->assertSame('foo', $compiler->getPath());
6363
}
@@ -73,7 +73,7 @@ public function testCompileWithPathSetBefore()
7373
{
7474
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
7575
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
76-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
76+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
7777
// set path before compilation
7878
$compiler->setPath('foo');
7979
// trigger compilation with $path
@@ -103,7 +103,7 @@ public function testIncludePathToTemplate($content, $compiled)
103103
{
104104
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
105105
$files->shouldReceive('get')->once()->with('foo')->andReturn($content);
106-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', $compiled);
106+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', $compiled);
107107

108108
$compiler->compile('foo');
109109
}
@@ -157,7 +157,7 @@ public function testDontIncludeEmptyPath()
157157
{
158158
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
159159
$files->shouldReceive('get')->once()->with('')->andReturn('Hello World');
160-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('').'.php', 'Hello World');
160+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2').'.php', 'Hello World');
161161
$compiler->setPath('');
162162
$compiler->compile();
163163
}
@@ -166,7 +166,7 @@ public function testDontIncludeNullPath()
166166
{
167167
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
168168
$files->shouldReceive('get')->once()->with(null)->andReturn('Hello World');
169-
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1(null).'.php', 'Hello World');
169+
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2').'.php', 'Hello World');
170170
$compiler->setPath(null);
171171
$compiler->compile();
172172
}

0 commit comments

Comments
 (0)