the whole shebang

This commit is contained in:
2014-11-25 16:42:40 +01:00
parent 7f74c0613e
commit ab1334c0cf
3686 changed files with 496409 additions and 1 deletions

View File

@@ -0,0 +1,450 @@
<?php namespace Illuminate\View\Compilers;
use Closure;
use Illuminate\Filesystem\Filesystem;
class BladeCompiler extends Compiler implements CompilerInterface {
/**
* All of the registered extensions.
*
* @var array
*/
protected $extensions = array();
/**
* All of the available compiler functions.
*
* @var array
*/
protected $compilers = array(
'Extensions',
'Extends',
'Comments',
'Echos',
'Openings',
'Closings',
'Else',
'Unless',
'EndUnless',
'Includes',
'Each',
'Yields',
'Shows',
'Language',
'SectionStart',
'SectionStop',
'SectionOverwrite',
);
/**
* Array of opening and closing tags for echos.
*
* @var array
*/
protected $contentTags = array('{{', '}}');
/**
* Array of opening and closing tags for escaped echos.
*
* @var array
*/
protected $escapedTags = array('{{{', '}}}');
/**
* Compile the view at the given path.
*
* @param string $path
* @return void
*/
public function compile($path)
{
$contents = $this->compileString($this->files->get($path));
if ( ! is_null($this->cachePath))
{
$this->files->put($this->getCompiledPath($path), $contents);
}
}
/**
* Compile the given Blade template contents.
*
* @param string $value
* @return string
*/
public function compileString($value)
{
foreach ($this->compilers as $compiler)
{
$value = $this->{"compile{$compiler}"}($value);
}
return $value;
}
/**
* Register a custom Blade compiler.
*
* @param Closure $compiler
* @return void
*/
public function extend(Closure $compiler)
{
$this->extensions[] = $compiler;
}
/**
* Execute the user defined extensions.
*
* @param string $value
* @return string
*/
protected function compileExtensions($value)
{
foreach ($this->extensions as $compiler)
{
$value = call_user_func($compiler, $value, $this);
}
return $value;
}
/**
* Compile Blade template extensions into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileExtends($value)
{
// By convention, Blade views using template inheritance must begin with the
// @extends expression, otherwise they will not be compiled with template
// inheritance. So, if they do not start with that we will just return.
if (strpos($value, '@extends') !== 0)
{
return $value;
}
$lines = preg_split("/(\r?\n)/", $value);
// Next, we just want to split the values by lines, and create an expression
// to include the parent layout at the end of the templates. Which allows
// the sections to get registered before the parent view gets rendered.
$lines = $this->compileLayoutExtends($lines);
return implode("\r\n", array_slice($lines, 1));
}
/**
* Compile the proper template inheritance for the lines.
*
* @param array $lines
* @return array
*/
protected function compileLayoutExtends($lines)
{
$pattern = $this->createMatcher('extends');
$lines[] = preg_replace($pattern, '$1@include$2', $lines[0]);
return $lines;
}
/**
* 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, '<?php /* $1 */ ?>', $value);
}
/**
* Compile Blade echos into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileEchos($value)
{
$difference = strlen($this->contentTags[0]) - strlen($this->escapedTags[0]);
if ($difference > 0)
{
return $this->compileEscapedEchos($this->compileRegularEchos($value));
}
return $this->compileRegularEchos($this->compileEscapedEchos($value));
}
/**
* Compile the "regular" echo statements.
*
* @param string $value
* @return string
*/
protected function compileRegularEchos($value)
{
$pattern = sprintf('/%s\s*(.+?)\s*%s/s', $this->contentTags[0], $this->contentTags[1]);
return preg_replace($pattern, '<?php echo $1; ?>', $value);
}
/**
* Compile the escaped echo statements.
*
* @param string $value
* @return string
*/
protected function compileEscapedEchos($value)
{
$pattern = sprintf('/%s\s*(.+?)\s*%s/s', $this->escapedTags[0], $this->escapedTags[1]);
return preg_replace($pattern, '<?php echo e($1); ?>', $value);
}
/**
* Compile Blade structure openings into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileOpenings($value)
{
$pattern = '/(?(R)\((?:[^\(\)]|(?R))*\)|(?<!\w)(\s*)@(if|elseif|foreach|for|while)(\s*(?R)+))/';
return preg_replace($pattern, '$1<?php $2$3: ?>', $value);
}
/**
* Compile Blade structure closings into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileClosings($value)
{
$pattern = '/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/';
return preg_replace($pattern, '$1<?php $2; ?>$3', $value);
}
/**
* Compile Blade else statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileElse($value)
{
$pattern = $this->createPlainMatcher('else');
return preg_replace($pattern, '$1<?php else: ?>$2', $value);
}
/**
* Compile Blade unless statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileUnless($value)
{
$pattern = $this->createMatcher('unless');
return preg_replace($pattern, '$1<?php if ( !$2): ?>', $value);
}
/**
* Compile Blade end unless statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileEndUnless($value)
{
$pattern = $this->createPlainMatcher('endunless');
return preg_replace($pattern, '$1<?php endif; ?>$2', $value);
}
/**
* Compile Blade include statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileIncludes($value)
{
$pattern = $this->createOpenMatcher('include');
$replace = '$1<?php echo $__env->make$2, array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>';
return preg_replace($pattern, $replace, $value);
}
/**
* Compile Blade each statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileEach($value)
{
$pattern = $this->createMatcher('each');
return preg_replace($pattern, '$1<?php echo $__env->renderEach$2; ?>', $value);
}
/**
* Compile Blade yield statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileYields($value)
{
$pattern = $this->createMatcher('yield');
return preg_replace($pattern, '$1<?php echo $__env->yieldContent$2; ?>', $value);
}
/**
* Compile Blade show statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileShows($value)
{
$pattern = $this->createPlainMatcher('show');
return preg_replace($pattern, '$1<?php echo $__env->yieldSection(); ?>$2', $value);
}
/**
* Compile Blade language and language choice statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileLanguage($value)
{
$pattern = $this->createMatcher('lang');
$value = preg_replace($pattern, '$1<?php echo \Illuminate\Support\Facades\Lang::get$2; ?>', $value);
$pattern = $this->createMatcher('choice');
return preg_replace($pattern, '$1<?php echo \Illuminate\Support\Facades\Lang::choice$2; ?>', $value);
}
/**
* Compile Blade section start statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileSectionStart($value)
{
$pattern = $this->createMatcher('section');
return preg_replace($pattern, '$1<?php $__env->startSection$2; ?>', $value);
}
/**
* Compile Blade section stop statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileSectionStop($value)
{
$pattern = $this->createPlainMatcher('stop');
$value = preg_replace($pattern, '$1<?php $__env->stopSection(); ?>$2', $value);
$pattern = $this->createPlainMatcher('endsection');
return preg_replace($pattern, '$1<?php $__env->stopSection(); ?>$2', $value);
}
/**
* Compile Blade section stop statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileSectionOverwrite($value)
{
$pattern = $this->createPlainMatcher('overwrite');
return preg_replace($pattern, '$1<?php $__env->stopSection(true); ?>$2', $value);
}
/**
* Get the regular expression for a generic Blade function.
*
* @param string $function
* @return string
*/
public function createMatcher($function)
{
return '/(?<!\w)(\s*)@'.$function.'(\s*\(.*\))/';
}
/**
* Get the regular expression for a generic Blade function.
*
* @param string $function
* @return string
*/
public function createOpenMatcher($function)
{
return '/(?<!\w)(\s*)@'.$function.'(\s*\(.*)\)/';
}
/**
* Create a plain Blade matcher.
*
* @param string $function
* @return string
*/
public function createPlainMatcher($function)
{
return '/(?<!\w)(\s*)@'.$function.'(\s*)/';
}
/**
* 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} = array(preg_quote($openTag), preg_quote($closeTag));
}
/**
* 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);
}
}

View File

@@ -0,0 +1,68 @@
<?php namespace Illuminate\View\Compilers;
use Illuminate\Filesystem\Filesystem;
abstract class Compiler {
/**
* The Filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* Get the cache path for the compiled views.
*
* @var string
*/
protected $cachePath;
/**
* Create a new compiler instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param string $cachePath
* @return void
*/
public function __construct(Filesystem $files, $cachePath)
{
$this->files = $files;
$this->cachePath = $cachePath;
}
/**
* Get the path to the compiled version of a view.
*
* @param string $path
* @return string
*/
public function getCompiledPath($path)
{
return $this->cachePath.'/'.md5($path);
}
/**
* Determine if the view at the given path is expired.
*
* @param string $path
* @return bool
*/
public function isExpired($path)
{
$compiled = $this->getCompiledPath($path);
// If the compiled file doesn't exist we will indicate that the view is expired
// so that it can be re-compiled. Else, we will verify the last modification
// of the views is less than the modification times of the compiled views.
if ( ! $this->cachePath or ! $this->files->exists($compiled))
{
return true;
}
$lastModified = $this->files->lastModified($path);
return $lastModified >= $this->files->lastModified($compiled);
}
}

