merged skunkworks into develop.

This commit is contained in:
Taylor Otwell
2012-01-16 13:59:24 -06:00
parent 610d8827c4
commit b5442c67fc
117 changed files with 7268 additions and 3999 deletions

View File

@@ -1,4 +1,4 @@
<?php namespace Laravel;
<?php namespace Laravel; use Closure;
class IoC {
@@ -7,49 +7,25 @@ class IoC {
*
* @var array
*/
protected static $registry = array();
public static $registry = array();
/**
* The resolved singleton instances.
*
* @var array
*/
protected static $singletons = array();
/**
* Bootstrap the application IoC container.
*
* This method is called automatically the first time the class is loaded.
*
* @param array $registry
* @return void
*/
public static function bootstrap($registry = array())
{
if (Config::load('container'))
{
static::$registry = Config::$items['container'];
}
}
public static $singletons = array();
/**
* Register an object and its resolver.
*
* The IoC container instance is always passed to the resolver, allowing the
* nested resolution of other objects from the container.
*
* <code>
* // Register an object and its resolver
* IoC::register('mailer', function($c) {return new Mailer;});
* </code>
*
* @param string $name
* @param Closure $resolver
* @return void
*/
public static function register($name, $resolver, $singleton = false)
public static function register($name, Closure $resolver, $singleton = false)
{
static::$registry[$name] = array('resolver' => $resolver, 'singleton' => $singleton);
static::$registry[$name] = compact('resolver', 'singleton');
}
/**
@@ -67,7 +43,6 @@ class IoC {
* Register an object as a singleton.
*
* Singletons will only be instantiated the first time they are resolved.
* The same instance will be returned on subsequent requests.
*
* @param string $name
* @param Closure $resolver
@@ -79,10 +54,7 @@ class IoC {
}
/**
* Register an instance as a singleton.
*
* This method allows you to register an already existing object instance
* with the container to be managed as a singleton instance.
* Register an existing instance as a singleton.
*
* <code>
* // Register an instance as a singleton in the container
@@ -105,11 +77,8 @@ class IoC {
* // Resolve the "laravel.router" class from the container
* $input = IoC::core('router');
*
* // Equivalent resolution using the "resolve" method
* // Equivalent resolution of the router using the "resolve" method
* $input = IoC::resolve('laravel.router');
*
* // Pass an array of parameters to the resolver
* $input = IoC::core('router', array('test'));
* </code>
*
* @param string $name
@@ -128,7 +97,7 @@ class IoC {
* // Get an instance of the "mailer" object registered in the container
* $mailer = IoC::resolve('mailer');
*
* // Pass an array of parameters to the resolver
* // Get an instance of the "mailer" object and pass parameters to the resolver
* $mailer = IoC::resolve('mailer', array('test'));
* </code>
*
@@ -145,12 +114,19 @@ class IoC {
if ( ! static::registered($name))
{
throw new \OutOfBoundsException("Error resolving [$name]. No resolver has been registered.");
throw new \Exception("Error resolving [$name]. No resolver has been registered.");
}
$object = call_user_func(static::$registry[$name]['resolver'], $parameters);
if (isset(static::$registry[$name]['singleton']) and static::$registry[$name]['singleton'])
// If the resolver is registering as a singleton resolver, we will cache
// the instance of the object in the container so we can resolve it next
// time without having to instantiate a new instance of the object.
//
// This allows the developer to reuse objects that do not need to be
// instantiated each time they are needed, such as a SwiftMailer or
// Twig object that can be shared.
if (isset(static::$registry[$name]['singleton']))
{
return static::$singletons[$name] = $object;
}
@@ -158,11 +134,4 @@ class IoC {
return $object;
}
}
/**
* We only bootstrap the IoC container once the class has been
* loaded since there isn't any reason to load the container
* configuration until the class is first requested.
*/
IoC::bootstrap();
}