continuing to refactor the bootstrap process.

This commit is contained in:
Taylor Otwell 2011-10-18 21:03:13 -05:00
parent 9fc9f88a41
commit d5d9776298
5 changed files with 117 additions and 61 deletions

View File

@ -0,0 +1,54 @@
<?php
return array(
/*
|--------------------------------------------------------------------------
| Inversion of Control Container
|--------------------------------------------------------------------------
|
| Here you may define resolvers for the Laravel inversion of control (IoC)
| container. An IoC container provides the ability to create more flexible
| and testable applications, as well as a convenient method of managing
| the instantiation of complex objects.
|
| To register a resolver in the container, simple create add an item to
| the array for the object with a closure that returns an instance of
| the object.
|
| For example, here's how to register a resolver for a Mailer class:
|
| 'mailer' => function($c)
| {
| return new Mailer($sender, $key);
| }
|
| Note that the container instance itself is passed into the resolver,
| allowing you to continue to resolve dependencies within the resolver
| itself. This allows you to easily resolve nested dependencies.
|
| When creating controller instances, Laravel will check to see if a
| resolver has been registered for the controller. If it has, it will
| be used to create the controller instance. All controller resolvers
| should be registered beginning using a {controllers}.{name} naming
| convention. For example:
|
| 'controllers.user' => function($c)
| {
| return new User_Controller($c->resolve('repository'));
| }
|
| Of course, sometimes you may wish to register an object as a singleton
| Singletons are resolved by the controller the first time they are
| resolved; however, that same resolved instance will continue to be
| returned by the container each time it is requested. Registering an
| object as a singleton couldn't be simpler:
|
| 'mailer' => array('singleton' => true, 'resolver' => function($c)
| {
| return new Mailer($sender, $key);
| })
|
*/
);

View File

