123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- <?php
-
- namespace Illuminate\View\Compilers;
-
- use Illuminate\Support\Arr;
- use Illuminate\Support\Str;
-
- class BladeCompiler extends Compiler implements CompilerInterface
- {
- use Concerns\CompilesAuthorizations,
- Concerns\CompilesComments,
- Concerns\CompilesComponents,
- Concerns\CompilesConditionals,
- Concerns\CompilesEchos,
- Concerns\CompilesHelpers,
- Concerns\CompilesIncludes,
- Concerns\CompilesInjections,
- Concerns\CompilesJson,
- Concerns\CompilesLayouts,
- Concerns\CompilesLoops,
- Concerns\CompilesRawPhp,
- Concerns\CompilesStacks,
- Concerns\CompilesTranslations;
-
- /**
- * All of the registered extensions.
- *
- * @var array
- */
- protected $extensions = [];
-
- /**
- * All custom "directive" handlers.
- *
- * @var array
- */
- protected $customDirectives = [];
-
- /**
- * All custom "condition" handlers.
- *
- * @var array
- */
- protected $conditions = [];
-
- /**
- * The file currently being compiled.
- *
- * @var string
- */
- protected $path;
-
- /**
- * All of the available compiler functions.
- *
- * @var array
- */
- protected $compilers = [
- 'Comments',
- 'Extensions',
- 'Statements',
- 'Echos',
- ];
-
- /**
- * Array of opening and closing tags for raw echos.
- *
- * @var array
- */
- protected $rawTags = ['{!!', '!!}'];
-
- /**
- * Array of opening and closing tags for regular echos.
- *
- * @var array
- */
- protected $contentTags = ['{{', '}}'];
-
- /**
- * Array of opening and closing tags for escaped echos.
- *
- * @var array
- */
- protected $escapedTags = ['{{{', '}}}'];
-
- /**
- * The "regular" / legacy echo string format.
- *
- * @var string
- */
- protected $echoFormat = 'e(%s)';
-
- /**
- * Array of footer lines to be added to template.
- *
- * @var array
- */
- protected $footer = [];
-
- /**
- * Array to temporary store the raw blocks found in the template.
- *
- * @var array
- */
- protected $rawBlocks = [];
-
- /**
- * Compile the view at the given path.
- *
- * @param string $path
- * @return void
- */
- public function compile($path = null)
- {
- if ($path) {
- $this->setPath($path);
- }
-
- if (! is_null($this->cachePath)) {
- $contents = $this->compileString($this->files->get($this->getPath()));
-
- $this->files->put($this->getCompiledPath($this->getPath()), $contents);
- }
- }
-
- /**
- * Get the path currently being compiled.
- *
- * @return string
- */
- public function getPath()
- {
- return $this->path;
- }
-
- /**
- * Set the path currently being compiled.
- *
- * @param string $path
- * @return void
- */
- public function setPath($path)
- {
- $this->path = $path;
- }
-
- /**
- * Compile the given Blade template contents.
- *
- * @param string $value
- * @return string
- */
- public function compileString($value)
- {
- if (strpos($value, '@verbatim') !== false) {
- $value = $this->storeVerbatimBlocks($value);
- }
-
- $this->footer = [];
-
- if (strpos($value, '@php') !== false) {
- $value = $this->storePhpBlocks($value);
- }
-
- $result = '';
-
- // Here we will loop through all of the tokens returned by the Zend lexer and
- // parse each one into the corresponding valid PHP. We will then have this
- // template as the correctly rendered PHP that can be rendered natively.
- foreach (token_get_all($value) as $token) {
- $result .= is_array($token) ? $this->parseToken($token) : $token;
- }
-
- if (! empty($this->rawBlocks)) {
- $result = $this->restoreRawContent($result);
- }
-
- // If there are any footer lines that need to get added to a template we will
- // add them here at the end of the template. This gets used mainly for the
- // template inheritance via the extends keyword that should be appended.
- if (count($this->footer) > 0) {
- $result = $this->addFooters($result);
- }
-
- return $result;
- }
-
- /**
- * Store the verbatim blocks and replace them with a temporary placeholder.
- *
- * @param string $value
- * @return string
- */
- protected function storeVerbatimBlocks($value)
- {
- return preg_replace_callback('/(?<!@)@verbatim(.*?)@endverbatim/s', function ($matches) {
- return $this->storeRawBlock($matches[1]);
- }, $value);
- }
-
- /**
- * Store the PHP blocks and replace them with a temporary placeholder.
- *
- * @param string $value
- * @return string
- */
- protected function storePhpBlocks($value)
- {
- return preg_replace_callback('/(?<!@)@php(.*?)@endphp/s', function ($matches) {
- return $this->storeRawBlock("<?php{$matches[1]}?>");
- }, $value);
- }
-
- /**
- * Store a raw block and return a unique raw placeholder.
- *
- * @param string $value
- * @return string
- */
- protected function storeRawBlock($value)
- {
- return $this->getRawPlaceholder(
- array_push($this->rawBlocks, $value) - 1
- );
- }
-
- /**
- * Replace the raw placeholders with the original code stored in the raw blocks.
- *
- * @param string $result
- * @return string
- */
- protected function restoreRawContent($result)
- {
- $result = preg_replace_callback('/'.$this->getRawPlaceholder('(\d+)').'/', function ($matches) {
- return $this->rawBlocks[$matches[1]];
- }, $result);
-
- $this->rawBlocks = [];
-
- return $result;
- }
-
- /**
- * Get a placeholder to temporary mark the position of raw blocks.
- *
- * @param int|string $replace
- * @return string
- */
- protected function getRawPlaceholder($replace)
- {
- return str_replace('#', $replace, '@__raw_block_#__@');
- }
-
- /**
- * Add the stored footers onto the given content.
- *
- * @param string $result
- * @return string
- */
- protected function addFooters($result)
- {
- return ltrim($result, PHP_EOL)
- .PHP_EOL.implode(PHP_EOL, array_reverse($this->footer));
- }
-
- /**
- * Parse the tokens from the template.
- *
- * @param array $token
- * @return string
- */
- protected function parseToken($token)
- {
- list($id, $content) = $token;
-
- if ($id == T_INLINE_HTML) {
- foreach ($this->compilers as $type) {
- $content = $this->{"compile{$type}"}($content);
- }
- }
-
- return $content;
- }
-
- /**
- * Execute the user defined extensions.
- *
- * @param string $value
- * @return string
- */
- protected function compileExtensions($value)
- {
- foreach ($this->extensions as $compiler) {
- $value = call_user_func($compiler, $value, $this);
- }
-
- return $value;
- }
-
- /**
- * Compile Blade statements that start with "@".
- *
- * @param string $value
- * @return string
- */
- protected function compileStatements($value)
- {
- return preg_replace_callback(
- '/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', function ($match) {
- return $this->compileStatement($match);
- }, $value
- );
- }
-
- /**
- * Compile a single Blade @ statement.
- *
- * @param array $match
- * @return string
- */
- protected function compileStatement($match)
- {
- if (Str::contains($match[1], '@')) {
- $match[0] = isset($match[3]) ? $match[1].$match[3] : $match[1];
- } elseif (isset($this->customDirectives[$match[1]])) {
- $match[0] = $this->callCustomDirective($match[1], Arr::get($match, 3));
- } elseif (method_exists($this, $method = 'compile'.ucfirst($match[1]))) {
- $match[0] = $this->$method(Arr::get($match, 3));
- }
-
- return isset($match[3]) ? $match[0] : $match[0].$match[2];
- }
-
- /**
- * Call the given directive with the given value.
- *
- * @param string $name
- * @param string|null $value
- * @return string
- */
- protected function callCustomDirective($name, $value)
- {
- if (Str::startsWith($value, '(') && Str::endsWith($value, ')')) {
- $value = Str::substr($value, 1, -1);
- }
-
- return call_user_func($this->customDirectives[$name], trim($value));
- }
-
- /**
- * Strip the parentheses from the given expression.
- *
- * @param string $expression
- * @return string
- */
- public function stripParentheses($expression)
- {
- if (Str::startsWith($expression, '(')) {
- $expression = substr($expression, 1, -1);
- }
-
- return $expression;
- }
-
- /**
- * Register a custom Blade compiler.
- *
- * @param callable $compiler
- * @return void
- */
- public function extend(callable $compiler)
- {
- $this->extensions[] = $compiler;
- }
-
- /**
- * Get the extensions used by the compiler.
- *
- * @return array
- */
- public function getExtensions()
- {
- return $this->extensions;
- }
-
- /**
- * Register an "if" statement directive.
- *
- * @param string $name
- * @param callable $callback
- * @return void
- */
- public function if($name, callable $callback)
- {
- $this->conditions[$name] = $callback;
-
- $this->directive($name, function ($expression) use ($name) {
- return $expression !== ''
- ? "<?php if (\Illuminate\Support\Facades\Blade::check('{$name}', {$expression})): ?>"
- : "<?php if (\Illuminate\Support\Facades\Blade::check('{$name}')): ?>";
- });
-
- $this->directive('else'.$name, function ($expression) use ($name) {
- return $expression !== ''
- ? "<?php elseif (\Illuminate\Support\Facades\Blade::check('{$name}', {$expression})): ?>"
- : "<?php elseif (\Illuminate\Support\Facades\Blade::check('{$name}')): ?>";
- });
-
- $this->directive('end'.$name, function () {
- return '<?php endif; ?>';
- });
- }
-
- /**
- * Check the result of a condition.
- *
- * @param string $name
- * @param array $parameters
- * @return bool
- */
- public function check($name, ...$parameters)
- {
- return call_user_func($this->conditions[$name], ...$parameters);
- }
-
- /**
- * Register a component alias directive.
- *
- * @param string $path
- * @param string $alias
- * @return void
- */
- public function component($path, $alias = null)
- {
- $alias = $alias ?: array_last(explode('.', $path));
-
- $this->directive($alias, function ($expression) use ($path) {
- return $expression
- ? "<?php \$__env->startComponent('{$path}', {$expression}); ?>"
- : "<?php \$__env->startComponent('{$path}'); ?>";
- });
-
- $this->directive('end'.$alias, function ($expression) {
- return '<?php echo $__env->renderComponent(); ?>';
- });
- }
-
- /**
- * Register an include alias directive.
- *
- * @param string $path
- * @param string $alias
- * @return void
- */
- public function include($path, $alias = null)
- {
- $alias = $alias ?: array_last(explode('.', $path));
-
- $this->directive($alias, function ($expression) use ($path) {
- $expression = $this->stripParentheses($expression) ?: '[]';
-
- return "<?php echo \$__env->make('{$path}', {$expression}, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
- });
- }
-
- /**
- * Register a handler for custom directives.
- *
- * @param string $name
- * @param callable $handler
- * @return void
- */
- public function directive($name, callable $handler)
- {
- $this->customDirectives[$name] = $handler;
- }
-
- /**
- * Get the list of custom directives.
- *
- * @return array
- */
- public function getCustomDirectives()
- {
- return $this->customDirectives;
- }
-
- /**
- * Set the echo format to be used by the compiler.
- *
- * @param string $format
- * @return void
- */
- public function setEchoFormat($format)
- {
- $this->echoFormat = $format;
- }
-
- /**
- * Set the "echo" format to double encode entities.
- *
- * @return void
- */
- public function withDoubleEncoding()
- {
- $this->setEchoFormat('e(%s, true)');
- }
-
- /**
- * Set the "echo" format to not double encode entities.
- *
- * @return void
- */
- public function withoutDoubleEncoding()
- {
- $this->setEchoFormat('e(%s, false)');
- }
- }
|