Commit 9be3d1a5 by Taylor Otwell

refactoring the view classes.

parent 56044d31
......@@ -107,7 +107,7 @@ return array(
{
require_once SYS_PATH.'view'.EXT;
return new View_Factory(require APP_PATH.'composers'.EXT, VIEW_PATH);
return new View_Factory(VIEW_PATH, new View_Composer(require APP_PATH.'composers'.EXT));
}),
/*
......
......@@ -40,14 +40,6 @@ class Manager {
*
* Note: Database connections are managed as singletons.
*
* <code>
* // Get the default database connection
* $connection = DB::connection();
*
* // Get a specific database connection
* $connection = DB::connection('mysql');
* </code>
*
* @param string $connection
* @return Database\Connection
*/
......@@ -75,17 +67,6 @@ class Manager {
*
* This method primarily serves as a short-cut to the $connection->table() method.
*
* <code>
* // Begin a fluent query against the "users" table
* $query = DB::table('users');
*
* // Equivalent call using the connection table method.
* $query = DB::connection()->table('users');
*
* // Begin a fluent query against the "users" table for a specific connection
* $query = DB::table('users', 'mysql');
* </code>
*
* @param string $table
* @param string $connection
* @return Database\Query
......@@ -99,14 +80,6 @@ class Manager {
* Magic Method for calling methods on the default database connection.
*
* This provides a convenient API for querying or examining the default database connection.
*
* <code>
* // Run a query against the default database connection
* $results = DB::query('select * from users');
*
* // Equivalent call using the connection instance
* $results = DB::connection()->query('select * from users');
* </code>
*/
public function __call($method, $parameters)
{
......
......@@ -8,7 +8,7 @@ require 'bootstrap.php';
// --------------------------------------------------------------
// Set the error reporting and display levels.
// --------------------------------------------------------------
error_reporting(E_ALL | E_STRICT);
error_reporting(-1);
ini_set('display_errors', 'Off');
......
<?php namespace Laravel;
/**
* The view composer class is responsible for calling the composer on a view and
* searching through the view composers for a given view name. It is injected
* into the View_Factory and View instances themselves, and is managed as a singleton
* by the application IoC container.
*/
class View_Composer {
/**
* 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.
*
* @param string $name
* @return string
*/
public function name($name)
{
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)
{
if (isset($this->composers[$view->view]))
{
foreach ((array) $this->composers[$view->view] as $key => $value)
{
if ($value instanceof \Closure) return call_user_func($value, $view);
}
}
}
}
/**
* The view factory class is responsible for the instantiation of Views. It is typically
* access through the application instance from a route or controller, and is managed
* as a singleton by the application IoC container.
*/
class View_Factory {
/**
......@@ -9,10 +66,10 @@ class View_Factory {
* @param string $path
* @return void
*/
public function __construct($composers, $path)
public function __construct($path, View_Composer $composer)
{
$this->path = $path;
$this->composers = $composers;
$this->composer = $composer;
}
/**
......@@ -24,7 +81,7 @@ class View_Factory {
*/
public function make($view, $data = array())
{
return new View($view, $this->path, $data, $this->composers, $this);
return new View($view, $data, $this->path($view), $this->composer, $this);
}
/**
......@@ -36,18 +93,26 @@ class View_Factory {
*/
protected function of($name, $data = array())
{
foreach ($this->composers as $key => $value)
{
if ($name === $value or (isset($value['name']) and $name === $value['name']))
if ( ! is_null($view = $this->composer->name($name)))
{
return new View($key, $this->path, $data, $this->composers, $this);
}
return new View($view, $data, $this->path($view), $this->composer, $this);
}
throw new \Exception("Named view [$name] is not defined.");
}
/**
* Get the path to a given view on disk.
*
* @param string $view
* @return string
*/
protected function path($view)
{
return $this->path.str_replace('.', '/', $view).EXT;
}
/**
* Magic Method for handling the dynamic creation of named views.
*
* <code>
......@@ -68,6 +133,11 @@ class View_Factory {
}
/**
* The view class is returned by the View Factory "make" method, and is the primary
* class for working with individual views. It provides methods for binding data to
* views as well as evaluating and rendering their contents.
*/
class View {
/**
......@@ -78,25 +148,25 @@ class View {
public $view;
/**
* The view name with dots replaced by slashes.
* The view data.
*
* @var string
* @var array
*/
public $path;
public $data;
/**
* The view data.
* The path to the view on disk.
*
* @var array
* @var string
*/
public $data;
protected $path;
/**
* The view composers defined for the application.
* The view composer instance.
*
* @var array $composers
* @var View_Composer
*/
protected $composers;
protected $composer;
/**
* The view factory instance, which is used to create sub-views.
......@@ -111,16 +181,17 @@ class View {
* @param string $view
* @param array $data
* @param string $path
* @param array $composers
* @param View_Composer $composer
* @param View_Factory $factory
* @return void
*/
public function __construct($view, $path, $data, $composers, $factory)
public function __construct($view, $data, $path, View_Composer $composer, View_Factory $factory)
{
$this->view = $view;
$this->data = $data;
$this->path = $path;
$this->factory = $factory;
$this->composers = $composers;
$this->path = $path.str_replace('.', '/', $view).EXT;
$this->composer = $composer;
if ( ! file_exists($this->path))
{
......@@ -129,22 +200,6 @@ class View {
}
/**
* Call the composer for the view instance.
*
* @return void
*/
protected function compose()
{
if (isset($this->composers[$this->view]))
{
foreach ((array) $this->composers[$this->view] as $key => $value)
{
if ($value instanceof \Closure) return call_user_func($value, $this);
}
}
}
/**
* Get the evaluated string content of the view.
*
* If the view has a composer, it will be executed. All sub-views and responses will
......@@ -154,7 +209,7 @@ class View {
*/
public function render()
{
$this->compose();
$this->composer->compose($this);
foreach ($this->data as &$data)
{
......@@ -163,14 +218,7 @@ class View {
ob_start() and extract($this->data, EXTR_SKIP);
try
{
include $this->path;
}
catch (\Exception $e)
{
Exception\Handler::make(new Exception\Examiner($e))->handle();
}
try { include $this->path; } catch (\Exception $e) { ob_get_clean(); throw $e; }
return ob_get_clean();
}
......@@ -178,14 +226,6 @@ class View {
/**
* Add a view instance to the view data.
*
* <code>
* // Bind the view "partial/login" to the view
* View::make('home')->partial('login', 'partial/login');
*
* // Equivalent binding using the "with" method
* View::make('home')->with('login', View::make('partials/login'));
* </code>
*
* @param string $key
* @param string $view
* @param array $data
......@@ -201,11 +241,6 @@ class View {
*
* Bound data will be available to the view as variables.
*
* <code>
* // Bind a "name" value to the view
* View::make('home')->with('name', 'Fred');
* </code>
*
* @param string $key
* @param mixed $value
* @return View
......@@ -248,4 +283,14 @@ class View {
unset($this->data[$key]);
}
/**
* Magic Method for passing undefined static methods to the View_Factory instance
* registered in the application IoC container. This provides easy access to the
* view functions while still maintaining testability within the view classes.
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array(IoC::container()->resolve('laravel.view'), $method), $parameters);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment