revert back to more sensible architecture.

This commit is contained in:
Taylor Otwell
2011-09-20 23:14:09 -05:00
parent 47db2ff19b
commit 4525eae25a
33 changed files with 1050 additions and 1558 deletions

View File

@@ -1,199 +1,5 @@
<?php namespace Laravel;
/**
* 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 {
/**
* The view composer instance.
*
* @var View_Composer
*/
protected $composer;
/**
* The directory containing the views.
*
* @var string
*/
protected $path;
/**
* Create a new view factory instance.
*
* @param View_Composer $composer
* @param string $path
* @return void
*/
public function __construct(View_Composer $composer, $path)
{
$this->composer = $composer;
$this->path = $path;
}
/**
* Create a new view instance.
*
* 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.
*
* <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' => 'Fred'));
* </code>
*
* @param string $view
* @param array $data
* @return View
*/
public function make($view, $data = array())
{
return new View($this, $this->composer, $view, $data, $this->path($view));
}
/**
* Create a new view instance from a view name.
*
* View names are defined in the application composers file.
*
* <code>
* // Create a new named view instance
* $view = View::of('layout');
*
* // Create a new named view instance with bound data
* $view = View::of('layout', array('name' => 'Fred'));
* </code>
*
* @param string $name
* @param array $data
* @return View
*/
protected function of($name, $data = array())
{
if ( ! is_null($view = $this->composer->name($name)))
{
return $this->make($view, $data);
}
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)
{
$view = str_replace('.', '/', $view);
if (file_exists($path = $this->path.$view.'.blade'.EXT))
{
return $path;
}
elseif (file_exists($path = $this->path.$view.EXT))
{
return $path;
}
throw new \Exception('View ['.$view.'] does not exist.');
}
/**
* Magic Method for handling the dynamic creation of named views.
*
* <code>
* // Create an instance of the "layout" named view
* $view = View::of_layout();
*
* // Create an instance of the "layout" named view with bound data
* $view = View::of_layout(array('name' => 'Fred'));
* </code>
*/
public function __call($method, $parameters)
{
if (strpos($method, 'of_') === 0)
{
return $this->of(substr($method, 3), Arr::get($parameters, 0, array()));
}
}
}
/**
* 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 {
/**
* 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.
*
* @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['shared'])) call_user_func($this->composers['shared'], $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 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 {
/**
@@ -218,36 +24,78 @@ class View {
protected $path;
/**
* The view composer instance.
* Create a new view instance.
*
* @var View_Composer
* @param string $view
* @param array $data
* @return void
*/
protected $composer;
/**
* The view factory instance, which is used to create sub-views.
*
* @var View_Factory
*/
protected $factory;
protected function __construct($view, $data = array())
{
$this->view = $view;
$this->data = $data;
$this->path = $this->path($view);
}
/**
* Create a new view instance.
*
* @param View_Factory $factory
* @param View_Composer $composer
* @param string $view
* @param array $data
* @param string $path
* @return void
* @return View
*/
public function __construct(View_Factory $factory, View_Composer $composer, $view, $data, $path)
public static function make($view, $data = array())
{
$this->view = $view;
$this->data = $data;
$this->path = $path;
$this->factory = $factory;
$this->composer = $composer;
return new static($view, $data);
}
/**
* Create a new view instance from a view name.
*
* View names are defined in the application composers file.
*
* <code>
* // Create a new named view instance
* $view = View::of('layout');
*
* // Create a new named view instance with bound data
* $view = View::of('layout', array('name' => 'Fred'));
* </code>
*
* @param string $name
* @param array $data
* @return View
*/
public static function of($name, $data = array())
{
if ( ! is_null($view = Composer::name($name)))
{
return new static($view, $data);
}
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)
{
$view = str_replace('.', '/', $view);
if (file_exists($path = VIEW_PATH.$view.'.blade'.EXT))
{
return $path;
}
elseif (file_exists($path = VIEW_PATH.$view.EXT))
{
return $path;
}
throw new \Exception('View ['.$view.'] does not exist.');
}
/**
@@ -260,7 +108,7 @@ class View {
*/
public function render()
{
$this->composer->compose($this);
Composer::compose($this);
foreach ($this->data as &$data)
{
@@ -304,7 +152,7 @@ class View {
*/
public function partial($key, $view, $data = array())
{
return $this->with($key, $this->factory->make($view, $data));
return $this->with($key, new static($view, $data));
}
/**
@@ -359,4 +207,76 @@ class View {
unset($this->data[$key]);
}
}
/**
* Magic Method for handling the dynamic creation of named views.
*
* <code>
* // Create an instance of the "layout" named view
* $view = View::of_layout();
*
* // Create an instance of the "layout" named view with bound data
* $view = View::of_layout(array('name' => 'Fred'));
* </code>
*/
public static function __callStatic($method, $parameters)
{
if (strpos($method, 'of_') === 0)
{
return static::of(substr($method, 3), Arr::get($parameters, 0, array()));
}
}
}
/**
* The view composer class is responsible for calling the composer on a view and
* searching through the view composers for a given view name.
*/
class Composer {
/**
* The view composers.
*
* @var array
*/
public static $composers;
/**
* Find the key for a view by name.
*
* @param string $name
* @return string
*/
public static function name($name)
{
foreach (static::$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 static function compose(View $view)
{
if (isset(static::$composers['shared'])) call_user_func(static::$composers['shared'], $view);
if (isset(static::$composers[$view->view]))
{
foreach ((array) static::$composers[$view->view] as $key => $value)
{
if ($value instanceof \Closure) return call_user_func($value, $view);
}
}
}
}
/**
* Load the application's composers into the composers property.
*/
Composer::$composers = require APP_PATH.'composers'.EXT;