ioc.php 3.17 KB
Newer Older
1
<?php namespace Laravel; use Closure;
2 3 4 5

class IoC {

	/**
6
	 * The registered dependencies.
7 8 9
	 *
	 * @var array
	 */
10
	public static $registry = array();
11 12

	/**
13
	 * The resolved singleton instances.
14 15 16
	 *
	 * @var array
	 */
17
	public static $singletons = array();
18 19

	/**
20
	 * Register an object and its resolver.
21 22 23
	 *
	 * @param  string   $name
	 * @param  Closure  $resolver
Phill Sparks committed
24
	 * @param  bool     $singleton
25 26
	 * @return void
	 */
27
	public static function register($name, Closure $resolver, $singleton = false)
28
	{
29
		static::$registry[$name] = compact('resolver', 'singleton');
30 31 32
	}

	/**
33
	 * Determine if an object has been registered in the container.
34 35 36 37
	 *
	 * @param  string  $name
	 * @return bool
	 */
38
	public static function registered($name)
39
	{
40
		return array_key_exists($name, static::$registry);
41 42 43
	}

	/**
44
	 * Register an object as a singleton.
45
	 *
Taylor Otwell committed
46
	 * Singletons will only be instantiated the first time they are resolved.
47
	 *
48 49 50 51
	 * @param  string   $name
	 * @param  Closure  $resolver
	 * @return void
	 */
52
	public static function singleton($name, $resolver)
53
	{
54
		static::register($name, $resolver, true);
55 56 57
	}

	/**
58
	 * Register an existing instance as a singleton.
59
	 *
60 61
	 * <code>
	 *		// Register an instance as a singleton in the container
62
	 *		IoC::instance('mailer', new Mailer);
63 64
	 * </code>
	 *
65 66 67 68
	 * @param  string  $name
	 * @param  mixed   $instance
	 * @return void
	 */
69
	public static function instance($name, $instance)
70
	{
71
		static::$singletons[$name] = $instance;
72 73 74
	}

	/**
75 76 77 78 79 80 81 82 83 84 85 86
	 * Register a controller with the IoC container.
	 *
	 * @param  string   $name
	 * @param  Closure  $resolver
	 * @return void
	 */
	public static function controller($name, $resolver)
	{
		static::register("controller: {$name}", $resolver);
	}

	/**
Taylor Otwell committed
87 88 89
	 * Resolve a core Laravel class from the container.
	 *
	 * <code>
90
	 *		// Resolve the "laravel.router" class from the container
91
	 *		$input = IoC::core('router');
Taylor Otwell committed
92
	 *
93
	 *		// Equivalent resolution of the router using the "resolve" method
94
	 *		$input = IoC::resolve('laravel.router');
Taylor Otwell committed
95 96 97
	 * </code>
	 *
	 * @param  string  $name
98
	 * @param  array   $parameters
Taylor Otwell committed
99 100
	 * @return mixed
	 */
101
	public static function core($name, $parameters = array())
Taylor Otwell committed
102
	{
103
		return static::resolve("laravel.{$name}", $parameters);
Taylor Otwell committed
104 105 106
	}

	/**
107 108 109 110
	 * Resolve an object instance from the container.
	 *
	 * <code>
	 *		// Get an instance of the "mailer" object registered in the container
111
	 *		$mailer = IoC::resolve('mailer');
112
	 *
113
	 *		// Get an instance of the "mailer" object and pass parameters to the resolver
114
	 *		$mailer = IoC::resolve('mailer', array('test'));
115
	 * </code>
116 117
	 *
	 * @param  string  $name
118
	 * @param  array   $parameters
119 120
	 * @return mixed
	 */
121
	public static function resolve($name, $parameters = array())
122
	{
123 124 125 126
		if (array_key_exists($name, static::$singletons))
		{
			return static::$singletons[$name];
		}
127

128
		$object = call_user_func(static::$registry[$name]['resolver'], $parameters);
129

130 131
		// 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
Taylor Otwell committed
132
		// time without having to instantiate a brand new instance.
133
		if (isset(static::$registry[$name]['singleton']))
134
		{
135
			return static::$singletons[$name] = $object;
136 137 138
		}

		return $object;
139 140
	}

141
}