View File

@@ -0,0 +1,29 @@
<?php namespace Illuminate\View\Compilers;
interface CompilerInterface {
/**
* Get the path to the compiled version of a view.
*
* @param string $path
* @return string
*/
public function getCompiledPath($path);
/**
* Determine if the given view is expired.
*
* @param string $path
* @return bool
*/
public function isExpired($path);
/**
* Compile the view at the given path.
*
* @param string $path
* @return void
*/
public function compile($path);
}

View File

@@ -0,0 +1,58 @@
<?php namespace Illuminate\View\Engines;
use Illuminate\View\Compilers\CompilerInterface;
class CompilerEngine extends PhpEngine {
/**
* The Blade compiler instance.
*
* @var \Illuminate\View\Compilers\CompilerInterface
*/
protected $compiler;
/**
* Create a new Blade view engine instance.
*
* @param \Illuminate\View\Compilers\CompilerInterface $compiler
* @return void
*/
public function __construct(CompilerInterface $compiler)
{
$this->compiler = $compiler;
}
/**
* Get the evaluated contents of the view.
*
* @param \Illuminate\View\Environment $environment
* @param string $view
* @param array $data
* @return string
*/
public function get($path, array $data = array())
{
// If this given view has expired, which means it has simply been edited since
// it was last compiled, we will re-compile the views so we can evaluate a
// fresh copy of the view. We'll pass the compiler the path of the view.
if ($this->compiler->isExpired($path))
{
$this->compiler->compile($path);
}
$compiled = $this->compiler->getCompiledPath($path);
return $this->evaluatePath($compiled, $data);
}
/**
* Get the compiler implementation.
*
* @return \Illuminate\View\Compilers\CompilerInterface
*/
public function getCompiler()
{
return $this->compiler;
}
}

