From 2331ae18cfa1bb7c5b4d4986b9b809074b31bec3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 1 Mar 2012 11:52:05 -0600 Subject: [PATCH] Added lang and view loader events. Added lang and view loader events similar to the configuration loader. Signed-off-by: Taylor Otwell --- application/start.php | 35 +++++++++++++++++++++++++++++++++ laravel/error.php | 2 +- laravel/lang.php | 34 ++++++++++++++++++++++++++------ laravel/view.php | 45 ++++++++++++++++++++++++++++++------------- 4 files changed, 96 insertions(+), 20 deletions(-) diff --git a/application/start.php b/application/start.php index 38fba8e1..ff0336cb 100644 --- a/application/start.php +++ b/application/start.php @@ -47,6 +47,41 @@ $aliases = Laravel\Config::get('application.aliases'); Laravel\Autoloader::$aliases = $aliases; +/* +|-------------------------------------------------------------------------- +| Laravel View Loader +|-------------------------------------------------------------------------- +| +| The Laravel view loader is responsible for returning the full file path +| for the given bundle and view. Of course, a default implementation is +| provided to load views according to typical Laravel conventions but +| you may change this to customize how your views are organized. +| +*/ + +Event::listen(View::loader, function($bundle, $view) +{ + return View::file($bundle, $view); +}); + +/* +|-------------------------------------------------------------------------- +| Laravel Language Loader +|-------------------------------------------------------------------------- +| +| The Laravel language loader is responsible for returning the array of +| language lines for a given bundle, language, and "file". A default +| implementation has been provided which uses the default language +| directories included with Laravel. However, you may tweak this +| method to laad your language arrays however you wish. +| +*/ + +Event::listen(Lang::loader, function($bundle, $language, $file) +{ + return Lang::file($bundle, $language, $file); +}); + /* |-------------------------------------------------------------------------- | Set The Default Timezone diff --git a/laravel/error.php b/laravel/error.php index 23574efa..bf668d8a 100644 --- a/laravel/error.php +++ b/laravel/error.php @@ -12,7 +12,7 @@ class Error { { static::log($exception); - ob_end_clean(); + ob_get_level() and ob_end_clean(); // If detailed errors are enabled, we'll just format the exception into // a simple error message and display it on the screen. We don't use a diff --git a/laravel/lang.php b/laravel/lang.php index 7840dab0..87be5f3d 100644 --- a/laravel/lang.php +++ b/laravel/lang.php @@ -32,6 +32,13 @@ class Lang { */ protected static $lines = array(); + /** + * The language loader event name. + * + * @var string + */ + const loader = 'laravel.language.loader'; + /** * Create a new Lang instance. * @@ -179,6 +186,26 @@ class Lang { return true; } + // We use a "loader" event to delegate the loading of the language + // array, which allows the develop to organize the language line + // arrays for their application however they wish. + $lines = Event::first(static::loader, func_get_args()); + + static::$lines[$bundle][$language][$file] = $lines; + + return count($lines) > 0; + } + + /** + * Load a language array from a language file. + * + * @param string $bundle + * @param string $language + * @param string $file + * @return array + */ + public static function file($bundle, $language, $file) + { $lines = array(); // Language files can belongs to the application or to any bundle @@ -191,12 +218,7 @@ class Lang { $lines = require $path; } - // All of the language lines are cached in an array so we can - // quickly look them up on subsequent reqwuests for the line. - // This keeps us from loading files each time. - static::$lines[$bundle][$language][$file] = $lines; - - return count($lines) > 0; + return $lines; } /** diff --git a/laravel/view.php b/laravel/view.php index 205129d9..4eddb64a 100644 --- a/laravel/view.php +++ b/laravel/view.php @@ -51,6 +51,13 @@ class View implements ArrayAccess { */ public static $paths = array(DEFAULT_BUNDLE => array('')); + /** + * The Laravel view loader event name. + * + * @var string + */ + const loader = 'laravel.view.loader'; + /** * The Laravel view engine event name. * @@ -106,28 +113,40 @@ class View implements ArrayAccess { */ protected function path($view) { + list($bundle, $view) = Bundle::parse($view); + $view = str_replace('.', '/', $view); - $root = Bundle::path(Bundle::name($view)).'views/'; + // We delegate the determination of view paths to the view loader + // event so that the developer is free to override and manage + // the loading views in any way they see fit. + $path = Event::first(static::loader, array($bundle, $view)); - // We need to make sure that the view exists. If it doesn't, we will - // throw an exception since there is not any point in going further. - // If it does, we can just return the full view path. - $paths = array_get(static::$paths, Bundle::name($view), array('')); - - foreach ($paths as $path) + if ( ! is_null($path)) { - foreach (static::$extensions as $ext) - { - $file = $root.$path.Bundle::element($view).$ext; - - if (file_exists($file)) return $file; - } + return $path; } throw new \Exception("View [$view] doesn't exist."); } + /** + * Get the path to a view using the default folder convention. + * + * @param string $bundle + * @param string $view + * @return string + */ + public static function file($bundle, $view) + { + $root = Bundle::path($bundle).'views/'; + + if (file_exists($path = $root.$view.EXT)) + { + return $path; + } + } + /** * Create a new view instance. *