Laravel version update

Laravel version update
This commit is contained in:
Manish Verma
2018-08-06 18:48:58 +05:30
parent d143048413
commit 126fbb0255
13678 changed files with 1031482 additions and 778530 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -68,8 +68,7 @@ abstract class Compiler
return true;
}
$lastModified = $this->files->lastModified($path);
return $lastModified >= $this->files->lastModified($compiled);
return $this->files->lastModified($path) >=
$this->files->lastModified($compiled);
}
}

View File

@@ -0,0 +1,70 @@
<?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::class)->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::class)->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::class)->check{$expression}): ?>";
}
/**
* Compile the else-cannot statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileElsecannot($expression)
{
return "<?php elseif (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->denies{$expression}): ?>";
}
/**
* Compile the end-can statements into valid PHP.
*
* @return string
*/
protected function compileEndcan()
{
return '<?php endif; ?>';
}
/**
* Compile the end-cannot statements into valid PHP.
*
* @return string
*/
protected function compileEndcannot()
{
return '<?php endif; ?>';
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Illuminate\View\Compilers\Concerns;
trait CompilesComments
{
/**
* Compile Blade comments into an empty string.
*
* @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);
}
}

View File

@@ -0,0 +1,48 @@
<?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.
*
* @return string
*/
protected function compileEndComponent()
{
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.
*
* @return string
*/
protected function compileEndSlot()
{
return '<?php $__env->endSlot(); ?>';
}
}

View File

@@ -0,0 +1,204 @@
<?php
namespace Illuminate\View\Compilers\Concerns;
trait CompilesConditionals
{
/**
* Identifier for the first case in switch statement.
*
* @var bool
*/
protected $firstCaseInSwitch = true;
/*
* Compile the if-auth statements into valid PHP.
*
* @param string|null $guard
* @return string
*/
protected function compileAuth($guard = null)
{
$guard = is_null($guard) ? '()' : $guard;
return "<?php if(auth()->guard{$guard}->check()): ?>";
}
/**
* Compile the end-auth statements into valid PHP.
*
* @return string
*/
protected function compileEndAuth()
{
return '<?php endif; ?>';
}
/**
* Compile the if-guest statements into valid PHP.
*
* @param string|null $guard
* @return string
*/
protected function compileGuest($guard = null)
{
$guard = is_null($guard) ? '()' : $guard;
return "<?php if(auth()->guard{$guard}->guest()): ?>";
}
/**
* Compile the end-guest statements into valid PHP.
*
* @return string
*/
protected function compileEndGuest()
{
return '<?php endif; ?>';
}
/**
* 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.
*
* @return string
*/
protected function compileElse()
{
return '<?php else: ?>';
}
/**
* Compile the end-if statements into valid PHP.
*
* @return string
*/
protected function compileEndif()
{
return '<?php endif; ?>';
}
/**
* Compile the end-unless statements into valid PHP.
*
* @return string
*/
protected function compileEndunless()
{
return '<?php endif; ?>';
}
/**
* Compile the if-isset statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileIsset($expression)
{
return "<?php if(isset{$expression}): ?>";
}
/**
* Compile the end-isset statements into valid PHP.
*
* @return string
*/
protected function compileEndIsset()
{
return '<?php endif; ?>';
}
/**
* Compile the switch statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileSwitch($expression)
{
$this->firstCaseInSwitch = true;
return "<?php switch{$expression}:";
}
/**
* Compile the case statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileCase($expression)
{
if ($this->firstCaseInSwitch) {
$this->firstCaseInSwitch = false;
return "case {$expression}: ?>";
}
return "<?php case {$expression}: ?>";
}
/**
* Compile the default statements in switch case into valid PHP.
*
* @return string
*/
protected function compileDefault()
{
return '<?php default: ?>';
}
/**
* Compile the end switch statements into valid PHP.
*
* @return string
*/
protected function compileEndSwitch()
{
return '<?php endswitch; ?>';
}
}

View File

@@ -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+)(.+?)$/si', 'isset($1) ? $1 : $2', $value);
}
}

View File

@@ -0,0 +1,69 @@
<?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-if 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(); ?>";
}
/**
* Compile the include-when statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileIncludeWhen($expression)
{
$expression = $this->stripParentheses($expression);
return "<?php echo \$__env->renderWhen($expression, array_except(get_defined_vars(), array('__data', '__path'))); ?>";
}
/**
* Compile the include-first statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileIncludeFirst($expression)
{
$expression = $this->stripParentheses($expression);
return "<?php echo \$__env->first({$expression}, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
}
}

View File

@@ -0,0 +1,23 @@
<?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));
$variable = trim($segments[0]);
$service = trim($segments[1]);
return "<?php \${$variable} = app('{$service}'); ?>";
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Illuminate\View\Compilers\Concerns;
trait CompilesJson
{
/**
* The default JSON encoding options.
*
* @var int
*/
private $encodingOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
/**
* Compile the JSON statement into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileJson($expression)
{
$parts = explode(',', $this->stripParentheses($expression));
$options = isset($parts[1]) ? trim($parts[1]) : $this->encodingOptions;
$depth = isset($parts[2]) ? trim($parts[2]) : 512;
return "<?php echo json_encode($parts[0], $options, $depth) ?>";
}
}

View File

@@ -0,0 +1,116 @@
<?php
namespace Illuminate\View\Compilers\Concerns;
use Illuminate\View\Factory as ViewFactory;
trait CompilesLayouts
{
/**
* The name of the last section that was started.
*
* @var string
*/
protected $lastSection;
/**
* Compile the extends statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileExtends($expression)
{
$expression = $this->stripParentheses($expression);
$echo = "<?php echo \$__env->make({$expression}, array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
$this->footer[] = $echo;
return '';
}
/**
* Compile the section statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileSection($expression)
{
$this->lastSection = trim($expression, "()'\" ");
return "<?php \$__env->startSection{$expression}; ?>";
}
/**
* Replace the @parent directive to a placeholder.
*
* @return string
*/
protected function compileParent()
{
return ViewFactory::parentPlaceholder($this->lastSection ?: '');
}
/**
* Compile the yield statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileYield($expression)
{
return "<?php echo \$__env->yieldContent{$expression}; ?>";
}
/**
* Compile the show statements into valid PHP.
*
* @return string
*/
protected function compileShow()
{
return '<?php echo $__env->yieldSection(); ?>';
}
/**
* Compile the append statements into valid PHP.
*
* @return string
*/
protected function compileAppend()
{
return '<?php $__env->appendSection(); ?>';
}
/**
* Compile the overwrite statements into valid PHP.
*
* @return string
*/
protected function compileOverwrite()
{
return '<?php $__env->stopSection(true); ?>';
}
/**
* Compile the stop statements into valid PHP.
*
* @return string
*/
protected function compileStop()
{
return '<?php $__env->stopSection(); ?>';
}
/**
* Compile the end-section statements into valid PHP.
*
* @return string
*/
protected function compileEndsection()
{
return '<?php $__env->stopSection(); ?>';
}
}

View File