View File

@@ -0,0 +1,32 @@
<?php namespace Illuminate\View\Engines;
abstract class Engine {
/**
* The view that was last to be rendered.
*
* @var string
*/
protected $lastRendered;
/**
* Determine if the engine is sectionable.
*
* @return bool
*/
public function isSectionable()
{
return $this instanceof SectionableInterface;
}
/**
* Get the last view that was rendered.
*
* @return string
*/
public function getLastRendered()
{
return $this->lastRendered;
}
}

View File

@@ -0,0 +1,14 @@
<?php namespace Illuminate\View\Engines;
interface EngineInterface {
/**
* Get the evaluated contents of the view.
*
* @param string $path
* @param array $data
* @return string
*/
public function get($path, array $data = array());
}

View File

@@ -0,0 +1,49 @@
<?php namespace Illuminate\View\Engines;
use Closure;
class EngineResolver {
/**
* The array of engine resolvers.
*
* @var array
*/
protected $resolvers = array();
/**
* The resolved engine instances.
*
* @var array
*/
protected $resolved = array();
/**
* Register a new engine resolver.
*
* @param string $engine
* @param Closure $resolver
* @return void
*/
public function register($engine, Closure $resolver)
{
$this->resolvers[$engine] = $resolver;
}
/**
* Resolver an engine instance by name.
*
* @param string $engine
* @return \Illuminate\View\Engines\EngineInterface
*/
public function resolve($engine)
{
if ( ! isset($this->resolved[$engine]))
{
$this->resolved[$engine] = call_user_func($this->resolvers[$engine]);
}
return $this->resolved[$engine];
}
}

View File

@@ -0,0 +1,59 @@
<?php namespace Illuminate\View\Engines;
use Illuminate\View\Exception;
use Illuminate\View\Environment;
class PhpEngine implements EngineInterface {
/**
* Get the evaluated contents of the view.
*
* @param string $path
* @param array $data
* @return string
*/
public function get($path, array $data = array())
{
return $this->evaluatePath($path, $data);
}
/**
* Get the evaluated contents of the view at the given path.
*
* @param string $__path
* @param array $__data
* @return string
*/
protected function evaluatePath($__path, $__data)
{
ob_start();
extract($__data);
// We'll evaluate the contents of the view inside a try/catch block so we can
// flush out any stray output that might get out before an error occurs or
// an exception is thrown. This prevents any partial views from leaking.
try
{
include $__path;
}
catch (\Exception $e)
{
$this->handleViewException($e);
}
return ob_get_clean();
}
/**
* Handle a view exception.
*
* @param Exception $e
* @return void
*/
protected function handleViewException($e)
{
ob_get_clean(); throw $e;
}
}

