Files
spa-api/laravel/ioc.php

168 lines
3.9 KiB
PHP
Raw Normal View History

<?php namespace Laravel;
class IoC {
/**
* The registered dependencies.
*
* @var array
*/
protected static $registry = array();
/**
* The resolved singleton instances.
*
* @var array
*/
protected static $singletons = array();
2011-08-26 21:42:04 -05:00
/**
* Bootstrap the application IoC container.
2011-08-26 21:42:04 -05:00
*
2011-11-14 21:18:18 -06:00
* This method is called automatically the first time the class is loaded.
*
2011-09-08 17:49:16 -05:00
* @param array $registry
2011-08-26 21:42:04 -05:00
* @return void
*/
public static function bootstrap($registry = array())
2011-08-26 21:42:04 -05:00
{
2011-11-11 21:42:36 -06:00
if (Config::load('container'))
{
static::$registry = Config::$items['container'];
}
2011-08-26 21:42:04 -05:00
}
/**
2011-09-16 20:30:22 -05:00
* Register an object and its resolver.
*
2011-09-21 22:39:02 -05:00
* 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
2011-11-11 09:52:30 +00:00
* IoC::register('mailer', function($c) {return new Mailer;});
2011-09-21 22:39:02 -05:00
* </code>
*
* @param string $name
* @param Closure $resolver
* @return void
*/
public static function register($name, $resolver, $singleton = false)
{
static::$registry[$name] = array('resolver' => $resolver, 'singleton' => $singleton);
}
/**
2011-09-16 20:30:22 -05:00
* Determine if an object has been registered in the container.
*
* @param string $name
* @return bool
*/
public static function registered($name)
{
return array_key_exists($name, static::$registry);
}
/**
2011-09-16 20:30:22 -05:00
* Register an object as a singleton.
*
2011-09-21 21:46:16 -05:00
* Singletons will only be instantiated the first time they are resolved.
* On subsequent requests for the object, the original instance will be returned.
2011-09-08 17:49:16 -05:00
*
* @param string $name
* @param Closure $resolver
* @return void
*/
public static function singleton($name, $resolver)
{
static::register($name, $resolver, true);
}
/**
* Register an instance as a singleton.
*
2011-09-21 21:46:16 -05:00
* This method allows you to register an already existing object instance
* with the container to be managed as a singleton instance.
2011-09-08 17:49:16 -05:00
*
2011-09-21 22:39:02 -05:00
* <code>
* // Register an instance as a singleton in the container
2011-11-11 09:52:30 +00:00
* IoC::instance('mailer', new Mailer);
2011-09-21 22:39:02 -05:00
* </code>
*
* @param string $name
* @param mixed $instance
* @return void
*/
public static function instance($name, $instance)
{
static::$singletons[$name] = $instance;
}
2011-09-27 21:10:32 -05:00
/**
* Resolve a core Laravel class from the container.
*
* <code>
2011-10-05 18:32:48 -05:00
* // Resolve the "laravel.router" class from the container
2011-11-11 09:52:30 +00:00
* $input = IoC::core('router');
2011-09-27 21:10:32 -05:00
*
* // Equivalent resolution using the "resolve" method
2011-11-11 09:52:30 +00:00
* $input = IoC::resolve('laravel.router');
2011-10-05 18:32:48 -05:00
*
* // Pass an array of parameters to the resolver
2011-11-11 09:52:30 +00:00
* $input = IoC::core('router', array('test'));
2011-09-27 21:10:32 -05:00
* </code>
*
* @param string $name
2011-10-05 18:32:48 -05:00
* @param array $parameters
2011-09-27 21:10:32 -05:00
* @return mixed
*/
public static function core($name, $parameters = array())
2011-09-27 21:10:32 -05:00
{
return static::resolve("laravel.{$name}", $parameters);
2011-09-27 21:10:32 -05:00
}
/**
2011-09-21 22:39:02 -05:00
* Resolve an object instance from the container.
*
* <code>
* // Get an instance of the "mailer" object registered in the container
2011-11-11 09:52:30 +00:00
* $mailer = IoC::resolve('mailer');
2011-10-05 18:32:48 -05:00
*
* // Pass an array of parameters to the resolver
2011-11-11 09:52:30 +00:00
* $mailer = IoC::resolve('mailer', array('test'));
2011-09-21 22:39:02 -05:00
* </code>
*
* @param string $name
2011-10-05 18:32:48 -05:00
* @param array $parameters
* @return mixed
*/
public static function resolve($name, $parameters = array())
{
if (array_key_exists($name, static::$singletons))
{
return static::$singletons[$name];
}
if ( ! static::registered($name))
{
2011-11-16 12:56:28 -06:00
throw new \OutOfBoundsException("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'])
{
return static::$singletons[$name] = $object;
}
return $object;
}
2011-11-11 09:52:30 +00:00
}
2011-11-14 21:18:18 -06:00
/**
* 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.
*/
2011-11-16 12:56:28 -06:00
IoC::bootstrap();