Files
spa-api/laravel/view.php

222 lines
4.4 KiB
PHP
Raw Normal View History

2011-08-18 20:21:37 -05:00
<?php namespace Laravel;
2011-06-08 23:45:08 -05:00
class View implements Renderable {
2011-06-08 23:45:08 -05:00
/**
* The name of the view.
*
* @var string
*/
public $view;
/**
* The view name with dots replaced by slashes.
*
* @var string
*/
protected $path;
2011-06-08 23:45:08 -05:00
/**
* The view data.
*
* @var array
*/
public $data = array();
/**
* Create a new view instance.
*
* @param string $view
* @param array $data
* @param string $path
2011-06-08 23:45:08 -05:00
* @return void
*/
public function __construct($view, $data = array(), $path = VIEW_PATH)
2011-06-08 23:45:08 -05:00
{
$this->view = $view;
2011-06-08 23:45:08 -05:00
$this->data = $data;
$this->path = $path.str_replace('.', '/', $view).EXT;
if ( ! file_exists($this->path))
{
throw new \Exception('View ['.$this->path.'] does not exist.');
}
2011-06-08 23:45:08 -05:00
}
/**
* Create a new view instance.
*
* @param string $view
* @param array $data
* @param string $path
2011-06-08 23:45:08 -05:00
* @return View
*/
public static function make($view, $data = array(), $path = VIEW_PATH)
2011-06-08 23:45:08 -05:00
{
return new static($view, $data, $path);
2011-08-03 22:10:07 -05:00
}
2011-08-05 11:11:26 -05:00
/**
* Create a new view instance from a view name.
*
* @param string $name
* @param array $data
* @return View
*/
protected static function of($name, $data = array())
2011-08-05 11:11:26 -05:00
{
$composers = IoC::container()->resolve('laravel.composers');
2011-08-05 11:11:26 -05:00
foreach ($composers as $key => $value)
{
if ($name === $value or (isset($value['name']) and $name === $value['name']))
2011-08-05 11:11:26 -05:00
{
return new static($key, $data);
2011-08-05 11:11:26 -05:00
}
}
throw new \Exception("Named view [$name] is not defined.");
}
/**
2011-08-03 22:10:07 -05:00
* Call the composer for the view instance.
*
2011-08-03 22:10:07 -05:00
* @return void
*/
protected function compose()
{
$composers = IoC::container()->resolve('laravel.composers');
2011-08-05 11:11:26 -05:00
if (isset($composers[$this->view]))
{
foreach ((array) $composers[$this->view] as $key => $value)
2011-08-05 11:11:26 -05:00
{
2011-08-08 15:16:21 -05:00
if (is_callable($value)) return call_user_func($value, $this);
2011-08-05 11:11:26 -05:00
}
}
2011-08-05 11:11:26 -05:00
}
2011-08-05 11:11:26 -05:00
/**
* Get the evaluated string content of the view.
2011-06-08 23:45:08 -05:00
*
* If the view has a composer, it will be executed. All sub-views and responses will
* also be evaluated and converted to their string values.
*
2011-06-08 23:45:08 -05:00
* @return string
*/
public function render()
2011-06-08 23:45:08 -05:00
{
$this->compose();
2011-08-05 13:21:31 -05:00
foreach ($this->data as &$data)
{
if ($data instanceof Renderable) $data = $data->render();
}
2011-08-08 14:54:54 -05:00
ob_start() and extract($this->data, EXTR_SKIP);
2011-06-08 23:45:08 -05:00
try
{
include $this->path;
}
catch (\Exception $e)
{
Exception\Handler::make(new Exception\Examiner($e, new File))->handle();
}
2011-06-08 23:45:08 -05:00
return ob_get_clean();
}
/**
* 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
* @return View
*/
public function partial($key, $view, $data = array())
{
return $this->with($key, new static($view, $data));
}
/**
* Add a key / value pair to the view data.
*
* 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
*/
public function with($key, $value)
{
$this->data[$key] = $value;
return $this;
}
2011-08-05 11:12:15 -05:00
/**
* Magic Method for handling the dynamic creation of named views.
*
* <code>
* // Create an instance of the "login" named view
* $view = View::of_login();
*
* // Create an instance of the "login" named view and bind data to the view
* $view = View::of_login(array('name' => 'Fred'));
* </code>
2011-08-05 11:12:15 -05:00
*/
public static function __callStatic($method, $parameters)
{
if (strpos($method, 'of_') === 0)
{
2011-08-05 11:31:12 -05:00
return static::of(substr($method, 3), Arr::get($parameters, 0, array()));
2011-08-05 11:12:15 -05:00
}
}
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]);
}
}