Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Fix appending path to inline Blade views #28141

Merged
merged 2 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ public function compile($path = null)
);

if (! empty($this->getPath())) {
$tokens = $this->getOpenAndClosingPhpTokens($contents);

// If the tokens we retrieved from the compiled contents have at least
// one opening tag and if that last token isn't the closing tag, we
// need to close the statement before adding the path at the end.
if ($tokens->isNotEmpty() && $tokens->last() !== T_CLOSE_TAG) {
$contents .= ' ?>';
}

$contents .= "<?php /**PATH {$this->getPath()} ENDPATH**/ ?>";
}

Expand All @@ -132,6 +141,21 @@ public function compile($path = null)
}
}

/**
* Get the open and closing PHP tag tokens from the given string.
*
* @param string $contents
* @return \Illuminate\Support\Collection
*/
protected function getOpenAndClosingPhpTokens($contents)
{
return collect(token_get_all($contents))
->pluck($tokenNumber = 0)
->filter(function ($token) {
return in_array($token, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, T_CLOSE_TAG]);
});
}

/**
* Get the path currently being compiled.
*
Expand Down
60 changes: 56 additions & 4 deletions tests/View/ViewBladeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testCompileWithPathSetBefore()
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
// set path before compilation
$compiler->setPath('foo');
// trigger compilation with null $path
// trigger compilation with $path
$compiler->compile();
$this->assertEquals('foo', $compiler->getPath());
}
Expand All @@ -93,14 +93,66 @@ public function testRawTagsCanBeSetToLegacyValues()
}}'));
}

public function testIncludePathToTemplate()
/**
* @param string $content
* @param string $compiled
*
* @dataProvider appendViewPathDataProvider
*/
public function testIncludePathToTemplate($content, $compiled)
{
$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('get')->once()->with('foo')->andReturn($content);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', $compiled);

$compiler->compile('foo');
}

/**
* @return array
*/
public function appendViewPathDataProvider()
{
return [
'No PHP blocks' => [
'Hello World',
'Hello World<?php /**PATH foo ENDPATH**/ ?>',
],
'Single PHP block without closing ?>' => [
'<?php echo $path',
'<?php echo $path ?><?php /**PATH foo ENDPATH**/ ?>',
],
'Ending PHP block.' => [
'Hello world<?php echo $path ?>',
'Hello world<?php echo $path ?><?php /**PATH foo ENDPATH**/ ?>',
],
'Ending PHP block without closing ?>' => [
'Hello world<?php echo $path',
'Hello world<?php echo $path ?><?php /**PATH foo ENDPATH**/ ?>',
],
'PHP block between content.' => [
'Hello world<?php echo $path ?>Hi There',
'Hello world<?php echo $path ?>Hi There<?php /**PATH foo ENDPATH**/ ?>',
],
'Multiple PHP blocks.' => [
'Hello world<?php echo $path ?>Hi There<?php echo $path ?>Hello Again',
'Hello world<?php echo $path ?>Hi There<?php echo $path ?>Hello Again<?php /**PATH foo ENDPATH**/ ?>',
],
'Multiple PHP blocks without closing ?>' => [
'Hello world<?php echo $path ?>Hi There<?php echo $path',
'Hello world<?php echo $path ?>Hi There<?php echo $path ?><?php /**PATH foo ENDPATH**/ ?>',
],
'Short open echo tag' => [
'Hello world<?= echo $path',
'Hello world<?= echo $path ?><?php /**PATH foo ENDPATH**/ ?>',
],
'Echo XML declaration' => [
'<?php echo \'<?xml version="1.0" encoding="UTF-8"?>\';',
'<?php echo \'<?xml version="1.0" encoding="UTF-8"?>\'; ?><?php /**PATH foo ENDPATH**/ ?>',
],
];
}

public function testDontIncludeEmptyPath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
Expand Down