@ -1,8 +1,17 @@
<?php <?php
/**
* Define the extensions used by the framework.
*/
define('EXT', '.php'); define('EXT', '.php');
define('BLADE_EXT', '.blade.php'); define('BLADE_EXT', '.blade.php');
/**
* Define a function that registers an array of constants
* if they haven't already been registered. This allows the
* constants to be changed from their default values when
* unit testing the framework.
*/
function constants($constants) function constants($constants)
{ {
foreach ($constants as $key => $value) foreach ($constants as $key => $value)
@ -11,6 +20,11 @@ function constants($constants)
} }
} }
/**
* Register the core framework paths. All other paths are
* built on top of these core paths. All of these paths are
* changable by the developer in the front controller.
*/
$constants = array( $constants = array(
'APP_PATH' => realpath($application).'/', 'APP_PATH' => realpath($application).'/',
'BASE_PATH' => realpath("$laravel/..").'/', 'BASE_PATH' => realpath("$laravel/..").'/',
@ -22,6 +36,10 @@ function constants($constants)
constants($constants); constants($constants);
/**
* Register all of the other framework paths. All of these
* paths are built on top of the core paths above.
*/
$constants = array( $constants = array(
'CACHE_PATH' => STORAGE_PATH.'cache/', 'CACHE_PATH' => STORAGE_PATH.'cache/',
'CONFIG_PATH' => APP_PATH.'config/', 'CONFIG_PATH' => APP_PATH.'config/',
@ -42,3 +60,14 @@ function constants($constants)
constants($constants); constants($constants);
unset($constants); unset($constants);
/**
* Set the Laravel environment configuration path constant.
* The environment is controller by setting an environment
* variable on the server running Laravel.
*/
$environment = (isset($_SERVER['LARAVEL_ENV'])) ? $_SERVER['LARAVEL_ENV'] : '';
define('ENV_CONFIG_PATH', $environment);
unset($environment);

View File

@ -11,49 +11,14 @@
require SYS_PATH.'config'.EXT; require SYS_PATH.'config'.EXT;
require SYS_PATH.'loader'.EXT; require SYS_PATH.'loader'.EXT;
/**
* If a Laravel environment has been specified on the server, we will
* add a path to the configuration manager for the environment.
*/
if (isset($_SERVER['LARAVEL_ENV']))
{
define('ENV_CONFIG_PATH', CONFIG_PATH.$_SERVER['LARAVEL_ENV'].'/');
Config::glance(ENV_CONFIG_PATH);
}
/** /**
* Load some core configuration files by default so we don't have to * Load some core configuration files by default so we don't have to
* let them fall through the Config loader. This will allow us to * let them fall through the Config parser. This will allow us to
* load these files faster for each request. * load these files faster for each request.
*/ */
foreach (array('application', 'session') as $file) Config::load('application');
{ Config::load('container');
$config = require CONFIG_PATH.$file.EXT; Config::load('session');
if (isset($_SERVER['LARAVEL_ENV']) and file_exists($path = ENV_CONFIG_PATH.$file.EXT))
{
$config = array_merge($config, require $path);
}
Config::$items[$file] = $config;
}
/**
* Load the container configuration into the Config class. We load
* this file manually to avoid the overhead of Config::load.
*/
Config::$items['container'] = require SYS_CONFIG_PATH.'container'.EXT;
if (file_exists($path = CONFIG_PATH.'container'.EXT))
{
Config::$items['container'] = array_merge(Config::$items['container'], require $path);
}
if (isset($_SERVER['LARAVEL_ENV']) and file_exists($path = ENV_CONFIG_PATH.'container'.EXT))
{
Config::$items['container'] = array_merge(Config::$items['container'], require $path);
}
/** /**
* Bootstrap the application inversion of control (IoC) container. * Bootstrap the application inversion of control (IoC) container.
@ -67,7 +32,7 @@
IoC::$container = $container; IoC::$container = $container;
unset($config, $container); unset($container);
/** /**
* Register the application auto-loader. The auto-loader is responsible * Register the application auto-loader. The auto-loader is responsible
@ -79,7 +44,9 @@
Loader::$aliases = Config::$items['application']['aliases']; Loader::$aliases = Config::$items['application']['aliases'];
/** /**
* Define a few convenient global functions. * Define a few convenient global functions. These functions primarily
* exists to provide a short way of accessing functions commonly used
* in views, allowing the reduction of code noise.
*/ */
function e($value) function e($value)
{ {

View File

@ -1,11 +1,23 @@
<?php namespace Laravel; <?php namespace Laravel;
/** /**
* Create the exception formatter closure. This function will format * Define a closure that will return a formatted error message
* the exception message and severity for display and return the two * when given an exception. This function will be used by the
* formatted strings in an array. * error handler to create a more readable message.
*/ */
$formatter = function($e) $message = function($e)
{
$file = str_replace(array(APP_PATH, SYS_PATH), array('APP_PATH/', 'SYS_PATH/'), $e->getFile());
return rtrim($e->getMessage(), '.').' in '.$file.' on line '.$e->getLine().'.';
};
/**
* Define a clousre that will return a more readable version
* of the severity of an exception. This function will be used
* by the error handler when parsing exceptions.
*/
$severity = function($e)
{ {
$levels = array( $levels = array(
0 => 'Error', 0 => 'Error',
@ -23,31 +35,25 @@
E_STRICT => 'Runtime Notice', E_STRICT => 'Runtime Notice',
); );
$file = str_replace(array(APP_PATH, SYS_PATH), array('APP_PATH/', 'SYS_PATH/'), $e->getFile()); return (array_key_exists($e->getCode(), $levels)) ? $levels[$e->getCode()] : $e->getCode();
$message = rtrim($e->getMessage(), '.').' in '.$file.' on line '.$e->getLine().'.';
$severity = (array_key_exists($e->getCode(), $levels)) ? $levels[$e->getCode()] : $e->getCode();
return array($severity, $message);
}; };
/** /**
* Create the exception handler function. All of the error handlers * Create the exception handler function. All of the error handlers
* registered with PHP call this closure to keep the code D.R.Y. * registered with PHP call this closure to keep the code D.R.Y.
* Each of the formatting closures defined above will be passed
* into the handler for convenient use.
*/ */
$handler = function($e) use ($formatter) $handler = function($e) use ($message, $severity)
{ {
list($severity, $message) = $formatter($e);
$config = Config::get('error'); $config = Config::get('error');
if ($config['log']) if ($config['log'])
{ {
call_user_func($config['logger'], $e, $severity, $message, $config); call_user_func($config['logger'], $e, $severity($e), $message($e), $config);
} }
call_user_func($config['handler'], $e, $severity, $message, $config); call_user_func($config['handler'], $e, $severity($e), $message($e), $config);
exit(1); exit(1);
}; };

View File

@ -16,7 +16,7 @@ class Config {
* *
* @var array * @var array
*/ */
public static $paths = array(SYS_CONFIG_PATH, CONFIG_PATH); public static $paths = array(SYS_CONFIG_PATH, CONFIG_PATH, ENV_CONFIG_PATH);
/** /**
* Determine if a configuration item or file exists. * Determine if a configuration item or file exists.
@ -130,7 +130,7 @@ protected static function parse($key)
* @param string $file * @param string $file
* @return bool * @return bool
*/ */
protected static function load($file) public static function load($file)
{ {
if (isset(static::$items[$file])) return true; if (isset(static::$items[$file])) return true;
@ -141,7 +141,7 @@ protected static function load($file)
// cascading of configuration options from system to application. // cascading of configuration options from system to application.
foreach (static::$paths as $directory) foreach (static::$paths as $directory)
{ {
if (file_exists($path = $directory.$file.EXT)) if ($directory !== '' and file_exists($path = $directory.$file.EXT))
{ {
$config = array_merge($config, require $path); $config = array_merge($config, require $path);
} }