Commit d5d97762 by Taylor Otwell

continuing to refactor the bootstrap process.

parent 9fc9f88a
<?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);
| })
|
*/
);
\ No newline at end of file
<?php
/**
* Define the extensions used by the framework.
*/
define('EXT', '.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)
{
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(
'APP_PATH' => realpath($application).'/',
'BASE_PATH' => realpath("$laravel/..").'/',
......@@ -22,6 +36,10 @@ $constants = array(
constants($constants);
/**
* Register all of the other framework paths. All of these
* paths are built on top of the core paths above.
*/
$constants = array(
'CACHE_PATH' => STORAGE_PATH.'cache/',
'CONFIG_PATH' => APP_PATH.'config/',
......@@ -41,4 +59,15 @@ $constants = array(
constants($constants);
unset($constants);
\ No newline at end of file
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);
\ No newline at end of file
......@@ -12,48 +12,13 @@ require SYS_PATH.'config'.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
* 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.
*/
foreach (array('application', 'session') as $file)
{
$config = require CONFIG_PATH.$file.EXT;
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);
}
Config::load('application');
Config::load('container');
Config::load('session');
/**
* Bootstrap the application inversion of control (IoC) container.
......@@ -67,7 +32,7 @@ $container = new Container(Config::$items['container']);
IoC::$container = $container;
unset($config, $container);
unset($container);
/**
* Register the application auto-loader. The auto-loader is responsible
......@@ -79,7 +44,9 @@ spl_autoload_register(array('Laravel\\Loader', 'load'));
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)
{
......
<?php namespace Laravel;
/**
* Create the exception formatter closure. This function will format
* the exception message and severity for display and return the two
* formatted strings in an array.
* Define a closure that will return a formatted error message
* when given an exception. This function will be used by the
* 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(
0 => 'Error',
......@@ -23,31 +35,25 @@ $formatter = function($e)
E_STRICT => 'Runtime Notice',
);
$file = str_replace(array(APP_PATH, SYS_PATH), array('APP_PATH/', 'SYS_PATH/'), $e->getFile());
$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);
return (array_key_exists($e->getCode(), $levels)) ? $levels[$e->getCode()] : $e->getCode();
};
/**
* Create the exception handler function. All of the error handlers
* 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');
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);
};
......
......@@ -16,7 +16,7 @@ class Config {
*
* @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.
......@@ -130,7 +130,7 @@ class Config {
* @param string $file
* @return bool
*/
protected static function load($file)
public static function load($file)
{
if (isset(static::$items[$file])) return true;
......@@ -141,7 +141,7 @@ class Config {
// cascading of configuration options from system to application.
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);
}
......
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