@@ -0,0 +1,180 @@
<?php
namespace Illuminate\View\Compilers\Concerns;
trait CompilesLoops
{
/**
* Counter to keep track of nested forelse statements.
*
* @var int
*/
protected $forElseCounter = 0;
/**
* Compile the for-else statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileForelse($expression)
{
$empty = '$__empty_'.++$this->forElseCounter;
preg_match('/\( *(.*) +as *(.*)\)$/is', $expression, $matches);
$iteratee = trim($matches[1]);
$iteration = trim($matches[2]);
$initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";
$iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';
return "<?php {$empty} = true; {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} {$empty} = false; ?>";
}
/**
* Compile the for-else-empty and empty statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEmpty($expression)
{
if ($expression) {
return "<?php if(empty{$expression}): ?>";
}
$empty = '$__empty_'.$this->forElseCounter--;
return "<?php endforeach; \$__env->popLoop(); \$loop = \$__env->getLastLoop(); if ({$empty}): ?>";
}
/**
* Compile the end-for-else statements into valid PHP.
*
* @return string
*/
protected function compileEndforelse()
{
return '<?php endif; ?>';
}
/**
* Compile the end-empty statements into valid PHP.
*
* @return string
*/
protected function compileEndEmpty()
{
return '<?php endif; ?>';
}
/**
* Compile the for statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileFor($expression)
{
return "<?php for{$expression}: ?>";
}
/**
* Compile the for-each statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileForeach($expression)
{
preg_match('/\( *(.*) +as *(.*)\)$/is', $expression, $matches);
$iteratee = trim($matches[1]);
$iteration = trim($matches[2]);
$initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";
$iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';
return "<?php {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} ?>";
}
/**
* Compile the break statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileBreak($expression)
{
if ($expression) {
preg_match('/\(\s*(-?\d+)\s*\)$/', $expression, $matches);
return $matches ? '<?php break '.max(1, $matches[1]).'; ?>' : "<?php if{$expression} break; ?>";
}
return '<?php break; ?>';
}
/**
* Compile the continue statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileContinue($expression)
{
if ($expression) {
preg_match('/\(\s*(-?\d+)\s*\)$/', $expression, $matches);
return $matches ? '<?php continue '.max(1, $matches[1]).'; ?>' : "<?php if{$expression} continue; ?>";
}
return '<?php continue; ?>';
}
/**
* Compile the end-for statements into valid PHP.
*
* @return string
*/
protected function compileEndfor()
{
return '<?php endfor; ?>';
}
/**
* Compile the end-for-each statements into valid PHP.
*
* @return string
*/
protected function compileEndforeach()
{
return '<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>';
}
/**
* Compile the while statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileWhile($expression)
{
return "<?php while{$expression}: ?>";
}
/**
* Compile the end-while statements into valid PHP.
*
* @return string
*/
protected function compileEndwhile()
{
return '<?php endwhile; ?>';
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Illuminate\View\Compilers\Concerns;
trait CompilesRawPhp
{
/**
* Compile the raw PHP statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compilePhp($expression)
{
if ($expression) {
return "<?php {$expression}; ?>";
}
return '@php';
}
/**
* Compile the unset statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileUnset($expression)
{
return "<?php unset{$expression}; ?>";
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Illuminate\View\Compilers\Concerns;
trait CompilesStacks
{
/**
* Compile the stack statements into the content.
*
* @param string $expression
* @return string
*/
protected function compileStack($expression)
{
return "<?php echo \$__env->yieldPushContent{$expression}; ?>";
}
/**
* Compile the push statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compilePush($expression)
{
return "<?php \$__env->startPush{$expression}; ?>";
}
/**
* Compile the end-push statements into valid PHP.
*
* @return string
*/
protected function compileEndpush()
{
return '<?php $__env->stopPush(); ?>';
}
/**
* Compile the prepend statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compilePrepend($expression)
{
return "<?php \$__env->startPrepend{$expression}; ?>";
}
/**
* Compile the end-prepend statements into valid PHP.
*
* @return string
*/
protected function compileEndprepend()
{
return '<?php $__env->stopPrepend(); ?>';
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Illuminate\View\Compilers\Concerns;
trait CompilesTranslations
{
/**
* Compile the lang statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileLang($expression)
{
if (is_null($expression)) {
return '<?php $__env->startTranslation(); ?>';
} elseif ($expression[1] === '[') {
return "<?php \$__env->startTranslation{$expression}; ?>";
}
return "<?php echo app('translator')->getFromJson{$expression}; ?>";
}
/**
* Compile the end-lang statements into valid PHP.
*
* @return string
*/
protected function compileEndlang()
{
return '<?php echo $__env->renderTranslation(); ?>';
}
/**
* Compile the choice statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileChoice($expression)
{
return "<?php echo app('translator')->choice{$expression}; ?>";
}
}

View File

@@ -0,0 +1,128 @@
<?php
namespace Illuminate\View\Concerns;
use Illuminate\Support\HtmlString;
trait ManagesComponents
{
/**
* The components being rendered.
*
* @var array
*/
protected $componentStack = [];
/**
* The original data passed to the component.
*
* @var array
*/
protected $componentData = [];
/**
* The slot contents for the component.
*
* @var array
*/
protected $slots = [];
/**
* The names of the slots being rendered.
*
* @var array
*/
protected $slotStack = [];
/**
* Start a component rendering process.
*
* @param string $name
* @param array $data
* @return void
*/
public function startComponent($name, array $data = [])
{
if (ob_start()) {
$this->componentStack[] = $name;
$this->componentData[$this->currentComponent()] = $data;
$this->slots[$this->currentComponent()] = [];
}
}
/**
* Render the current component.
*
* @return string
*/
public function renderComponent()
{
$name = array_pop($this->componentStack);
return $this->make($name, $this->componentData($name))->render();
}
/**
* Get the data for the given component.
*
* @param string $name
* @return array
*/
protected function componentData($name)
{
return array_merge(
$this->componentData[count($this->componentStack)],
['slot' => new HtmlString(trim(ob_get_clean()))],
$this->slots[count($this->componentStack)]
);
}
/**
* Start the slot rendering process.
*
* @param string $name
* @param string|null $content
* @return void
*/
public function slot($name, $content = null)
{
if (count(func_get_args()) == 2) {
$this->slots[$this->currentComponent()][$name] = $content;
} else {
if (ob_start()) {
$this->slots[$this->currentComponent()][$name] = '';
$this->slotStack[$this->currentComponent()][] = $name;
}
}
}
/**
* Save the slot content for rendering.
*
* @return void
*/
public function endSlot()
{
last($this->componentStack);
$currentSlot = array_pop(
$this->slotStack[$this->currentComponent()]
);
$this->slots[$this->currentComponent()]
[$currentSlot] = new HtmlString(trim(ob_get_clean()));
}
/**
* Get the index for the current component.
*
* @return int
*/
protected function currentComponent()
{
return count($this->componentStack) - 1;
}
}

View File

@@ -0,0 +1,192 @@
<?php
namespace Illuminate\View\Concerns;
use Closure;
use Illuminate\Support\Str;
use Illuminate\Contracts\View\View as ViewContract;
trait ManagesEvents
{
/**
* Register a view creator event.
*
* @param array|string $views
* @param \Closure|string $callback
* @return array
*/
public function creator($views, $callback)
{
$creators = [];
foreach ((array) $views as $view) {
$creators[] = $this->addViewEvent($view, $callback, 'creating: ');
}
return $creators;
}
/**
* Register multiple view composers via an array.
*
* @param array $composers
* @return array
*/
public function composers(array $composers)
{
$registered = [];
foreach ($composers as $callback => $views) {
$registered = array_merge($registered, $this->composer($views, $callback));
}
return $registered;
}
/**
* Register a view composer event.
*
* @param array|string $views
* @param \Closure|string $callback
* @return array
*/
public function composer($views, $callback)
{
$composers = [];
foreach ((array) $views as $view) {
$composers[] = $this->addViewEvent($view, $callback, 'composing: ');
}
return $composers;
}
/**
* Add an event for a given view.
*
* @param string $view
* @param \Closure|string $callback
* @param string $prefix
* @return \Closure|null
*/
protected function addViewEvent($view, $callback, $prefix = 'composing: ')
{
$view = $this->normalizeName($view);
if ($callback instanceof Closure) {
$this->addEventListener($prefix.$view, $callback);
return $callback;
} elseif (is_string($callback)) {
return $this->addClassEvent($view, $callback, $prefix);
}
}
/**
* Register a class based view composer.
*
* @param string $view
* @param string $class
* @param string $prefix
* @return \Closure
*/
protected function addClassEvent($view, $class, $prefix)
{
$name = $prefix.$view;
// When registering a class based view "composer", we will simply resolve the
// classes from the application IoC container then call the compose method
// on the instance. This allows for convenient, testable view composers.
$callback = $this->buildClassEventCallback(
$class, $prefix
);
$this->addEventListener($name, $callback);
return $callback;
}
/**
* Build a class based container callback Closure.
*
* @param string $class
* @param string $prefix
* @return \Closure
*/
protected function buildClassEventCallback($class, $prefix)
{
list($class, $method) = $this->parseClassEvent($class, $prefix);
// Once we have the class and method name, we can build the Closure to resolve
// the instance out of the IoC container and call the method on it with the
// given arguments that are passed to the Closure as the composer's data.
return function () use ($class, $method) {
return call_user_func_array(
[$this->container->make($class), $method], func_get_args()
);
};
}
/**
* Parse a class based composer name.
*
* @param string $class
* @param string $prefix
* @return array
*/
protected function parseClassEvent($class, $prefix)
{
return Str::parseCallback($class, $this->classEventMethodForPrefix($prefix));
}
/**
* Determine the class event method based on the given prefix.
*
* @param string $prefix
* @return string
*/
protected function classEventMethodForPrefix($prefix)
{
return Str::contains($prefix, 'composing') ? 'compose' : 'create';
}
/**
* Add a listener to the event dispatcher.
*
* @param string $name
* @param \Closure $callback
* @return void
*/
protected function addEventListener($name, $callback)
{
if (Str::contains($name, '*')) {
$callback = function ($name, array $data) use ($callback) {
return $callback($data[0]);
};
}
$this->events->listen($name, $callback);
}
/**
* Call the composer for a given view.
*
* @param \Illuminate\Contracts\View\View $view
* @return void
*/
public function callComposer(ViewContract $view)
{
$this->events->dispatch('composing: '.$view->name(), [$view]);
}
/**
* Call the creator for a given view.
*
* @param \Illuminate\Contracts\View\View $view
* @return void
*/
public function callCreator(ViewContract $view)
{
$this->events->dispatch('creating: '.$view->name(), [$view]);
}
}

View File

@@ -0,0 +1,218 @@
<?php
namespace Illuminate\View\Concerns;
use InvalidArgumentException;
use Illuminate\Contracts\View\View;
trait ManagesLayouts
{
/**
* All of the finished, captured sections.
*
* @var array
*/
protected $sections = [];
/**
* The stack of in-progress sections.
*
* @var array
*/
protected $sectionStack = [];
/**
* The parent placeholder for the request.
*
* @var mixed
*/
protected static $parentPlaceholder = [];
/**
* Start injecting content into a section.
*
* @param string $section
* @param string|null $content
* @return void
*/
public function startSection($section, $content = null)
{
if ($content === null) {
if (ob_start()) {
$this->sectionStack[] = $section;
}
} else {
$this->extendSection($section, $content instanceof View ? $content : e($content));
}
}
/**
* Inject inline content into a section.
*
* @param string $section
* @param string $content
* @return void
*/
public function inject($section, $content)
{
$this->startSection($section, $content);
}
/**
* Stop injecting content into a section and return its contents.
*
* @return string
*/
public function yieldSection()
{
if (empty($this->sectionStack)) {
return '';
}
return $this->yieldContent($this->stopSection());
}
/**
* Stop injecting content into a section.
*
* @param bool $overwrite
* @return string
* @throws \InvalidArgumentException
*/
public function stopSection($overwrite = false)
{
if (empty($this->sectionStack)) {
throw new InvalidArgumentException('Cannot end a section without first starting one.');
}
$last = array_pop($this->sectionStack);
if ($overwrite) {
$this->sections[$last] = ob_get_clean();
} else {
$this->extendSection($last, ob_get_clean());
}
return $last;
}
/**
* Stop injecting content into a section and append it.
*
* @return string
* @throws \InvalidArgumentException
*/
public function appendSection()
{
if (empty($this->sectionStack)) {
throw new InvalidArgumentException('Cannot end a section without first starting one.');
}
$last = array_pop($this->sectionStack);
if (isset($this->sections[$last])) {
$this->sections[$last] .= ob_get_clean();
} else {
$this->sections[$last] = ob_get_clean();
}
return $last;
}
/**
* Append content to a given section.
*
* @param string $section
* @param string $content
* @return void
*/
protected function extendSection($section, $content)
{
if (isset($this->sections[$section])) {
$content = str_replace(static::parentPlaceholder($section), $content, $this->sections[$section]);
}
$this->sections[$section] = $content;
}
/**
* Get the string contents of a section.
*
* @param string $section
* @param string $default
* @return string
*/
public function yieldContent($section, $default = '')
{
$sectionContent = $default instanceof View ? $default : e($default);
if (isset($this->sections[$section])) {
$sectionContent = $this->sections[$section];
}
$sectionContent = str_replace('@@parent', '--parent--holder--', $sectionContent);
return str_replace(
'--parent--holder--', '@parent', str_replace(static::parentPlaceholder($section), '', $sectionContent)
);
}
/**
* Get the parent placeholder for the current request.
*
* @param string $section
* @return string
*/
public static function parentPlaceholder($section = '')
{
if (! isset(static::$parentPlaceholder[$section])) {
static::$parentPlaceholder[$section] = '##parent-placeholder-'.sha1($section).'##';
}
return static::$parentPlaceholder[$section];
}
/**
* Check if section exists.
*
* @param string $name
* @return bool
*/
public function hasSection($name)
{
return array_key_exists($name, $this->sections);
}
/**
* Get the contents of a section.
*
* @param string $name
* @param string $default
* @return mixed
*/
public function getSection($name, $default = null)
{
return $this->getSections()[$name] ?? $default;
}
/**
* Get the entire array of sections.
*
* @return array
*/
public function getSections()
{
return $this->sections;
}
/**
* Flush all of the sections.
*
* @return void
*/
public function flushSections()
{
$this->sections = [];
$this->sectionStack = [];
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace Illuminate\View\Concerns;
use Countable;
use Illuminate\Support\Arr;
trait ManagesLoops
{
/**
* The stack of in-progress loops.
*
* @var array
*/
protected $loopsStack = [];
/**
* Add new loop to the stack.
*
* @param \Countable|array $data
* @return void
*/
public function addLoop($data)
{
$length = is_array($data) || $data instanceof Countable ? count($data) : null;
$parent = Arr::last($this->loopsStack);
$this->loopsStack[] = [
'iteration' => 0,
'index' => 0,
'remaining' => $length ?? null,
'count' => $length,
'first' => true,
'last' => isset($length) ? $length == 1 : null,
'depth' => count($this->loopsStack) + 1,
'parent' => $parent ? (object) $parent : null,
];
}
/**
* Increment the top loop's indices.
*
* @return void
*/
public function incrementLoopIndices()
{
$loop = $this->loopsStack[$index = count($this->loopsStack) - 1];
$this->loopsStack[$index] = array_merge($this->loopsStack[$index], [
'iteration' => $loop['iteration'] + 1,
'index' => $loop['iteration'],
'first' => $loop['iteration'] == 0,
'remaining' => isset($loop['count']) ? $loop['remaining'] - 1 : null,
'last' => isset($loop['count']) ? $loop['iteration'] == $loop['count'] - 1 : null,
]);
}
/**
* Pop a loop from the top of the loop stack.
*
* @return void
*/
public function popLoop()
{
array_pop($this->loopsStack);
}
/**
* Get an instance of the last loop in the stack.
*
* @return \stdClass|null
*/
public function getLastLoop()
{
if ($last = Arr::last($this->loopsStack)) {
return (object) $last;
}
}
/**
* Get the entire loop stack.
*
* @return array
*/
public function getLoopStack()
{
return $this->loopsStack;
}
}

View File

@@ -0,0 +1,177 @@
<?php
namespace Illuminate\View\Concerns;
use InvalidArgumentException;
trait ManagesStacks
{
/**
* All of the finished, captured push sections.
*
* @var array
*/
protected $pushes = [];
/**
* All of the finished, captured prepend sections.
*
* @var array
*/
protected $prepends = [];
/**
* The stack of in-progress push sections.
*
* @var array
*/
protected $pushStack = [];
/**
* Start injecting content into a push section.
*
* @param string $section
* @param string $content
* @return void
*/
public function startPush($section, $content = '')
{
if ($content === '') {
if (ob_start()) {
$this->pushStack[] = $section;
}
} else {
$this->extendPush($section, $content);
}
}
/**
* Stop injecting content into a push section.
*
* @return string
* @throws \InvalidArgumentException
*/
public function stopPush()
{
if (empty($this->pushStack)) {
throw new InvalidArgumentException('Cannot end a push stack without first starting one.');
}
return tap(array_pop($this->pushStack), function ($last) {
$this->extendPush($last, ob_get_clean());
});
}
/**
* Append content to a given push section.
*
* @param string $section
* @param string $content
* @return void
*/
protected function extendPush($section, $content)
{
if (! isset($this->pushes[$section])) {
$this->pushes[$section] = [];
}
if (! isset($this->pushes[$section][$this->renderCount])) {
$this->pushes[$section][$this->renderCount] = $content;
} else {
$this->pushes[$section][$this->renderCount] .= $content;
}
}
/**
* Start prepending content into a push section.
*
* @param string $section
* @param string $content
* @return void
*/
public function startPrepend($section, $content = '')
{
if ($content === '') {
if (ob_start()) {
$this->pushStack[] = $section;
}
} else {
$this->extendPrepend($section, $content);
}
}
/**
* Stop prepending content into a push section.
*
* @return string
* @throws \InvalidArgumentException
*/
public function stopPrepend()
{
if (empty($this->pushStack)) {
throw new InvalidArgumentException('Cannot end a prepend operation without first starting one.');
}
return tap(array_pop($this->pushStack), function ($last) {
$this->extendPrepend($last, ob_get_clean());
});
}
/**
* Prepend content to a given stack.
*
* @param string $section
* @param string $content
* @return void
*/
protected function extendPrepend($section, $content)
{
if (! isset($this->prepends[$section])) {
$this->prepends[$section] = [];
}
if (! isset($this->prepends[$section][$this->renderCount])) {
$this->prepends[$section][$this->renderCount] = $content;
} else {
$this->prepends[$section][$this->renderCount] = $content.$this->prepends[$section][$this->renderCount];
}
}
/**
* Get the string contents of a push section.
*
* @param string $section
* @param string $default
* @return string
*/
public function yieldPushContent($section, $default = '')
{
if (! isset($this->pushes[$section]) && ! isset($this->prepends[$section])) {
return $default;
}
$output = '';
if (isset($this->prepends[$section])) {
$output .= implode(array_reverse($this->prepends[$section]));
}
if (isset($this->pushes[$section])) {
$output .= implode($this->pushes[$section]);
}
return $output;
}
/**
* Flush all of the stacks.
*
* @return void
*/
public function flushStacks()
{
$this->pushes = [];
$this->prepends = [];
$this->pushStack = [];
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Illuminate\View\Concerns;
trait ManagesTranslations
{
/**
* The translation replacements for the translation being rendered.
*
* @var array
*/
protected $translationReplacements = [];
/**
* Start a translation block.
*
* @param array $replacements
* @return void
*/
public function startTranslation($replacements = [])
{
ob_start();
$this->translationReplacements = $replacements;
}
/**
* Render the current translation.
*
* @return string
*/
public function renderTranslation()
{
return $this->container->make('translator')->getFromJson(
trim(ob_get_clean()), $this->translationReplacements
);
}
}

View File

@@ -70,7 +70,7 @@ class CompilerEngine extends PhpEngine
* @param int $obLevel
* @return void
*
* @throws $e
* @throws \Exception
*/
protected function handleViewException(Exception $e, $obLevel)
{

View File

@@ -38,10 +38,10 @@ class EngineResolver
}
/**
* Resolver an engine instance by name.
* Resolve an engine instance by name.
*
* @param string $engine
* @return \Illuminate\View\Engines\EngineInterface
* @return \Illuminate\Contracts\View\Engine
* @throws \InvalidArgumentException
*/
public function resolve($engine)

View File

@@ -2,7 +2,9 @@
namespace Illuminate\View\Engines;
interface EngineInterface
use Illuminate\Contracts\View\Engine;
class FileEngine implements Engine
{
/**
* Get the evaluated contents of the view.
@@ -11,5 +13,8 @@ interface EngineInterface
* @param array $data
* @return string
*/
public function get($path, array $data = []);
public function get($path, array $data = [])
{
return file_get_contents($path);
}
}

View File

@@ -4,9 +4,10 @@ namespace Illuminate\View\Engines;
use Exception;
use Throwable;
use Illuminate\Contracts\View\Engine;
use Symfony\Component\Debug\Exception\FatalThrowableError;
class PhpEngine implements EngineInterface
class PhpEngine implements Engine
{
/**
* Get the evaluated contents of the view.
@@ -56,7 +57,7 @@ class PhpEngine implements EngineInterface
* @param int $obLevel
* @return void
*
* @throws $e
* @throws \Exception
*/
protected function handleViewException(Exception $e, $obLevel)
{

View File

@@ -1,12 +0,0 @@
<?php
namespace Illuminate\View;
use Illuminate\Support\HtmlString;
/**
* @deprecated since version 5.2. Use Illuminate\Support\HtmlString.
*/
class Expression extends HtmlString
{
}

View File

@@ -2,18 +2,24 @@
namespace Illuminate\View;
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\View\Factory as FactoryContract;
class Factory implements FactoryContract
{
use Concerns\ManagesComponents,
Concerns\ManagesEvents,
Concerns\ManagesLayouts,
Concerns\ManagesLoops,
Concerns\ManagesStacks,
Concerns\ManagesTranslations;
/**
* The engine implementation.
*
@@ -49,26 +55,16 @@ class Factory implements FactoryContract
*/
protected $shared = [];
/**
* Array of registered view name aliases.
*
* @var array
*/
protected $aliases = [];
/**
* All of the registered view names.
*
* @var array
*/
protected $names = [];
/**
* The extension to engine bindings.
*
* @var array
*/
protected $extensions = ['blade.php' => 'blade', 'php' => 'php'];
protected $extensions = [
'blade.php' => 'blade',
'php' => 'php',
'css' => 'file',
];
/**
* The view composer events.
@@ -77,34 +73,6 @@ class Factory implements FactoryContract
*/
protected $composers = [];
/**
* All of the finished, captured sections.
*
* @var array
*/
protected $sections = [];
/**
* The stack of in-progress sections.
*
* @var array
*/
protected $sectionStack = [];
/**
* All of the finished, captured push sections.
*
* @var array
*/
protected $pushes = [];
/**
* The stack of in-progress push sections.
*
* @var array
*/
protected $pushStack = [];
/**
* The number of active rendering operations.
*
@@ -141,9 +109,9 @@ class Factory implements FactoryContract
{
$data = array_merge($mergeData, $this->parseData($data));
$this->callCreator($view = new View($this, $this->getEngineFromPath($path), $path, $path, $data));
return $view;
return tap($this->viewInstance($path, $path, $data), function ($view) {
$this->callCreator($view);
});
}
/**
@@ -156,102 +124,57 @@ class Factory implements FactoryContract
*/
public function make($view, $data = [], $mergeData = [])
{
if (isset($this->aliases[$view])) {
$view = $this->aliases[$view];
}
$view = $this->normalizeName($view);
$path = $this->finder->find($view);
$path = $this->finder->find(
$view = $this->normalizeName($view)
);
// Next, we will create the view instance and call the view creator for the view
// which can set any data, etc. Then we will return the view instance back to
// the caller for rendering or performing other view manipulations on this.
$data = array_merge($mergeData, $this->parseData($data));
$this->callCreator($view = new View($this, $this->getEngineFromPath($path), $view, $path, $data));
return $view;
return tap($this->viewInstance($view, $path, $data), function ($view) {
$this->callCreator($view);
});
}
/**
* Normalize a view name.
* Get the first view that actually exists from the given list.
*
* @param string $name
* @return string
*/
protected function normalizeName($name)
{
$delimiter = ViewFinderInterface::HINT_PATH_DELIMITER;
if (strpos($name, $delimiter) === false) {
return str_replace('/', '.', $name);
}
list($namespace, $name) = explode($delimiter, $name);
return $namespace.$delimiter.str_replace('/', '.', $name);
}
/**
* Parse the given data into a raw array.
*
* @param mixed $data
* @return array
*/
protected function parseData($data)
{
return $data instanceof Arrayable ? $data->toArray() : $data;
}
/**
* Get the evaluated view contents for a named view.
*
* @param string $view
* @param mixed $data
* @param array $views
* @param array $data
* @param array $mergeData
* @return \Illuminate\Contracts\View\View
*/
public function of($view, $data = [])
public function first(array $views, $data = [], $mergeData = [])
{
return $this->make($this->names[$view], $data);
}
$view = collect($views)->first(function ($view) {
return $this->exists($view);
});
/**
* Register a named view.
*
* @param string $view
* @param string $name
* @return void
*/
public function name($view, $name)
{
$this->names[$name] = $view;
}
/**
* Add an alias for a view.
*
* @param string $view
* @param string $alias
* @return void
*/
public function alias($view, $alias)
{
$this->aliases[$alias] = $view;
}
/**
* Determine if a given view exists.
*
* @param string $view
* @return bool
*/
public function exists($view)
{
try {
$this->finder->find($view);
} catch (InvalidArgumentException $e) {
return false;
if (! $view) {
throw new InvalidArgumentException('None of the views in the given array exist.');
}
return true;
return $this->make($view, $data, $mergeData);
}
/**
* Get the rendered content of the view based on a given condition.
*
* @param bool $condition
* @param string $view
* @param array $data
* @param array $mergeData
* @return string
*/
public function renderWhen($condition, $view, $data = [], $mergeData = [])
{
if (! $condition) {
return '';
}
return $this->make($view, $this->parseData($data), $mergeData)->render();
}
/**
@@ -272,9 +195,9 @@ class Factory implements FactoryContract
// iterated value of this data array, allowing the views to access them.
if (count($data) > 0) {
foreach ($data as $key => $value) {
$data = ['key' => $key, $iterator => $value];
$result .= $this->make($view, $data)->render();
$result .= $this->make(
$view, ['key' => $key, $iterator => $value]
)->render();
}
}
@@ -282,21 +205,71 @@ class Factory implements FactoryContract
// view. Alternatively, the "empty view" could be a raw string that begins
// with "raw|" for convenience and to let this know that it is a string.
else {
if (Str::startsWith($empty, 'raw|')) {
$result = substr($empty, 4);
} else {
$result = $this->make($empty)->render();
}
$result = Str::startsWith($empty, 'raw|')
? substr($empty, 4)
: $this->make($empty)->render();
}
return $result;
}
/**
* Normalize a view name.
*
* @param string $name
* @return string
*/
protected function normalizeName($name)
{
return ViewName::normalize($name);
}
/**
* Parse the given data into a raw array.
*
* @param mixed $data
* @return array
*/
protected function parseData($data)
{
return $data instanceof Arrayable ? $data->toArray() : $data;
}
/**
* Create a new view instance from the given arguments.
*
* @param string $view
* @param string $path
* @param array $data
* @return \Illuminate\Contracts\View\View
*/
protected function viewInstance($view, $path, $data)
{
return new View($this, $this->getEngineFromPath($path), $view, $path, $data);
}
/**
* Determine if a given view exists.
*
* @param string $view
* @return bool
*/
public function exists($view)
{
try {
$this->finder->find($view);
} catch (InvalidArgumentException $e) {
return false;
}
return true;
}
/**
* Get the appropriate view engine for the given path.
*
* @param string $path
* @return \Illuminate\View\Engines\EngineInterface
* @return \Illuminate\Contracts\View\Engine
*
* @throws \InvalidArgumentException
*/
@@ -321,7 +294,7 @@ class Factory implements FactoryContract
{
$extensions = array_keys($this->extensions);
return Arr::first($extensions, function ($key, $value) use ($path) {
return Arr::first($extensions, function ($value) use ($path) {
return Str::endsWith($path, '.'.$value);
});
}
@@ -335,419 +308,13 @@ class Factory implements FactoryContract
*/
public function share($key, $value = null)
{
if (! is_array($key)) {
return $this->shared[$key] = $value;
$keys = is_array($key) ? $key : [$key => $value];
foreach ($keys as $key => $value) {
$this->shared[$key] = $value;
}
foreach ($key as $innerKey => $innerValue) {
$this->share($innerKey, $innerValue);
}
}
/**
* Register a view creator event.
*
* @param array|string $views
* @param \Closure|string $callback
* @return array
*/
public function creator($views, $callback)
{
$creators = [];
foreach ((array) $views as $view) {
$creators[] = $this->addViewEvent($view, $callback, 'creating: ');
}
return $creators;
}
/**
* Register multiple view composers via an array.
*
* @param array $composers
* @return array
*/
public function composers(array $composers)
{
$registered = [];
foreach ($composers as $callback => $views) {
$registered = array_merge($registered, $this->composer($views, $callback));
}
return $registered;
}
/**
* Register a view composer event.
*
* @param array|string $views
* @param \Closure|string $callback
* @param int|null $priority
* @return array
*/
public function composer($views, $callback, $priority = null)
{
$composers = [];
foreach ((array) $views as $view) {
$composers[] = $this->addViewEvent($view, $callback, 'composing: ', $priority);
}
return $composers;
}
/**
* Add an event for a given view.
*
* @param string $view
* @param \Closure|string $callback
* @param string $prefix
* @param int|null $priority
* @return \Closure|null
*/
protected function addViewEvent($view, $callback, $prefix = 'composing: ', $priority = null)
{
$view = $this->normalizeName($view);
if ($callback instanceof Closure) {
$this->addEventListener($prefix.$view, $callback, $priority);
return $callback;
} elseif (is_string($callback)) {
return $this->addClassEvent($view, $callback, $prefix, $priority);
}
}
/**
* Register a class based view composer.
*
* @param string $view
* @param string $class
* @param string $prefix
* @param int|null $priority
* @return \Closure
*/
protected function addClassEvent($view, $class, $prefix, $priority = null)
{
$name = $prefix.$view;
// When registering a class based view "composer", we will simply resolve the
// classes from the application IoC container then call the compose method
// on the instance. This allows for convenient, testable view composers.
$callback = $this->buildClassEventCallback($class, $prefix);
$this->addEventListener($name, $callback, $priority);
return $callback;
}
/**
* Add a listener to the event dispatcher.
*
* @param string $name
* @param \Closure $callback
* @param int|null $priority
* @return void
*/
protected function addEventListener($name, $callback, $priority = null)
{
if (is_null($priority)) {
$this->events->listen($name, $callback);
} else {
$this->events->listen($name, $callback, $priority);
}
}
/**
* Build a class based container callback Closure.
*
* @param string $class
* @param string $prefix
* @return \Closure
*/
protected function buildClassEventCallback($class, $prefix)
{
list($class, $method) = $this->parseClassEvent($class, $prefix);
// Once we have the class and method name, we can build the Closure to resolve
// the instance out of the IoC container and call the method on it with the
// given arguments that are passed to the Closure as the composer's data.
return function () use ($class, $method) {
$callable = [$this->container->make($class), $method];
return call_user_func_array($callable, func_get_args());
};
}
/**
* Parse a class based composer name.
*
* @param string $class
* @param string $prefix
* @return array
*/
protected function parseClassEvent($class, $prefix)
{
if (Str::contains($class, '@')) {
return explode('@', $class);
}
$method = Str::contains($prefix, 'composing') ? 'compose' : 'create';
return [$class, $method];
}
/**
* Call the composer for a given view.
*
* @param \Illuminate\Contracts\View\View $view
* @return void
*/
public function callComposer(View $view)
{
$this->events->fire('composing: '.$view->getName(), [$view]);
}
/**
* Call the creator for a given view.
*
* @param \Illuminate\Contracts\View\View $view
* @return void
*/
public function callCreator(View $view)
{
$this->events->fire('creating: '.$view->getName(), [$view]);
}
/**
* Start injecting content into a section.
*
* @param string $section
* @param string $content
* @return void
*/
public function startSection($section, $content = '')
{
if ($content === '') {
if (ob_start()) {
$this->sectionStack[] = $section;
}
} else {
$this->extendSection($section, $content);
}
}
/**
* Inject inline content into a section.
*
* @param string $section
* @param string $content
* @return void
*/
public function inject($section, $content)
{
return $this->startSection($section, $content);
}
/**
* Stop injecting content into a section and return its contents.
*
* @return string
*/
public function yieldSection()
{
if (empty($this->sectionStack)) {
return '';
}
return $this->yieldContent($this->stopSection());
}
/**
* Stop injecting content into a section.
*
* @param bool $overwrite
* @return string
* @throws \InvalidArgumentException
*/
public function stopSection($overwrite = false)
{
if (empty($this->sectionStack)) {
throw new InvalidArgumentException('Cannot end a section without first starting one.');
}
$last = array_pop($this->sectionStack);
if ($overwrite) {
$this->sections[$last] = ob_get_clean();
} else {
$this->extendSection($last, ob_get_clean());
}
return $last;
}
/**
* Stop injecting content into a section and append it.
*
* @return string
* @throws \InvalidArgumentException
*/
public function appendSection()
{
if (empty($this->sectionStack)) {
throw new InvalidArgumentException('Cannot end a section without first starting one.');
}
$last = array_pop($this->sectionStack);
if (isset($this->sections[$last])) {
$this->sections[$last] .= ob_get_clean();
} else {
$this->sections[$last] = ob_get_clean();
}
return $last;
}
/**
* Append content to a given section.
*
* @param string $section
* @param string $content
* @return void
*/
protected function extendSection($section, $content)
{
if (isset($this->sections[$section])) {
$content = str_replace('@parent', $content, $this->sections[$section]);
}
$this->sections[$section] = $content;
}
/**
* Get the string contents of a section.
*
* @param string $section
* @param string $default
* @return string
*/
public function yieldContent($section, $default = '')
{
$sectionContent = $default;
if (isset($this->sections[$section])) {
$sectionContent = $this->sections[$section];
}
$sectionContent = str_replace('@@parent', '--parent--holder--', $sectionContent);
return str_replace(
'--parent--holder--', '@parent', str_replace('@parent', '', $sectionContent)
);
}
/**
* Start injecting content into a push section.
*
* @param string $section
* @param string $content
* @return void
*/
public function startPush($section, $content = '')
{
if ($content === '') {
if (ob_start()) {
$this->pushStack[] = $section;
}
} else {
$this->extendPush($section, $content);
}
}
/**
* Stop injecting content into a push section.
*
* @return string
* @throws \InvalidArgumentException
*/
public function stopPush()
{
if (empty($this->pushStack)) {
throw new InvalidArgumentException('Cannot end a section without first starting one.');
}
$last = array_pop($this->pushStack);
$this->extendPush($last, ob_get_clean());
return $last;
}
/**
* Append content to a given push section.
*
* @param string $section
* @param string $content
* @return void
*/
protected function extendPush($section, $content)
{
if (! isset($this->pushes[$section])) {
$this->pushes[$section] = [];
}
if (! isset($this->pushes[$section][$this->renderCount])) {
$this->pushes[$section][$this->renderCount] = $content;
} else {
$this->pushes[$section][$this->renderCount] .= $content;
}
}
/**
* Get the string contents of a push section.
*
* @param string $section
* @param string $default
* @return string
*/
public function yieldPushContent($section, $default = '')
{
if (! isset($this->pushes[$section])) {
return $default;
}
return implode(array_reverse($this->pushes[$section]));
}
/**
* Flush all of the section contents.
*
* @return void
*/
public function flushSections()
{
$this->renderCount = 0;
$this->sections = [];
$this->sectionStack = [];
$this->pushes = [];
$this->pushStack = [];
}
/**
* Flush all of the section contents if done rendering.
*
* @return void
*/
public function flushSectionsIfDoneRendering()
{
if ($this->doneRendering()) {
$this->flushSections();
}
return $value;
}
/**
@@ -796,11 +363,13 @@ class Factory implements FactoryContract
*
* @param string $namespace
* @param string|array $hints
* @return void
* @return $this
*/
public function addNamespace($namespace, $hints)
{
$this->finder->addNamespace($namespace, $hints);
return $this;
}
/**
@@ -808,11 +377,27 @@ class Factory implements FactoryContract
*
* @param string $namespace
* @param string|array $hints
* @return void
* @return $this
*/
public function prependNamespace($namespace, $hints)
{
$this->finder->prependNamespace($namespace, $hints);
return $this;
}
/**
* Replace the namespace hints for the given namespace.
*
* @param string $namespace
* @param string|array $hints
* @return $this
*/
public function replaceNamespace($namespace, $hints)
{
$this->finder->replaceNamespace($namespace, $hints);
return $this;
}
/**
@@ -836,6 +421,31 @@ class Factory implements FactoryContract
$this->extensions = array_merge([$extension => $engine], $this->extensions);
}
/**
* Flush all of the factory state like sections and stacks.
*
* @return void
*/
public function flushState()
{
$this->renderCount = 0;
$this->flushSections();
$this->flushStacks();
}
/**
* Flush all of the section contents if done rendering.
*
* @return void
*/
public function flushStateIfDoneRendering()
{
if ($this->doneRendering()) {
$this->flushState();
}
}
/**
* Get the extension to engine bindings.
*
@@ -877,6 +487,16 @@ class Factory implements FactoryContract
$this->finder = $finder;
}
/**
* Flush the cache of views located by the finder.
*
* @return void
*/
public function flushFinderCache()
{
$this->getFinder()->flush();
}
/**
* Get the event dispatcher instance.
*
@@ -940,35 +560,4 @@ class Factory implements FactoryContract
{
return $this->shared;
}
/**
* Check if section exists.
*
* @param string $name
* @return bool
*/
public function hasSection($name)
{
return array_key_exists($name, $this->sections);
}
/**
* Get the entire array of sections.
*
* @return array
*/
public function getSections()
{
return $this->sections;
}
/**
* Get all of the registered named views in environment.
*
* @return array
*/
public function getNames()
{
return $this->names;
}
}

View File

@@ -40,7 +40,7 @@ class FileViewFinder implements ViewFinderInterface
*
* @var array
*/
protected $extensions = ['blade.php', 'php'];
protected $extensions = ['blade.php', 'php', 'css'];
/**
* Create a new file view loader instance.
@@ -73,7 +73,7 @@ class FileViewFinder implements ViewFinderInterface
}
if ($this->hasHintInformation($name = trim($name))) {
return $this->views[$name] = $this->findNamedPathView($name);
return $this->views[$name] = $this->findNamespacedView($name);
}
return $this->views[$name] = $this->findInPaths($name, $this->paths);
@@ -85,9 +85,9 @@ class FileViewFinder implements ViewFinderInterface
* @param string $name
* @return string
*/
protected function findNamedPathView($name)
protected function findNamespacedView($name)
{
list($namespace, $view) = $this->getNamespaceSegments($name);
list($namespace, $view) = $this->parseNamespaceSegments($name);
return $this->findInPaths($view, $this->hints[$namespace]);
}
@@ -100,7 +100,7 @@ class FileViewFinder implements ViewFinderInterface
*
* @throws \InvalidArgumentException
*/
protected function getNamespaceSegments($name)
protected function parseNamespaceSegments($name)
{
$segments = explode(static::HINT_PATH_DELIMITER, $name);
@@ -161,6 +161,17 @@ class FileViewFinder implements ViewFinderInterface
$this->paths[] = $location;
}
/**
* Prepend a location to the finder.
*
* @param string $location
* @return void
*/
public function prependLocation($location)
{
array_unshift($this->paths, $location);
}
/**
* Add a namespace hint to the finder.
*
@@ -197,6 +208,18 @@ class FileViewFinder implements ViewFinderInterface
$this->hints[$namespace] = $hints;
}
/**
* Replace the namespace hints for the given namespace.
*
* @param string $namespace
* @param string|array $hints
* @return void
*/
public function replaceNamespace($namespace, $hints)
{
$this->hints[$namespace] = (array) $hints;
}
/**
* Register an extension with the view finder.
*
@@ -213,7 +236,7 @@ class FileViewFinder implements ViewFinderInterface
}
/**
* Returns whether or not the view specify a hint information.
* Returns whether or not the view name has any hint information.
*
* @param string $name
* @return bool
@@ -223,6 +246,16 @@ class FileViewFinder implements ViewFinderInterface
return strpos($name, static::HINT_PATH_DELIMITER) > 0;
}
/**
* Flush the cache of located views.
*
* @return void
*/
public function flush()
{
$this->views = [];
}
/**
* Get the filesystem instance.
*

View File

@@ -8,8 +8,8 @@ use ArrayAccess;
use BadMethodCallException;
use Illuminate\Support\Str;
use Illuminate\Support\MessageBag;
use Illuminate\Contracts\View\Engine;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\View\Engines\EngineInterface;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Contracts\Support\MessageProvider;
use Illuminate\Contracts\View\View as ViewContract;
@@ -26,7 +26,7 @@ class View implements ArrayAccess, ViewContract
/**
* The engine implementation.
*
* @var \Illuminate\View\Engines\EngineInterface
* @var \Illuminate\Contracts\View\Engine
*/
protected $engine;
@@ -55,13 +55,13 @@ class View implements ArrayAccess, ViewContract
* Create a new view instance.
*
* @param \Illuminate\View\Factory $factory
* @param \Illuminate\View\Engines\EngineInterface $engine
* @param \Illuminate\Contracts\View\Engine $engine
* @param string $view
* @param string $path
* @param mixed $data
* @return void
*/
public function __construct(Factory $factory, EngineInterface $engine, $view, $path, $data = [])
public function __construct(Factory $factory, Engine $engine, $view, $path, $data = [])
{
$this->view = $view;
$this->path = $path;
@@ -89,15 +89,15 @@ class View implements ArrayAccess, ViewContract
// Once we have the contents of the view, we will flush the sections if we are
// done rendering all views so that there is nothing left hanging over when
// another view gets rendered in the future by the application developer.
$this->factory->flushSectionsIfDoneRendering();
$this->factory->flushStateIfDoneRendering();
return ! is_null($response) ? $response : $contents;
} catch (Exception $e) {
$this->factory->flushSections();
$this->factory->flushState();
throw $e;
} catch (Throwable $e) {
$this->factory->flushSections();
$this->factory->flushState();
throw $e;
}
@@ -127,18 +127,6 @@ class View implements ArrayAccess, ViewContract
return $contents;
}
/**
* Get the sections of the rendered view.
*
* @return array
*/
public function renderSections()
{
return $this->render(function () {
return $this->factory->getSections();
});
}
/**
* Get the evaluated contents of the view.
*
@@ -167,6 +155,18 @@ class View implements ArrayAccess, ViewContract
return $data;
}
/**
* Get the sections of the rendered view.
*
* @return string
*/
public function renderSections()
{
return $this->render(function () {
return $this->factory->getSections();
});
}
/**
* Add a piece of data to the view.
*
@@ -206,33 +206,21 @@ class View implements ArrayAccess, ViewContract
*/
public function withErrors($provider)
{
if ($provider instanceof MessageProvider) {
$this->with('errors', $provider->getMessageBag());
} else {
$this->with('errors', new MessageBag((array) $provider));
}
$this->with('errors', $this->formatErrors($provider));
return $this;
}
/**
* Get the view factory instance.
* Format the given message provider into a MessageBag.
*
* @return \Illuminate\View\Factory
* @param \Illuminate\Contracts\Support\MessageProvider|array $provider
* @return \Illuminate\Support\MessageBag
*/
public function getFactory()
protected function formatErrors($provider)
{
return $this->factory;
}
/**
* Get the view's rendering engine.
*
* @return \Illuminate\View\Engines\EngineInterface
*/
public function getEngine()
{
return $this->engine;
return $provider instanceof MessageProvider
? $provider->getMessageBag() : new MessageBag((array) $provider);
}
/**
@@ -286,6 +274,26 @@ class View implements ArrayAccess, ViewContract
$this->path = $path;
}
/**
* Get the view factory instance.
*
* @return \Illuminate\View\Factory
*/
public function getFactory()
{
return $this->factory;
}
/**
* Get the view's rendering engine.
*
* @return \Illuminate\Contracts\View\Engine
*/
public function getEngine()
{
return $this->engine;
}
/**
* Determine if a piece of data is bound.
*
@@ -387,11 +395,11 @@ class View implements ArrayAccess, ViewContract
*/
public function __call($method, $parameters)
{
if (Str::startsWith($method, 'with')) {
return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
if (! Str::startsWith($method, 'with')) {
throw new BadMethodCallException("Method [$method] does not exist on view.");
}
throw new BadMethodCallException("Method [$method] does not exist on view.");
return $this->with(Str::camel(substr($method, 4)), $parameters[0]);
}
/**

View File

@@ -45,6 +45,15 @@ interface ViewFinderInterface
*/
public function prependNamespace($namespace, $hints);
/**
* Replace the namespace hints for the given namespace.
*
* @param string $namespace
* @param string|array $hints
* @return $this
*/
public function replaceNamespace($namespace, $hints);
/**
* Add a valid view extension to the finder.
*
@@ -52,4 +61,11 @@ interface ViewFinderInterface
* @return void
*/
public function addExtension($extension);
/**
* Flush the cache of located views.
*
* @return void
*/
public function flush();
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Illuminate\View;
class ViewName
{
/**
* Normalize the given event name.
*
* @param string $name
* @return string
*/
public static function normalize($name)
{
$delimiter = ViewFinderInterface::HINT_PATH_DELIMITER;
if (strpos($name, $delimiter) === false) {
return str_replace('/', '.', $name);
}
list($namespace, $name) = explode($delimiter, $name);
return $namespace.$delimiter.str_replace('/', '.', $name);
}
}

View File

@@ -4,6 +4,7 @@ namespace Illuminate\View;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Engines\FileEngine;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Compilers\BladeCompiler;
@@ -17,11 +18,64 @@ class ViewServiceProvider extends ServiceProvider
*/
public function register()
{
$this->registerEngineResolver();
$this->registerFactory();
$this->registerViewFinder();
$this->registerFactory();
$this->registerEngineResolver();
}
/**
* Register the view environment.
*
* @return void
*/
public function registerFactory()
{
$this->app->singleton('view', function ($app) {
// Next we need to grab the engine resolver instance that will be used by the
// environment. The resolver will be used by an environment to get each of
// the various engine implementations such as plain PHP or Blade engine.
$resolver = $app['view.engine.resolver'];
$finder = $app['view.finder'];
$factory = $this->createFactory($resolver, $finder, $app['events']);
// We will also set the container instance on this view environment since the
// view composers may be classes registered in the container, which allows
// for great testable, flexible composers for the application developer.
$factory->setContainer($app);
$factory->share('app', $app);
return $factory;
});
}
/**
* Create a new Factory Instance.
*
* @param \Illuminate\View\Engines\EngineResolver $resolver
* @param \Illuminate\View\ViewFinderInterface $finder
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return \Illuminate\View\Factory
*/
protected function createFactory($resolver, $finder, $events)
{
return new Factory($resolver, $finder, $events);
}
/**
* Register the view finder implementation.
*
* @return void
*/
public function registerViewFinder()
{
$this->app->bind('view.finder', function ($app) {
return new FileViewFinder($app['files'], $app['config']['view.paths']);
});
}
/**
@@ -34,10 +88,10 @@ class ViewServiceProvider extends ServiceProvider
$this->app->singleton('view.engine.resolver', function () {
$resolver = new EngineResolver;
// Next we will register the various engines with the resolver so that the
// environment can resolve the engines it needs for various views based
// on the extension of view files. We call a method for each engines.
foreach (['php', 'blade'] as $engine) {
// Next, we will register the various view engines with the resolver so that the
// environment will resolve the engines needed for various views based on the
// extension of view file. We call a method for each of the view's engines.
foreach (['file', 'php', 'blade'] as $engine) {
$this->{'register'.ucfirst($engine).'Engine'}($resolver);
}
@@ -45,6 +99,19 @@ class ViewServiceProvider extends ServiceProvider
});
}
/**
* Register the file engine implementation.
*
* @param \Illuminate\View\Engines\EngineResolver $resolver
* @return void
*/
public function registerFileEngine($resolver)
{
$resolver->register('file', function () {
return new FileEngine;
});
}
/**
* Register the PHP engine implementation.
*
@@ -66,61 +133,17 @@ class ViewServiceProvider extends ServiceProvider
*/
public function registerBladeEngine($resolver)
{
$app = $this->app;
// The Compiler engine requires an instance of the CompilerInterface, which in
// this case will be the Blade compiler, so we'll first create the compiler
// instance to pass into the engine so it can compile the views properly.
$app->singleton('blade.compiler', function ($app) {
$cache = $app['config']['view.compiled'];
return new BladeCompiler($app['files'], $cache);
$this->app->singleton('blade.compiler', function () {
return new BladeCompiler(
$this->app['files'], $this->app['config']['view.compiled']
);
});
$resolver->register('blade', function () use ($app) {
return new CompilerEngine($app['blade.compiler']);
});
}
/**
* Register the view finder implementation.
*
* @return void
*/
public function registerViewFinder()
{
$this->app->bind('view.finder', function ($app) {
$paths = $app['config']['view.paths'];
return new FileViewFinder($app['files'], $paths);
});
}
/**
* Register the view environment.
*
* @return void
*/
public function registerFactory()
{
$this->app->singleton('view', function ($app) {
// Next we need to grab the engine resolver instance that will be used by the
// environment. The resolver will be used by an environment to get each of
// the various engine implementations such as plain PHP or Blade engine.
$resolver = $app['view.engine.resolver'];
$finder = $app['view.finder'];
$env = new Factory($resolver, $finder, $app['events']);
// We will also set the container instance on this view environment since the
// view composers may be classes registered in the container, which allows
// for great testable, flexible composers for the application developer.
$env->setContainer($app);
$env->share('app', $app);
return $env;
$resolver->register('blade', function () {
return new CompilerEngine($this->app['blade.compiler']);
});
}
}

View File

@@ -2,7 +2,7 @@
"name": "illuminate/view",
"description": "The Illuminate View package.",
"license": "MIT",
"homepage": "http://laravel.com",
"homepage": "https://laravel.com",
"support": {
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
@@ -14,13 +14,13 @@
}
],
"require": {
"php": ">=5.5.9",
"illuminate/container": "5.2.*",
"illuminate/contracts": "5.2.*",
"illuminate/events": "5.2.*",
"illuminate/filesystem": "5.2.*",
"illuminate/support": "5.2.*",
"symfony/debug": "2.8.*|3.0.*"
"php": ">=7.0",
"illuminate/container": "5.5.*",
"illuminate/contracts": "5.5.*",
"illuminate/events": "5.5.*",
"illuminate/filesystem": "5.5.*",
"illuminate/support": "5.5.*",
"symfony/debug": "~3.3"
},
"autoload": {
"psr-4": {
@@ -29,8 +29,11 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.2-dev"
"dev-master": "5.5-dev"
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev"
}