Files
spa-api/laravel/view.php

376 lines
8.6 KiB
PHP
Raw Normal View History

2011-09-25 22:13:43 -05:00
<?php namespace Laravel; use Closure;
2011-06-08 23:45:08 -05:00
2011-09-21 21:46:16 -05:00
class View_Factory {
2011-08-26 21:42:04 -05:00
2011-08-29 19:27:02 -05:00
/**
2011-09-21 21:46:16 -05:00
* The directory containing the views.
2011-08-29 19:27:02 -05:00
*
* @var string
2011-08-29 19:27:02 -05:00
*/
2011-09-21 21:46:16 -05:00
public $path;
2011-09-25 23:00:29 -05:00
/**
2011-09-25 23:31:54 -05:00
* The path to the directory containing compiled views.
2011-09-25 23:00:29 -05:00
*
* @var string
*/
public $compiled;
/**
2011-09-21 21:46:16 -05:00
* The view composer instance.
2011-08-29 19:27:02 -05:00
*
2011-09-21 21:46:16 -05:00
* @var View_Composer
2011-08-29 19:27:02 -05:00
*/
2011-09-21 21:46:16 -05:00
protected $composer;
2011-08-29 19:27:02 -05:00
2011-08-26 21:42:04 -05:00
/**
2011-09-21 21:46:16 -05:00
* Create a new view factory instance.
2011-08-26 21:42:04 -05:00
*
2011-09-21 21:46:16 -05:00
* @param View_Composer $composer
* @param string $path
2011-09-25 23:00:29 -05:00
* @param string $compiled
2011-08-26 21:42:04 -05:00
* @return void
*/
2011-09-25 23:00:29 -05:00
public function __construct(View_Composer $composer, $path, $compiled)
2011-08-26 21:42:04 -05:00
{
2011-09-21 21:46:16 -05:00
$this->path = $path;
$this->composer = $composer;
2011-09-25 23:00:29 -05:00
$this->compiled = $compiled;
2011-08-26 21:42:04 -05:00
}
/**
* Create a new view instance.
*
2011-09-21 21:46:16 -05:00
* The name of the view given to this method should correspond to a view
* within your application views directory. Dots or slashes may used to
* reference views within sub-directories.
*
2011-09-25 22:13:43 -05:00
* <code>
* // Create a new view instance
* $view = View::make('home.index');
*
* // Create a new view instance with bound data
* $view = View::make('home.index', array('name' => 'Taylor'));
* </code>
*
2011-09-21 21:46:16 -05:00
* @param string $view
* @param array $data
* @return View
*/
2011-09-21 21:46:16 -05:00
public function make($view, $data = array())
{
2011-09-21 21:46:16 -05:00
return new View($this, $this->composer, $view, $data, $this->path($view));
}
2011-08-26 21:42:04 -05:00
/**
* Create a new view instance from a view name.
*
2011-09-08 17:49:16 -05:00
* View names are defined in the application composers file.
*
2011-09-25 22:13:43 -05:00
* <code>
* // Create an instance of the "layout" named view
* $view = View::of('layout');
*
* // Create an instance of the "layout" view with bound data
* $view = View::of('layout', array('name' => 'Taylor'));
* </code>
*
2011-08-26 21:42:04 -05:00
* @param string $name
* @param array $data
* @return View
*/
2011-09-22 00:42:44 -05:00
public function of($name, $data = array())
2011-08-26 21:42:04 -05:00
{
2011-09-25 22:13:43 -05:00
if ( ! is_null($view = $this->composer->name($name))) return $this->make($view, $data);
2011-08-26 21:42:04 -05:00
throw new \Exception("Named view [$name] is not defined.");
}
2011-08-29 00:07:10 -05:00
/**
* Get the path to a given view on disk.
*
* @param string $view
* @return string
*/
protected function path($view)
{
2011-09-08 17:49:16 -05:00
$view = str_replace('.', '/', $view);
2011-09-25 22:13:43 -05:00
foreach (array(EXT, BLADE_EXT) as $extension)
2011-09-08 17:49:16 -05:00
{
2011-09-22 00:42:44 -05:00
if (file_exists($path = $this->path.$view.$extension)) return $path;
2011-09-08 17:49:16 -05:00
}
2011-09-25 22:13:43 -05:00
throw new \Exception("View [$view] does not exist.");
2011-08-29 00:07:10 -05:00
}
2011-09-21 21:46:16 -05:00
/**
* Magic Method for handling the dynamic creation of named views.
2011-09-25 22:13:43 -05:00
*
* <code>
* // Create an instance of the "layout" named view
* $view = View::of_layout();
*
* // Create an instance of a named view with data
* $view = View::of_layout(array('name' => 'Taylor'));
* </code>
2011-09-21 21:46:16 -05:00
*/
public function __call($method, $parameters)
{
if (strpos($method, 'of_') === 0)
{
return $this->of(substr($method, 3), Arr::get($parameters, 0, array()));
}
}
}
class View_Composer {
/**
* The view composers.
*
* @var array
*/
protected $composers;
/**
* Create a new view composer instance.
*
* @param array $composers
* @return void
*/
public function __construct($composers)
{
$this->composers = $composers;
}
/**
* Find the key for a view by name.
*
2011-09-25 22:13:43 -05:00
* The view's key can be used to create instances of the view through the typical
* methods available on the view factory.
*
2011-09-21 21:46:16 -05:00
* @param string $name
* @return string
*/
public function name($name)
{
2011-09-25 22:13:43 -05:00
// The view's name may specified in several different ways in the composers file.
// The composer may simple have a string value, which is the name. Or, the composer
// could have an array value in which a "name" key exists.
2011-09-21 21:46:16 -05:00
foreach ($this->composers as $key => $value)
{
if ($name === $value or (isset($value['name']) and $name === $value['name'])) { return $key; }
}
}
/**
* Call the composer for the view instance.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
2011-09-25 22:13:43 -05:00
// The shared composer is called for every view instance. This allows the
// convenient binding of global view data or partials within a single method.
2011-09-21 21:46:16 -05:00
if (isset($this->composers['shared'])) call_user_func($this->composers['shared'], $view);
if (isset($this->composers[$view->view]))
{
foreach ((array) $this->composers[$view->view] as $key => $value)
{
2011-09-25 22:13:43 -05:00
if ($value instanceof Closure) return call_user_func($value, $view);
2011-09-21 21:46:16 -05:00
}
}
}
}
class View {
/**
* The name of the view.
*
* @var string
*/
public $view;
/**
* The view data.
*
* @var array
*/
public $data;
/**
* The path to the view on disk.
*
* @var string
*/
protected $path;
/**
* The view composer instance.
*
* @var View_Composer
*/
protected $composer;
/**
* The view factory instance, which is used to create sub-views.
*
* @var View_Factory
*/
protected $factory;
/**
* Create a new view instance.
*
* @param View_Factory $factory
* @param View_Composer $composer
* @param string $view
* @param array $data
* @param string $path
* @return void
*/
public function __construct(View_Factory $factory, View_Composer $composer, $view, $data, $path)
{
$this->view = $view;
$this->data = $data;
$this->path = $path;
$this->factory = $factory;
$this->composer = $composer;
}
2011-08-05 11:11:26 -05:00
/**
* Get the evaluated string content of the view.
2011-06-08 23:45:08 -05:00
*
* @return string
*/
public function render()
2011-06-08 23:45:08 -05:00
{
2011-09-21 21:46:16 -05:00
$this->composer->compose($this);
2011-08-05 13:21:31 -05:00
2011-09-25 22:13:43 -05:00
// All nested views and responses are evaluated before the main view. This allows
// the assets used by these views to be added to the asset container before the
// main view is evaluated and dumps the links to the assets.
foreach ($this->data as &$data)
{
2011-08-26 21:42:04 -05:00
if ($data instanceof View or $data instanceof Response) $data = $data->render();
}
2011-09-25 22:13:43 -05:00
// We don't want the view's contents to be rendered immediately, so we will fire
// up an output buffer to catch the view output. The output of the view will be
// rendered automatically later in the request lifecycle.
2011-08-08 14:54:54 -05:00
ob_start() and extract($this->data, EXTR_SKIP);
2011-06-08 23:45:08 -05:00
2011-09-25 22:13:43 -05:00
// If the view is a "Blade" view, we need to check the view for modifications
// and get the path to the compiled view file. Otherwise, we'll just use the
// regular path to the view.
$view = (strpos($this->path, BLADE_EXT) !== false) ? $this->compile() : $this->path;
2011-09-08 17:49:16 -05:00
2011-09-25 22:13:43 -05:00
try { include $view; } catch (Exception $e) { ob_get_clean(); throw $e; }
2011-06-08 23:45:08 -05:00
return ob_get_clean();
}
/**
2011-09-21 21:46:16 -05:00
* Compile the Bladed view and return the path to the compiled view.
*
2011-09-21 21:46:16 -05:00
* @return string
*/
protected function compile()
{
2011-09-25 22:13:43 -05:00
// For simplicity, compiled views are stored in a single directory by the MD5 hash of
// their name. This allows us to avoid recreating the entire view directory structure
// within the compiled views directory.
2011-09-25 23:00:29 -05:00
$compiled = $this->factory->compiled.md5($this->view);
2011-09-21 21:46:16 -05:00
2011-09-25 22:13:43 -05:00
// The view will only be re-compiled if the view has been modified since the last compiled
// version of the view was created or no compiled view exists. Otherwise, the path will
// be returned without re-compiling.
2011-09-21 21:46:16 -05:00
if ((file_exists($compiled) and filemtime($this->path) > filemtime($compiled)) or ! file_exists($compiled))
{
file_put_contents($compiled, Blade::parse($this->path));
}
2011-09-25 22:13:43 -05:00
return $compiled;
2011-09-21 21:46:16 -05:00
}
/**
* Add a view instance to the view data.
2011-09-08 17:49:16 -05:00
*
2011-09-25 22:13:43 -05:00
* <code>
* // Add a view instance to a view's data
* $view = View::make('foo')->partial('footer', 'partials.footer');
*
* // Equivalent functionality using the "with" method
* $view = View::make('foo')->with('footer', View::make('partials.footer'));
*
* // Bind a view instance with data
* $view = View::make('foo')->partial('footer', 'partials.footer', array('name' => 'Taylor'));
* </code>
*
* @param string $key
* @param string $view
* @param array $data
* @return View
*/
public function partial($key, $view, $data = array())
{
2011-09-21 21:46:16 -05:00
return $this->with($key, $this->factory->make($view, $data));
}
/**
* Add a key / value pair to the view data.
*
* Bound data will be available to the view as variables.
*
* @param string $key
* @param mixed $value
* @return View
*/
public function with($key, $value)
{
$this->data[$key] = $value;
2011-09-21 21:46:16 -05:00
return $this;
}
2011-06-08 23:45:08 -05:00
/**
* Magic Method for getting items from the view data.
*/
public function __get($key)
{
return $this->data[$key];
}
/**
* Magic Method for setting items in the view data.
*/
public function __set($key, $value)
{
$this->with($key, $value);
2011-06-08 23:45:08 -05:00
}
/**
* Magic Method for determining if an item is in the view data.
*/
public function __isset($key)
{
return array_key_exists($key, $this->data);
}
/**
* Magic Method for removing an item from the view data.
*/
public function __unset($key)
{
unset($this->data[$key]);
}
2011-09-21 21:46:16 -05:00
}