Skip to content

Commit

Permalink
split blade compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 27, 2016
1 parent 5e394bb commit 7cdb6a6
Show file tree
Hide file tree
Showing 13 changed files with 817 additions and 721 deletions.
761 changes: 40 additions & 721 deletions src/Illuminate/View/Compilers/BladeCompiler.php

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesAuthorizations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesAuthorizations
{
/**
* Compile the can statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileCan($expression)
{
return "<?php if (app('Illuminate\\Contracts\\Auth\\Access\\Gate')->check{$expression}): ?>";
}

/**
* Compile the cannot statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileCannot($expression)
{
return "<?php if (app('Illuminate\\Contracts\\Auth\\Access\\Gate')->denies{$expression}): ?>";
}

/**
* Compile the else-can statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileElsecan($expression)
{
return "<?php elseif (app('Illuminate\\Contracts\\Auth\\Access\\Gate')->check{$expression}): ?>";
}

/**
* Compile the else-can statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileElsecannot($expression)
{
return "<?php elseif (app('Illuminate\\Contracts\\Auth\\Access\\Gate')->denies{$expression}): ?>";
}

/**
* Compile the end-can statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndcan($expression)
{
return '<?php endif; ?>';
}

/**
* Compile the end-cannot statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndcannot($expression)
{
return '<?php endif; ?>';
}
}
19 changes: 19 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesComments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesComments
{
/**
* Compile Blade comments into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileComments($value)
{
$pattern = sprintf('/%s--(.*?)--%s/s', $this->contentTags[0], $this->contentTags[1]);

return preg_replace($pattern, '', $value);
}
}
50 changes: 50 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesComponents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesComponents
{
/**
* Compile the component statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileComponent($expression)
{
return "<?php \$__env->startComponent{$expression}; ?>";
}

/**
* Compile the end component statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndComponent($expression)
{
return '<?php echo $__env->renderComponent(); ?>';
}

/**
* Compile the slot statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileSlot($expression)
{
return "<?php \$__env->slot{$expression}; ?>";
}

/**
* Compile the end slot statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndSlot($expression)
{
return '<?php $__env->endSlot(); ?>';
}
}
83 changes: 83 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesConditionals
{
/**
* Compile the has section statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileHasSection($expression)
{
return "<?php if (! empty(trim(\$__env->yieldContent{$expression}))): ?>";
}

/**
* Compile the if statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileIf($expression)
{
return "<?php if{$expression}: ?>";
}

/**
* Compile the unless statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileUnless($expression)
{
return "<?php if (! $expression): ?>";
}

/**
* Compile the else-if statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileElseif($expression)
{
return "<?php elseif{$expression}: ?>";
}

/**
* Compile the else statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileElse($expression)
{
return '<?php else: ?>';
}

/**
* Compile the end-if statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndif($expression)
{
return '<?php endif; ?>';
}

/**
* Compile the end unless statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndunless($expression)
{
return '<?php endif; ?>';
}
}
105 changes: 105 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesEchos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesEchos
{
/**
* Compile Blade echos into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileEchos($value)
{
foreach ($this->getEchoMethods() as $method) {
$value = $this->$method($value);
}

return $value;
}

/**
* Get the echo methods in the proper order for compilation.
*
* @return array
*/
protected function getEchoMethods()
{
return [
'compileRawEchos',
'compileEscapedEchos',
'compileRegularEchos',
];
}

/**
* Compile the "raw" echo statements.
*
* @param string $value
* @return string
*/
protected function compileRawEchos($value)
{
$pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->rawTags[0], $this->rawTags[1]);

$callback = function ($matches) {
$whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];

return $matches[1] ? substr($matches[0], 1) : '<?php echo '.$this->compileEchoDefaults($matches[2]).'; ?>'.$whitespace;
};

return preg_replace_callback($pattern, $callback, $value);
}

/**
* Compile the "regular" echo statements.
*
* @param string $value
* @return string
*/
protected function compileRegularEchos($value)
{
$pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->contentTags[0], $this->contentTags[1]);

$callback = function ($matches) {
$whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];

$wrapped = sprintf($this->echoFormat, $this->compileEchoDefaults($matches[2]));

return $matches[1] ? substr($matches[0], 1) : '<?php echo '.$wrapped.'; ?>'.$whitespace;
};

return preg_replace_callback($pattern, $callback, $value);
}

/**
* Compile the escaped echo statements.
*
* @param string $value
* @return string
*/
protected function compileEscapedEchos($value)
{
$pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->escapedTags[0], $this->escapedTags[1]);

$callback = function ($matches) {
$whitespace = empty($matches[3]) ? '' : $matches[3].$matches[3];

return $matches[1] ? $matches[0] : '<?php echo e('.$this->compileEchoDefaults($matches[2]).'); ?>'.$whitespace;
};

return preg_replace_callback($pattern, $callback, $value);
}

/**
* Compile the default values for the echo statement.
*
* @param string $value
* @return string
*/
public function compileEchoDefaults($value)
{
return preg_replace('/^(?=\$)(.+?)(?:\s+or\s+)(.+?)$/s', 'isset($1) ? $1 : $2', $value);
}
}
43 changes: 43 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesIncludes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesIncludes
{
/**
* Compile the each statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEach($expression)
{
return "<?php echo \$__env->renderEach{$expression}; ?>";
}

/**
* Compile the include statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileInclude($expression)
{
$expression = $this->stripParentheses($expression);

return "<?php echo \$__env->make($expression, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
}

/**
* Compile the include statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileIncludeIf($expression)
{
$expression = $this->stripParentheses($expression);

return "<?php if (\$__env->exists($expression)) echo \$__env->make($expression, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
}
}
19 changes: 19 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesInjections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesInjections
{
/**
* Compile the inject statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileInject($expression)
{
$segments = explode(',', preg_replace("/[\(\)\\\"\']/", '', $expression));

return '<?php $'.trim($segments[0])." = app('".trim($segments[1])."'); ?>";
}
}
Loading

0 comments on commit 7cdb6a6

Please sign in to comment.