Browse Source

Added lang and view loader events.

Added lang and view loader events similar to the configuration loader.

Signed-off-by: Taylor Otwell <taylorotwell@gmail.com>
Taylor Otwell 12 years ago
parent
commit
2331ae18cf
4 changed files with 96 additions and 20 deletions
  1. 35 0
      application/start.php
  2. 1 1
      laravel/error.php
  3. 28 6
      laravel/lang.php
  4. 32 13
      laravel/view.php

+ 35 - 0
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

+ 1 - 1
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

+ 28 - 6
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;
 	}
 
 	/**

+ 32 - 13
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)
 	{
-		$view = str_replace('.', '/', $view);
+		list($bundle, $view) = Bundle::parse($view);
 
-		$root = Bundle::path(Bundle::name($view)).'views/';
+		$view = str_replace('.', '/', $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(''));
+		// 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));
 
-		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.
 	 *