Commit 2331ae18 authored by Taylor Otwell's avatar Taylor Otwell

Added lang and view loader events.

Added lang and view loader events similar to the configuration loader.
Signed-off-by: 's avatarTaylor Otwell <taylorotwell@gmail.com>
parent 32d4eeff
...@@ -47,6 +47,41 @@ $aliases = Laravel\Config::get('application.aliases'); ...@@ -47,6 +47,41 @@ $aliases = Laravel\Config::get('application.aliases');
Laravel\Autoloader::$aliases = $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 | Set The Default Timezone
......
...@@ -12,7 +12,7 @@ class Error { ...@@ -12,7 +12,7 @@ class Error {
{ {
static::log($exception); 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 // 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 // a simple error message and display it on the screen. We don't use a
......
...@@ -32,6 +32,13 @@ class Lang { ...@@ -32,6 +32,13 @@ class Lang {
*/ */
protected static $lines = array(); protected static $lines = array();
/**
* The language loader event name.
*
* @var string
*/
const loader = 'laravel.language.loader';
/** /**
* Create a new Lang instance. * Create a new Lang instance.
* *
...@@ -179,6 +186,26 @@ class Lang { ...@@ -179,6 +186,26 @@ class Lang {
return true; 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(); $lines = array();
// Language files can belongs to the application or to any bundle // Language files can belongs to the application or to any bundle
...@@ -191,12 +218,7 @@ class Lang { ...@@ -191,12 +218,7 @@ class Lang {
$lines = require $path; $lines = require $path;
} }
// All of the language lines are cached in an array so we can return $lines;
// 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;
} }
/** /**
......
...@@ -51,6 +51,13 @@ class View implements ArrayAccess { ...@@ -51,6 +51,13 @@ class View implements ArrayAccess {
*/ */
public static $paths = array(DEFAULT_BUNDLE => array('')); 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. * The Laravel view engine event name.
* *
...@@ -106,28 +113,40 @@ class View implements ArrayAccess { ...@@ -106,28 +113,40 @@ class View implements ArrayAccess {
*/ */
protected function path($view) 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 // We delegate the determination of view paths to the view loader
// throw an exception since there is not any point in going further. // event so that the developer is free to override and manage
// If it does, we can just return the full view path. // the loading views in any way they see fit.
$paths = array_get(static::$paths, Bundle::name($view), array('')); $path = Event::first(static::loader, array($bundle, $view));
foreach ($paths as $path) if ( ! is_null($path))
{ {
foreach (static::$extensions as $ext) return $path;
{
$file = $root.$path.Bundle::element($view).$ext;
if (file_exists($file)) return $file;
}
} }
throw new \Exception("View [$view] doesn't exist."); 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. * Create a new view instance.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment