Files
spa-api/laravel/container.php

190 lines
4.3 KiB
PHP
Raw Normal View History

<?php namespace Laravel;
class IoC {
/**
2011-08-26 21:42:04 -05:00
* The active container instance.
*
2011-08-26 21:42:04 -05:00
* @var Container
*/
2011-08-26 21:42:04 -05:00
public static $container;
/**
2011-08-26 21:42:04 -05:00
* Get the active container instance.
*
* @return Container
*/
2011-08-26 21:42:04 -05:00
public static function container()
{
2011-08-26 21:42:04 -05:00
return static::$container;
}
/**
2011-08-26 21:42:04 -05:00
* Magic Method for calling methods on the active container instance.
2011-09-21 22:39:02 -05:00
*
* <code>
* // Call the "resolve" method on the active container
* $instance = IoC::resolve('laravel.routing.router');
*
* // Call the "instance" method on the active container
* IoC::instance('mailer', new Mailer);
* </code>
*/
public static function __callStatic($method, $parameters)
{
2011-08-26 21:42:04 -05:00
return call_user_func_array(array(static::$container, $method), $parameters);
}
}
class Container {
/**
* The resolved singleton instances.
*
* @var array
*/
2011-09-04 23:55:28 -05:00
public $singletons = array();
/**
* The registered dependencies.
*
* @var array
*/
2011-09-08 17:49:16 -05:00
protected $registry = array();
2011-08-26 21:42:04 -05:00
/**
* Create a new IoC container instance.
*
2011-09-08 17:49:16 -05:00
* @param array $registry
2011-08-26 21:42:04 -05:00
* @return void
*/
2011-09-08 17:49:16 -05:00
public function __construct($registry = array())
2011-08-26 21:42:04 -05:00
{
2011-09-08 17:49:16 -05:00
$this->registry = $registry;
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
* IoC::container()->register('mailer', function($c) {return new Mailer;});
* </code>
*
* @param string $name
* @param Closure $resolver
* @return void
*/
public function register($name, $resolver, $singleton = false)
{
2011-09-08 17:49:16 -05:00
$this->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 function registered($name)
{
2011-09-08 17:49:16 -05:00
return array_key_exists($name, $this->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 function singleton($name, $resolver)
{
$this->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
* IoC::container()->instance('mailer', new Mailer);
* </code>
*
* @param string $name
* @param mixed $instance
* @return void
*/
public function instance($name, $instance)
{
$this->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-10-08 22:41:52 -05:00
* $input = IoC::container()->core('router');
2011-09-27 21:10:32 -05:00
*
* // Equivalent resolution using the "resolve" method
2011-10-05 18:32:48 -05:00
* $input = IoC::container()->resolve('laravel.router');
*
* // Pass an array of parameters to the resolver
2011-10-08 22:41:52 -05:00
* $input = IoC::container()->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
*/
2011-10-05 18:32:48 -05:00
public function core($name, $parameters = array())
2011-09-27 21:10:32 -05:00
{
2011-10-05 18:32:48 -05:00
return $this->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
* $mailer = IoC::container()->resolve('mailer');
2011-10-05 18:32:48 -05:00
*
* // Pass an array of parameters to the resolver
* $mailer = IoC::container()->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
*/
2011-10-05 18:32:48 -05:00
public function resolve($name, $parameters = array())
{
if (array_key_exists($name, $this->singletons)) return $this->singletons[$name];
if ( ! $this->registered($name))
{
throw new \Exception("Error resolving [$name]. No resolver has been registered in the container.");
}
2011-10-05 18:32:48 -05:00
$object = call_user_func($this->registry[$name]['resolver'], $this, $parameters);
if (isset($this->registry[$name]['singleton']) and $this->registry[$name]['singleton'])
{
return $this->singletons[$name] = $object;
}
return $object;
}
}