View File

@@ -0,0 +1,703 @@
<?php namespace Illuminate\View;
use Closure;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\Support\Contracts\ArrayableInterface as Arrayable;
class Environment {
/**
* The engine implementation.
*
* @var \Illuminate\View\Engines\EngineResolver
*/
protected $engines;
/**
* The view finder implementation.
*
* @var \Illuminate\View\ViewFinderInterface
*/
protected $finder;
/**
* The event dispatcher instance.
*
* @var \Illuminate\Events\Dispatcher
*/
protected $events;
/**
* The IoC container instance.
*
* @var \Illuminate\Container
*/
protected $container;
/**
* Data that should be available to all templates.
*
* @var array
*/
protected $shared = array();
/**
* All of the registered view names.
*
* @var array
*/
protected $names = array();
/**
* The extension to engine bindings.
*
* @var array
*/
protected $extensions = array('blade.php' => 'blade', 'php' => 'php');
/**
* The view composer events.
*
* @var array
*/
protected $composers = array();
/**
* All of the finished, captured sections.
*
* @var array
*/
protected $sections = array();
/**
* The stack of in-progress sections.
*
* @var array
*/
protected $sectionStack = array();
/**
* The number of active rendering operations.
*
* @var int
*/
protected $renderCount = 0;
/**
* Create a new view environment instance.
*
* @param \Illuminate\View\Engines\EngineResolver $engines
* @param \Illuminate\View\ViewFinderInterface $finder
* @param \Illuminate\Events\Dispatcher $events
* @return void
*/
public function __construct(EngineResolver $engines, ViewFinderInterface $finder, Dispatcher $events)
{
$this->finder = $finder;
$this->events = $events;
$this->engines = $engines;
$this->share('__env', $this);
}
/**
* Get a evaluated view contents for the given view.
*
* @param string $view
* @param array $data
* @param array $mergeData
* @return \Illuminate\View\View
*/
public function make($view, $data = array(), $mergeData = array())
{
$path = $this->finder->find($view);
$data = array_merge($mergeData, $this->parseData($data));
$this->callCreator($view = new View($this, $this->getEngineFromPath($path), $view, $path, $data));
return $view;
}
/**
* 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 a evaluated view contents for a named view.
*
* @param string $view
* @param mixed $data
* @return \Illuminate\View\View
*/
public function of($view, $data = array())
{
return $this->make($this->names[$view], $data);
}
/**
* Register a named view.
*
* @param string $view
* @param string $name
* @return void
*/
public function name($view, $name)
{
$this->names[$name] = $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;
}
return true;
}
/**
* Get the rendered contents of a partial from a loop.
*
* @param string $view
* @param array $data
* @param string $iterator
* @param string $empty
* @return string
*/
public function renderEach($view, $data, $iterator, $empty = 'raw|')
{
$result = '';
// If is actually data in the array, we will loop through the data and append
// an instance of the partial view to the final result HTML passing in the
// iterated value of this data array, allowing the views to access them.
if (count($data) > 0)
{
foreach ($data as $key => $value)
{
$data = array('key' => $key, $iterator => $value);
$result .= $this->make($view, $data)->render();
}
}
// If there is no data in the array, we will render the contents of the empty
// 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 (starts_with($empty, 'raw|'))
{
$result = substr($empty, 4);
}
else
{
$result = $this->make($empty)->render();
}
}
return $result;
}
/**
* Get the appropriate view engine for the given path.
*
* @param string $path
* @return \Illuminate\View\Engines\EngineInterface
*/
protected function getEngineFromPath($path)
{
$engine = $this->extensions[$this->getExtension($path)];
return $this->engines->resolve($engine);
}
/**
* Get the extension used by the view file.
*
* @param string $path
* @return string
*/
protected function getExtension($path)
{
$extensions = array_keys($this->extensions);
return array_first($extensions, function($key, $value) use ($path)
{
return ends_with($path, $value);
});
}
/**
* Add a piece of shared data to the environment.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function share($key, $value = null)
{
if ( ! is_array($key)) return $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 = array();
foreach ((array) $views as $view)
{
$creators[] = $this->addViewEvent($view, $callback, 'creating: ');
}
return $creators;
}
/**
* Register a view composer event.
*
* @param array|string $views
* @param \Closure|string $callback
* @return array
*/
public function composer($views, $callback)
{
$composers = array();
foreach ((array) $views as $view)
{
$composers[] = $this->addViewEvent($view, $callback);
}
return $composers;
}
/**
* Add an event for a given view.
*
* @param string $view
* @param Closure|string $callback
* @param string $prefix
* @return Closure
*/
protected function addViewEvent($view, $callback, $prefix = 'composing: ')
{
if ($callback instanceof Closure)
{
$this->events->listen($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->events->listen($name, $callback);
return $callback;
}
/**
* Build a class based container callback Closure.
*
* @param string $class
* @param string $prefix
* @return \Closure
*/
protected function buildClassEventCallback($class, $prefix)
{
$container = $this->container;
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, $container)
{
$callable = array($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);
}
else
{
$method = str_contains($prefix, 'composing') ? 'compose' : 'create';
return array($class, $method);
}
}
/**
* Call the composer for a given view.
*
* @param \Illuminate\View\View $view
* @return void
*/
public function callComposer(View $view)
{
$this->events->fire('composing: '.$view->getName(), array($view));
}
/**
* Call the creator for a given view.
*
* @param \Illuminate\View\View $view
* @return void
*/
public function callCreator(View $view)
{
$this->events->fire('creating: '.$view->getName(), array($view));
}
/**
* Start injecting content into a section.
*
* @param string $section
* @param string $content
* @return void
*/
public function startSection($section, $content = '')
{
if ($content === '')
{
ob_start() and $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()
{
return $this->yieldContent($this->stopSection());
}
/**
* Stop injecting content into a section.
*
* @param bool $overwrite
* @return string
*/
public function stopSection($overwrite = false)
{
$last = array_pop($this->sectionStack);
if ($overwrite)
{
$this->sections[$last] = ob_get_clean();
}
else
{
$this->extendSection($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;
}
else
{
$this->sections[$section] = $content;
}
}
/**
* Get the string contents of a section.
*
* @param string $section
* @param string $default
* @return string
*/
public function yieldContent($section, $default = '')
{
return isset($this->sections[$section]) ? $this->sections[$section] : $default;
}
/**
* Flush all of the section contents.
*
* @return void
*/
public function flushSections()
{
$this->sections = array();
$this->sectionStack = array();
}
/**
* Increment the rendering counter.
*
* @return void
*/
public function incrementRender()
{
$this->renderCount++;
}
/**
* Decrement the rendering counter.
*
* @return void
*/
public function decrementRender()
{
$this->renderCount--;
}
/**
* Check if there are no active render operations.
*
* @return bool
*/
public function doneRendering()
{
return $this->renderCount == 0;
}
/**
* Add a location to the array of view locations.
*
* @param string $location
* @return void
*/
public function addLocation($location)
{
$this->finder->addLocation($location);
}
/**
* Add a new namespace to the loader.
*
* @param string $namespace
* @param string|array $hints
* @return void
*/
public function addNamespace($namespace, $hints)
{
$this->finder->addNamespace($namespace, $hints);
}
/**
* Register a valid view extension and its engine.
*
* @param string $extension
* @param string $engine
* @param Closure $resolver
* @return void
*/
public function addExtension($extension, $engine, $resolver = null)
{
$this->finder->addExtension($extension);
if (isset($resolver))
{
$this->engines->register($engine, $resolver);
}
unset($this->extensions[$engine]);
$this->extensions = array_merge(array($extension => $engine), $this->extensions);
}
/**
* Get the extension to engine bindings.
*
* @return array
*/
public function getExtensions()
{
return $this->extensions;
}
/**
* Get the engine resolver instance.
*
* @return \Illuminate\View\Engines\EngineResolver
*/
public function getEngineResolver()
{
return $this->engines;
}
/**
* Get the view finder instance.
*
* @return \Illuminate\View\ViewFinderInterface
*/
public function getFinder()
{
return $this->finder;
}
/**
* Get the event dispatcher instance.
*
* @return \Illuminate\Events\Dispatcher
*/
public function getDispatcher()
{
return $this->events;
}
/**
* Set the event dispatcher instance.
*
* @param \Illuminate\Events\Dispatcher
* @return void
*/
public function setDispatcher(Dispatcher $events)
{
$this->events = $events;
}
/**
* Get the IoC container instance.
*
* @return \Illuminate\Container\Container
*/
public function getContainer()
{
return $this->container;
}
/**
* Set the IoC container instance.
*
* @param \Illuminate\Container\Container $container
* @return void
*/
public function setContainer(Container $container)
{
$this->container = $container;
}
/**
* Get all of the shared data for the environment.
*
* @return array
*/
public function getShared()
{
return $this->shared;
}
/**
* 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

@@ -0,0 +1,227 @@
<?php namespace Illuminate\View;
use Illuminate\Filesystem\Filesystem;
class FileViewFinder implements ViewFinderInterface {
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* The array of active view paths.
*
* @var array
*/
protected $paths;
/**
* The namespace to file path hints.
*
* @var array
*/
protected $hints = array();
/**
* Register a view extension with the finder.
*
* @var array
*/
protected $extensions = array('blade.php', 'php');
/**
* Create a new file view loader instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param array $paths
* @param array $extensions
* @return void
*/
public function __construct(Filesystem $files, array $paths, array $extensions = null)
{
$this->files = $files;
$this->paths = $paths;
if (isset($extensions))
{
$this->extensions = $extensions;
}
}
/**
* Get the fully qualified location of the view.
*
* @param string $name
* @return string
*/
public function find($name)
{
if (strpos($name, '::') !== false) return $this->findNamedPathView($name);
return $this->findInPaths($name, $this->paths);
}
/**
* Get the path to a template with a named path.
*
* @param string $name
* @return string
*/
protected function findNamedPathView($name)
{
list($namespace, $view) = $this->getNamespaceSegments($name);
return $this->findInPaths($view, $this->hints[$namespace]);
}
/**
* Get the segments of a template with a named path.
*
* @param string $name
* @return array
*/
protected function getNamespaceSegments($name)
{
$segments = explode('::', $name);
if (count($segments) != 2)
{
throw new \InvalidArgumentException("View [$name] has an invalid name.");
}
if ( ! isset($this->hints[$segments[0]]))
{
throw new \InvalidArgumentException("No hint path defined for [{$segments[0]}].");
}
return $segments;
}
/**
* Find the given view in the list of paths.
*
* @param string $name
* @param array $paths
* @return string
*/
protected function findInPaths($name, $paths)
{
foreach ((array) $paths as $path)
{
foreach ($this->getPossibleViewFiles($name) as $file)
{
if ($this->files->exists($viewPath = $path.'/'.$file))
{
return $viewPath;
}
}
}
throw new \InvalidArgumentException("View [$name] not found.");
}
/**
* Get an array of possible view files.
*
* @param string $name
* @return array
*/
protected function getPossibleViewFiles($name)
{
return array_map(function($extension) use ($name)
{
return str_replace('.', '/', $name).'.'.$extension;
}, $this->extensions);
}
/**
* Add a location to the finder.
*
* @param string $location
* @return void
*/
public function addLocation($location)
{
$this->paths[] = $location;
}
/**
* Add a namespace hint to the finder.
*
* @param string $namespace
* @param string|array $hints
* @return void
*/
public function addNamespace($namespace, $hints)
{
$hints = (array) $hints;
if (isset($this->hints[$namespace]))
{
$hints = array_merge($this->hints[$namespace], $hints);
}
$this->hints[$namespace] = $hints;
}
/**
* Register an extension with the view finder.
*
* @param string $extension
* @return void
*/
public function addExtension($extension)
{
if (($index = array_search($extension, $this->extensions)) !== false)
{
unset($this->extensions[$index]);
}
array_unshift($this->extensions, $extension);
}
/**
* Get the filesystem instance.
*
* @return \Illuminate\Filesystem\Filesystem
*/
public function getFilesystem()
{
return $this->files;
}
/**
* Get the active view paths.
*
* @return array
*/
public function getPaths()
{
return $this->paths;
}
/**
* Get the namespace to file path hints.
*
* @return array
*/
public function getHints()
{
return $this->hints;
}
/**
* Get registered extensions.
*
* @return array
*/
public function getExtensions()
{
return $this->extensions;
}
}

View File

@@ -0,0 +1,317 @@
<?php namespace Illuminate\View;
use ArrayAccess;
use Illuminate\View\Engines\EngineInterface;
use Illuminate\Support\Contracts\ArrayableInterface as Arrayable;
use Illuminate\Support\Contracts\RenderableInterface as Renderable;
class View implements ArrayAccess, Renderable {
/**
* The view environment instance.
*
* @var \Illuminate\View\Environment
*/
protected $environment;
/**
* The engine implementation.
*
* @var \Illuminate\View\Engines\EngineInterface
*/
protected $engine;
/**
* The name of the view.
*
* @var string
*/
protected $view;
/**
* The array of view data.
*
* @var array
*/
protected $data;
/**
* The path to the view file.
*
* @var string
*/
protected $path;
/**
* Create a new view instance.
*
* @param \Illuminate\View\Environment $environment
* @param \Illuminate\View\Engines\EngineInterface $engine
* @param string $view
* @param string $path
* @param array $data
* @return void
*/
public function __construct(Environment $environment, EngineInterface $engine, $view, $path, $data = array())
{
$this->view = $view;
$this->path = $path;
$this->engine = $engine;
$this->environment = $environment;
$this->data = $data instanceof Arrayable ? $data->toArray() : (array) $data;
}
/**
* Get the string contents of the view.
*
* @return string
*/
public function render()
{
$env = $this->environment;
// We will keep track of the amount of views being rendered so we can flush
// the section after the complete rendering operation is done. This will
// clear out the sections for any separate views that may be rendered.
$env->incrementRender();
$env->callComposer($this);
$contents = trim($this->getContents());
// Once we've finished rendering the view, we'll decrement the render count
// then if we are at the bottom of the stack we'll flush out sections as
// they might interfere with totally separate view's evaluations later.
$env->decrementRender();
if ($env->doneRendering()) $env->flushSections();
return $contents;
}
/**
* Get the evaluated contents of the view.
*
* @return string
*/
protected function getContents()
{
return $this->engine->get($this->path, $this->gatherData());
}
/**
* Get the data bound to the view instance.
*
* @return array
*/
protected function gatherData()
{
$data = array_merge($this->environment->getShared(), $this->data);
foreach ($data as $key => $value)
{
if ($value instanceof Renderable)
{
$data[$key] = $value->render();
}
}
return $data;
}
/**
* Add a piece of data to the view.
*
* @param string|array $key
* @param mixed $value
* @return \Illuminate\View\View
*/
public function with($key, $value = null)
{
if (is_array($key))
{
$this->data = array_merge($this->data, $key);
}
else
{
$this->data[$key] = $value;
}
return $this;
}
/**
* Add a view instance to the view data.
*
* @param string $key
* @param string $view
* @param array $data
* @return \Illuminate\View\View
*/
public function nest($key, $view, array $data = array())
{
return $this->with($key, $this->environment->make($view, $data));
}
/**
* Get the view environment instance.
*
* @return \Illuminate\View\Environment
*/
public function getEnvironment()
{
return $this->environment;
}
/**
* Get the view's rendering engine.
*
* @return \Illuminate\View\Engines\EngineInterface
*/
public function getEngine()
{
return $this->engine;
}
/**
* Get the name of the view.
*
* @return string
*/
public function getName()
{
return $this->view;
}
/**
* Get the array of view data.
*
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* Get the path to the view file.
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set the path to the view.
*
* @param string $path
* @return void
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* Determine if a piece of data is bound.
*
* @param string $key
* @return bool
*/
public function offsetExists($key)
{
return array_key_exists($key, $this->data);
}
/**
* Get a piece of bound data to the view.
*
* @param string $key
* @return mixed
*/
public function offsetGet($key)
{
return $this->data[$key];
}
/**
* Set a piece of data on the view.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function offsetSet($key, $value)
{
$this->with($key, $value);
}
/**
* Unset a piece of data from the view.
*
* @param string $key
* @return void
*/
public function offsetUnset($key)
{
unset($this->data[$key]);
}
/**
* Get a piece of data from the view.
*
* @return mixed
*/
public function __get($key)
{
return $this->data[$key];
}
/**
* Set a piece of data on the view.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
$this->with($key, $value);
}
/**
* Check if a piece of data is bound to the view.
*
* @param string $key
* @return bool
*/
public function __isset($key)
{
return isset($this->data[$key]);
}
/**
* Remove a piece of bound data from the view.
*
* @param string $key
* @return bool
*/
public function __unset($key)
{
unset($this->data[$key]);
}
/**
* Get the string contents of the view.
*
* @return string
*/
public function __toString()
{
return $this->render();
}
}

View File

@@ -0,0 +1,38 @@
<?php namespace Illuminate\View;
interface ViewFinderInterface {
/**
* Get the fully qualified location of the view.
*
* @param string $view
* @return string
*/
public function find($view);
/**
* Add a location to the finder.
*
* @param string $location
* @return void
*/
public function addLocation($location);
/**
* Add a namespace hint to the finder.
*
* @param string $namespace
* @param string $hint
* @return void
*/
public function addNamespace($namespace, $hint);
/**
* Add a valid view extension to the finder.
*
* @param string $extension
* @return void
*/
public function addExtension($extension);
}

View File

@@ -0,0 +1,182 @@
<?php namespace Illuminate\View;
use Illuminate\Support\MessageBag;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Engines\BladeEngine;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Compilers\BladeCompiler;
class ViewServiceProvider extends ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerEngineResolver();
$this->registerViewFinder();
// Once the other components have been registered we're ready to include the
// view environment and session binder. The session binder will bind onto
// the "before" application event and add errors into shared view data.
$this->registerEnvironment();
$this->registerSessionBinder();
}
/**
* Register the engine resolver instance.
*
* @return void
*/
public function registerEngineResolver()
{
list($me, $app) = array($this, $this->app);
$app['view.engine.resolver'] = $app->share(function($app) use ($me)
{
$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 (array('php', 'blade') as $engine)
{
$me->{'register'.ucfirst($engine).'Engine'}($resolver);
}
return $resolver;
});
}
/**
* Register the PHP engine implementation.
*
* @param \Illuminate\View\Engines\EngineResolver $resolver
* @return void
*/
public function registerPhpEngine($resolver)
{
$resolver->register('php', function() { return new PhpEngine; });
}
/**
* Register the Blade engine implementation.
*
* @param \Illuminate\View\Engines\EngineResolver $resolver
* @return void
*/
public function registerBladeEngine($resolver)
{
$app = $this->app;
$resolver->register('blade', function() use ($app)
{
$cache = $app['path.storage'].'/views';
// 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.
$compiler = new BladeCompiler($app['files'], $cache);
return new CompilerEngine($compiler, $app['files']);
});
}
/**
* Register the view finder implementation.
*
* @return void
*/
public function registerViewFinder()
{
$this->app['view.finder'] = $this->app->share(function($app)
{
$paths = $app['config']['view.paths'];
return new FileViewFinder($app['files'], $paths);
});
}
/**
* Register the view environment.
*
* @return void
*/
public function registerEnvironment()
{
$this->app['view'] = $this->app->share(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 Environment($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;
});
}
/**
* Register the session binder for the view environment.
*
* @return void
*/
protected function registerSessionBinder()
{
list($app, $me) = array($this->app, $this);
$app->booted(function() use ($app, $me)
{
// If the current session has an "errors" variable bound to it, we will share
// its value with all view instances so the views can easily access errors
// without having to bind. An empty bag is set when there aren't errors.
if ($me->sessionHasErrors($app))
{
$errors = $app['session']->get('errors');
$app['view']->share('errors', $errors);
}
// Putting the errors in the view for every view allows the developer to just
// assume that some errors are always available, which is convenient since
// they don't have to continually run checks for the presence of errors.
else
{
$app['view']->share('errors', new MessageBag);
}
});
}
/**
* Determine if the application session has errors.
*
* @param \Illuminate\Foundation\Application $app
* @return bool
*/
public function sessionHasErrors($app)
{
$config = $app['config']['session'];
if (isset($app['session']) and ! is_null($config['driver']))
{
return $app['session']->has('errors');
}
}
}

View File

@@ -0,0 +1,33 @@
{
"name": "illuminate/view",
"license": "MIT",
"authors": [
{
"name": "Taylor Otwell",
"email": "taylorotwell@gmail.com"
}
],
"require": {
"php": ">=5.3.0",
"illuminate/container": "4.0.x",
"illuminate/events": "4.0.x",
"illuminate/filesystem": "4.0.x",
"illuminate/support": "4.0.x"
},
"require-dev": {
"mockery/mockery": "0.7.2",
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": {
"Illuminate\\View": ""
}
},
"target-dir": "Illuminate/View",
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
}
},
"minimum-stability": "dev"
}