Skip to content

Commit

Permalink
[6.x] Fix parent call (#39908)
Browse files Browse the repository at this point in the history
* Fix parent call

* Apply fixes from StyleCI

Co-authored-by: Taylor Otwell <taylorotwell@users.noreply.github.com>
  • Loading branch information
driesvints and taylorotwell committed Dec 6, 2021
1 parent 2d72d1f commit b817416
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/View/Compilers/Compiler.php
Expand Up @@ -48,7 +48,7 @@ public function __construct(Filesystem $files, $cachePath)
*/
public function getCompiledPath($path)
{
return $this->cachePath.'/'.sha1($path).'.php';
return $this->cachePath.'/'.sha1('v2'.$path).'.php';
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/View/Compilers/Concerns/CompilesLayouts.php
Expand Up @@ -2,8 +2,6 @@

namespace Illuminate\View\Compilers\Concerns;

use Illuminate\View\Factory as ViewFactory;

trait CompilesLayouts
{
/**
Expand Down Expand Up @@ -50,7 +48,9 @@ protected function compileSection($expression)
*/
protected function compileParent()
{
return ViewFactory::parentPlaceholder($this->lastSection ?: '');
$escapedLastSection = strtr($this->lastSection, ['\\' => '\\\\', "'" => "\\'"]);

return "<?php echo \Illuminate\View\Factory::parentPlaceholder('{$escapedLastSection}'); ?>";
}

/**
Expand Down
26 changes: 25 additions & 1 deletion src/Illuminate/View/Concerns/ManagesLayouts.php
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\View\Concerns;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Str;
use InvalidArgumentException;

trait ManagesLayouts
Expand All @@ -28,6 +29,13 @@ trait ManagesLayouts
*/
protected static $parentPlaceholder = [];

/**
* The parent placeholder salt for the request.
*
* @var string
*/
protected static $parentPlaceholderSalt;

/**
* Start injecting content into a section.
*
Expand Down Expand Up @@ -168,12 +176,28 @@ public function yieldContent($section, $default = '')
public static function parentPlaceholder($section = '')
{
if (! isset(static::$parentPlaceholder[$section])) {
static::$parentPlaceholder[$section] = '##parent-placeholder-'.sha1($section).'##';
$salt = static::parentPlaceholderSalt();

static::$parentPlaceholder[$section] = '##parent-placeholder-'.sha1($salt.$section).'##';
}

return static::$parentPlaceholder[$section];
}

/**
* Get the parent placeholder salt.
*
* @return string
*/
protected static function parentPlaceholderSalt()
{
if (! static::$parentPlaceholderSalt) {
return static::$parentPlaceholderSalt = Str::random(40);
}

return static::$parentPlaceholderSalt;
}

/**
* Check if section exists.
*
Expand Down
20 changes: 10 additions & 10 deletions tests/View/ViewBladeCompilerTest.php
Expand Up @@ -18,7 +18,7 @@ protected function tearDown(): void
public function testIsExpiredReturnsTrueIfCompiledFileDoesntExist()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('foo').'.php')->andReturn(false);
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(false);
$this->assertTrue($compiler->isExpired('foo'));
}

Expand All @@ -33,31 +33,31 @@ public function testCannotConstructWithBadCachePath()
public function testIsExpiredReturnsTrueWhenModificationTimesWarrant()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('foo').'.php')->andReturn(true);
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(true);
$files->shouldReceive('lastModified')->once()->with('foo')->andReturn(100);
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('foo').'.php')->andReturn(0);
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('v2foo').'.php')->andReturn(0);
$this->assertTrue($compiler->isExpired('foo'));
}

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

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

public function testCompileCompilesAndGetThePath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
$compiler->compile('foo');
$this->assertSame('foo', $compiler->getPath());
}
Expand All @@ -73,7 +73,7 @@ public function testCompileWithPathSetBefore()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
// set path before compilation
$compiler->setPath('foo');
// trigger compilation with $path
Expand Down Expand Up @@ -103,7 +103,7 @@ public function testIncludePathToTemplate($content, $compiled)
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn($content);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', $compiled);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2foo').'.php', $compiled);

$compiler->compile('foo');
}
Expand Down Expand Up @@ -157,7 +157,7 @@ public function testDontIncludeEmptyPath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('')->andReturn('Hello World');
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('').'.php', 'Hello World');
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2').'.php', 'Hello World');
$compiler->setPath('');
$compiler->compile();
}
Expand All @@ -166,7 +166,7 @@ public function testDontIncludeNullPath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with(null)->andReturn('Hello World');
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1(null).'.php', 'Hello World');
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('v2').'.php', 'Hello World');
$compiler->setPath(null);
$compiler->compile();
}
Expand Down

0 comments on commit b817416

Please sign in to comment.