Skip to content

Commit

Permalink
Fix appending path to inline Blade views
Browse files Browse the repository at this point in the history
When appending the view path to Blade views with only one line and opening tags we should take into consideration for closing the PHP tag first before appending the path. I've addeded tests which should cover quite a few situations.

Thanks to Sisve for making me aware of this: laravel/framework#28117 (comment)
  • Loading branch information
driesvints committed Apr 9, 2019
1 parent 2aa70e0 commit 5930b92
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 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)
}
}

/**
* Returns the open and closing PHP tag tokens which are present in the compiled contents.
*
* @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

0 comments on commit 5930b92

Please sign in to comment.