refactoring the view classes.

This commit is contained in:
Taylor Otwell 2011-08-29 00:07:10 -05:00
parent 56044d31f5
commit 9be3d1a54b
4 changed files with 110 additions and 92 deletions

View File

@ -107,7 +107,7 @@
{ {
require_once SYS_PATH.'view'.EXT; 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));
}), }),
/* /*

View File

@ -40,14 +40,6 @@ public function __construct($config, $default)
* *
* Note: Database connections are managed as singletons. * 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 * @param string $connection
* @return Database\Connection * @return Database\Connection
*/ */
@ -75,17 +67,6 @@ public function connection($connection = null)
* *
* This method primarily serves as a short-cut to the $connection->table() method. * 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 $table
* @param string $connection * @param string $connection
* @return Database\Query * @return Database\Query
@ -99,14 +80,6 @@ public function table($table, $connection = null)
* Magic Method for calling methods on the default database connection. * Magic Method for calling methods on the default database connection.
* *
* This provides a convenient API for querying or examining 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) public function __call($method, $parameters)
{ {

View File

@ -8,7 +8,7 @@
// -------------------------------------------------------------- // --------------------------------------------------------------
// Set the error reporting and display levels. // Set the error reporting and display levels.
// -------------------------------------------------------------- // --------------------------------------------------------------
error_reporting(E_ALL | E_STRICT); error_reporting(-1);
ini_set('display_errors', 'Off'); ini_set('display_errors', 'Off');

View File

@ -1,5 +1,62 @@
<?php namespace Laravel; <?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 { class View_Factory {
/** /**
@ -9,10 +66,10 @@ class View_Factory {
* @param string $path * @param string $path
* @return void * @return void
*/ */
public function __construct($composers, $path) public function __construct($path, View_Composer $composer)
{ {
$this->path = $path; $this->path = $path;
$this->composers = $composers; $this->composer = $composer;
} }
/** /**
@ -24,7 +81,7 @@ public function __construct($composers, $path)
*/ */
public function make($view, $data = array()) 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,17 +93,25 @@ public function make($view, $data = array())
*/ */
protected function of($name, $data = array()) protected function of($name, $data = array())
{ {
foreach ($this->composers as $key => $value) if ( ! is_null($view = $this->composer->name($name)))
{ {
if ($name === $value or (isset($value['name']) and $name === $value['name'])) return new View($view, $data, $this->path($view), $this->composer, $this);
{
return new View($key, $this->path, $data, $this->composers, $this);
}
} }
throw new \Exception("Named view [$name] is not defined."); 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. * Magic Method for handling the dynamic creation of named views.
* *
@ -68,6 +133,11 @@ public function __call($method, $parameters)
} }
/**
* 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 { class View {
/** /**
@ -77,13 +147,6 @@ class View {
*/ */
public $view; public $view;
/**
* The view name with dots replaced by slashes.
*
* @var string
*/
public $path;
/** /**
* The view data. * The view data.
* *
@ -92,11 +155,18 @@ class View {
public $data; public $data;
/** /**
* The view composers defined for the application. * The path to the view on disk.
* *
* @var array $composers * @var string
*/ */
protected $composers; protected $path;
/**
* The view composer instance.
*
* @var View_Composer
*/
protected $composer;
/** /**
* The view factory instance, which is used to create sub-views. * The view factory instance, which is used to create sub-views.
@ -108,19 +178,20 @@ class View {
/** /**
* Create a new view instance. * Create a new view instance.
* *
* @param string $view * @param string $view
* @param array $data * @param array $data
* @param string $path * @param string $path
* @param array $composers * @param View_Composer $composer
* @param View_Factory $factory
* @return void * @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->view = $view;
$this->data = $data; $this->data = $data;
$this->path = $path;
$this->factory = $factory; $this->factory = $factory;
$this->composers = $composers; $this->composer = $composer;
$this->path = $path.str_replace('.', '/', $view).EXT;
if ( ! file_exists($this->path)) if ( ! file_exists($this->path))
{ {
@ -128,22 +199,6 @@ public function __construct($view, $path, $data, $composers, $factory)
} }
} }
/**
* 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. * Get the evaluated string content of the view.
* *
@ -154,7 +209,7 @@ protected function compose()
*/ */
public function render() public function render()
{ {
$this->compose(); $this->composer->compose($this);
foreach ($this->data as &$data) foreach ($this->data as &$data)
{ {
@ -163,14 +218,7 @@ public function render()
ob_start() and extract($this->data, EXTR_SKIP); ob_start() and extract($this->data, EXTR_SKIP);
try try { include $this->path; } catch (\Exception $e) { ob_get_clean(); throw $e; }
{
include $this->path;
}
catch (\Exception $e)
{
Exception\Handler::make(new Exception\Examiner($e))->handle();
}
return ob_get_clean(); return ob_get_clean();
} }
@ -178,14 +226,6 @@ public function render()
/** /**
* Add a view instance to the view data. * 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 $key
* @param string $view * @param string $view
* @param array $data * @param array $data
@ -201,11 +241,6 @@ public function partial($key, $view, $data = array())
* *
* Bound data will be available to the view as variables. * 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 string $key
* @param mixed $value * @param mixed $value
* @return View * @return View
@ -248,4 +283,14 @@ public function __unset($key)
unset($this->data[$key]); 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);
}
} }