Files
spa-api/laravel/ioc.php

137 lines
3.2 KiB
PHP
Raw Normal View History

2012-01-16 13:59:24 -06:00
<?php namespace Laravel; use Closure;
class IoC {
/**
* The registered dependencies.
*
* @var array
*/
2012-01-16 13:59:24 -06:00
public static $registry = array();
/**
* The resolved singleton instances.
*
* @var array
*/
2012-01-16 13:59:24 -06:00
public static $singletons = array();
2011-08-26 21:42:04 -05:00
/**
2011-09-16 20:30:22 -05:00
* Register an object and its resolver.
*
* @param string $name
* @param Closure $resolver
* @return void
*/
2012-01-16 13:59:24 -06:00
public static function register($name, Closure $resolver, $singleton = false)
{
2012-01-16 13:59:24 -06:00
static::$registry[$name] = compact('resolver', '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.
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);
}
/**
2012-01-16 13:59:24 -06:00
* Register an existing instance as a singleton.
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
*
2012-01-16 13:59:24 -06:00
* // Equivalent resolution of the router using the "resolve" method
2011-11-11 09:52:30 +00:00
* $input = IoC::resolve('laravel.router');
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
*
2012-01-16 13:59:24 -06:00
* // Get an instance of the "mailer" object and pass 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))
{
2012-01-16 13:59:24 -06:00
throw new \Exception("Error resolving [$name]. No resolver has been registered.");
}
$object = call_user_func(static::$registry[$name]['resolver'], $parameters);
2012-01-16 13:59:24 -06:00
// 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;
}
return $object;
}
2012-01-16 13:59:24 -06:00
}