diff --git a/laravel/config/container.php b/laravel/config/container.php index 5cd9d163..abbe4171 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -107,7 +107,7 @@ { 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)); }), /* diff --git a/laravel/database/manager.php b/laravel/database/manager.php index 510e0ebe..820ffa92 100644 --- a/laravel/database/manager.php +++ b/laravel/database/manager.php @@ -40,14 +40,6 @@ public function __construct($config, $default) * * Note: Database connections are managed as singletons. * - * - * // Get the default database connection - * $connection = DB::connection(); - * - * // Get a specific database connection - * $connection = DB::connection('mysql'); - * - * * @param string $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. * - * - * // 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'); - * - * * @param string $table * @param string $connection * @return Database\Query @@ -99,14 +80,6 @@ public function table($table, $connection = null) * Magic Method for calling methods on the default database connection. * * This provides a convenient API for querying or examining the default database connection. - * - * - * // 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'); - * */ public function __call($method, $parameters) { diff --git a/laravel/laravel.php b/laravel/laravel.php index 94dd22a8..09617eee 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -8,7 +8,7 @@ // -------------------------------------------------------------- // Set the error reporting and display levels. // -------------------------------------------------------------- -error_reporting(E_ALL | E_STRICT); +error_reporting(-1); ini_set('display_errors', 'Off'); diff --git a/laravel/view.php b/laravel/view.php index 93d0f0f1..07fba57d 100644 --- a/laravel/view.php +++ b/laravel/view.php @@ -1,5 +1,62 @@ 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 @@ public function __construct($composers, $path) */ 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()) { - 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($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. * @@ -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 { /** @@ -77,13 +147,6 @@ class View { */ public $view; - /** - * The view name with dots replaced by slashes. - * - * @var string - */ - public $path; - /** * The view data. * @@ -92,11 +155,18 @@ class View { 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. @@ -108,19 +178,20 @@ class View { /** * Create a new view instance. * - * @param string $view - * @param array $data - * @param string $path - * @param array $composers + * @param string $view + * @param array $data + * @param string $path + * @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)) { @@ -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. * @@ -154,7 +209,7 @@ protected function compose() */ public function render() { - $this->compose(); + $this->composer->compose($this); foreach ($this->data as &$data) { @@ -163,14 +218,7 @@ public function render() 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 @@ public function render() /** * Add a view instance to the view data. * - * - * // 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')); - * - * * @param string $key * @param string $view * @param array $data @@ -201,11 +241,6 @@ public function partial($key, $view, $data = array()) * * Bound data will be available to the view as variables. * - * - * // Bind a "name" value to the view - * View::make('home')->with('name', 'Fred'); - * - * * @param string $key * @param mixed $value * @return View @@ -248,4 +283,14 @@ public function __unset($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); + } + } \ No newline at end of file