/**
* Compile Blade statements that start with "@".
*
* @param string $value
* @return mixed
*/
protected function compileStatements($value)
{// Compile Blade statements that start with "@"
$callback = function ($match) {
if (method_exists($this, $method = 'compile'.ucfirst($match[1]))) {
$match[0] = $this->$method(Arr::get($match, 3));
} elseif (isset($this->customDirectives[$match[1]])) {
$match[0] = call_user_func($this->customDirectives[$match[1]], Arr::get($match, 3));
}
return isset($match[3]) ? $match[0] : $match[0].$match[2];// get you want
};// a function be define in a function
return preg_replace_callback('/\B@(\w+)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', $callback, $value);
}// just a replace
// value handler match
// match pattern like B@block
/**
* Compile the "raw" echo statements.
*
* @param string $value
* @return string
*/
protected function compileRawEchos($value)
{// Compile the raw echo statements
$pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->rawTags[0], $this->rawTags[1]);
// get the pattern in a special way
$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;
};// a parameter function
return preg_replace_callback($pattern, $callback, $value);
}// just a preg replace callback
/**
* Compile the "regular" echo statements.
*
* @param string $value
* @return string
*/
protected function compileRegularEchos($value)
{// Compile the "regular" echo statements
$pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->contentTags[0], $this->contentTags[1]);
// a good pattern type
$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);
}// a preg replace
/**
* 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);
}// a preg replace
/**
* 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);
}// compile echo default value
// this $1 and $2 is very powerful
/**
* Compile the each statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEach($expression)
{
return "<?php echo \$__env->renderEach{$expression}; ?>";
}// compile Each env_renderEach
/**
* 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])."'); ?>";
}//Compile the inject statements into valid PHP
/**
* Compile the yield statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileYield($expression)
{
return "<?php echo \$__env->yieldContent{$expression}; ?>";
}// a yield statements
/**
* Compile the show statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileShow($expression)
{
return '<?php echo $__env->yieldSection(); ?>';
}// compile show
/**
* Compile the section statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileSection($expression)
{
return "<?php \$__env->startSection{$expression}; ?>";
}//section
/**
* Compile the append statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileAppend($expression)
{
return '<?php $__env->appendSection(); ?>';
}// Section
/**
* Compile the end-section statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndsection($expression)
{
return '<?php $__env->stopSection(); ?>';
}// stop Section
/**
* Compile the stop statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileStop($expression)
{
return '<?php $__env->stopSection(); ?>';
}//stop Section
/**
* Compile the overwrite statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileOverwrite($expression)
{
return '<?php $__env->stopSection(true); ?>';
}// Over write
/**
* Compile the unless statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileUnless($expression)
{
return "<?php if ( ! $expression): ?>";
}// CompileUnless
/**
* Compile the end unless statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndunless($expression)
{
return '<?php endif; ?>';
}//compile Endunless
/**
* Compile the lang statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileLang($expression)
{
return "<?php echo app('translator')->get$expression; ?>";
}// echo app translator
/**
* Compile the choice statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileChoice($expression)
{
return "<?php echo app('translator')->choice$expression; ?>";
}// get choice
/**
* Compile the else statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileElse($expression)
{
return '<?php else: ?>';
}// compile else
/**
* Compile the for statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileFor($expression)
{
return "<?php for{$expression}: ?>";
}// for compile For
/**
* Compile the foreach statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileForeach($expression)
{
return "<?php foreach{$expression}: ?>";
}// foreach this expression
/**
* Compile the break statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileBreak($expression)
{
return '<?php break; ?>';
}// break
/**
* Compile the continue statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileContinue($expression)
{
return '<?php continue; ?>';
}// continue;
/**
* Compile the forelse statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileForelse($expression)
{
$empty = '$__empty_'.++$this->forelseCounter;
return "<?php {$empty} = true; foreach{$expression}: {$empty} = false; ?>";
}// For else
/**
* Compile the can statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileCan($expression)
{
return "<?php if (Gate::check{$expression}): ?>";
}// Gate:: check
/**
* Compile the cannot statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileCannot($expression)
{
return "<?php if (Gate::denies{$expression}): ?>";
}//Gate:: denies
/**
* Compile the if statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileIf($expression)
{
return "<?php if{$expression}: ?>";
}// if make expression
/**
* Compile the else-if statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileElseif($expression)
{
return "<?php elseif{$expression}: ?>";
}// expression
/**
* Compile the forelse statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEmpty($expression)
{
$empty = '$__empty_'.$this->forelseCounter--;
return "<?php endforeach; if ({$empty}): ?>";
}// end empty
/**
* Compile the while statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileWhile($expression)
{
return "<?php while{$expression}: ?>";
}//while
/**
* Compile the end-while statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndwhile($expression)
{
return '<?php endwhile; ?>';
}// end while
/**
* Compile the end-for statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndfor($expression)
{
return '<?php endfor; ?>';
}// end for
/**
* Compile the end-for-each statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndforeach($expression)
{
return '<?php endforeach; ?>';
}// end for each
/**
* Compile the end-can statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndcan($expression)
{
return '<?php endif; ?>';
}// endif
/**
* Compile the end-cannot statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndcannot($expression)
{
return '<?php endif; ?>';
}// endif
/**
* Compile the end-if statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndif($expression)
{
return '<?php endif; ?>';
}// end if
/**
* Compile the end-for-else statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndforelse($expression)
{
return '<?php endif; ?>';
}// endif
/**
* Compile the extends statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileExtends($expression)
{// compile the extends statements into valid PHP
if (Str::startsWith($expression, '(')) {
$expression = substr($expression, 1, -1);
}// expression
$data = "<?php echo \$__env->make($expression, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
$this->footer[] = $data;// data
return '';
}
/**
* Compile the include statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileInclude($expression)
{
if (Str::startsWith($expression, '(')) {
$expression = substr($expression, 1, -1);
}
return "<?php echo \$__env->make($expression, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
}// compile the include statements into valid PHP
/**
* Compile the stack statements into the content.
*
* @param string $expression
* @return string
*/
protected function compileStack($expression)
{
return "<?php echo \$__env->yieldContent{$expression}; ?>";
}//compile Stack
/**
* Compile the push statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compilePush($expression)
{
return "<?php \$__env->startSection{$expression}; ?>";
}// Push
/**
* Compile the endpush statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndpush($expression)
{
return '<?php $__env->appendSection(); ?>';
}// End push
/**
* Get the extensions used by the compiler.
*
* @return array
*/
public function getExtensions()
{
return $this->extensions;
}// get extensions
/**
* Register a custom Blade compiler.
*
* @param callable $compiler
* @return void
*/
public function extend(callable $compiler)
{
$this->extensions[] = $compiler;
}// Set extend
/**
* Register a handler for custom directives.
*
* @param string $name
* @param callable $handler
* @return void
*/
public function directive($name, callable $handler)
{
$this->customDirectives[$name] = $handler;
}//directive
/**
* Get the list of custom directives.
*
* @return array
*/
public function getCustomDirectives()
{
return $this->customDirectives;
}//get Custom Directives
/**
* Gets the raw tags used by the compiler.
*
* @return array
*/
public function getRawTags()
{
return $this->rawTags;
}// get Raw Tags
/**
* Sets the raw tags used for the compiler.
*
* @param string $openTag
* @param string $closeTag
* @return void
*/
public function setRawTags($openTag, $closeTag)
{
$this->rawTags = [preg_quote($openTag), preg_quote($closeTag)];
}//set Raw Tags
/**
* Sets the content tags used for the compiler.
*
* @param string $openTag
* @param string $closeTag
* @param bool $escaped
* @return void
*/
public function setContentTags($openTag, $closeTag, $escaped = false)
{
$property = ($escaped === true) ? 'escapedTags' : 'contentTags';
$this->{$property} = [preg_quote($openTag), preg_quote($closeTag)];
// set property
}//Sets the content tags used for the compiler
/**
* Sets the escaped content tags used for the compiler.
*
* @param string $openTag
* @param string $closeTag
* @return void
*/
public function setEscapedContentTags($openTag, $closeTag)
{
$this->setContentTags($openTag, $closeTag, true);
}//set Content Tags
/**
* Gets the content tags used for the compiler.
*
* @return string
*/
public function getContentTags()
{
return $this->getTags();
}//get Tags
/**
* Gets the escaped content tags used for the compiler.
*
* @return string
*/
public function getEscapedContentTags()
{
return $this->getTags(true);
}//get
/**
* Gets the tags used for the compiler.
*
* @param bool $escaped
* @return array
*/
protected function getTags($escaped = false)
{
$tags = $escaped ? $this->escapedTags : $this->contentTags;
return array_map('stripcslashes', $tags);
}// get
/**
* Set the echo format to be used by the compiler.
*
* @param string $format
* @return void
*/
public function setEchoFormat($format)
{
$this->echoFormat = $format;
}// set echo format
}
转载于:https://blog.51cto.com/jingshanls/1845443