From 1c9798e9802585e798883e44c0a9e8aace962aa7 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 21 Sep 2011 22:39:02 -0500 Subject: [PATCH] added comments to ioc container class. --- laravel/container.php | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/laravel/container.php b/laravel/container.php index 3bff4545..003af5ac 100644 --- a/laravel/container.php +++ b/laravel/container.php @@ -21,6 +21,14 @@ public static function container() /** * Magic Method for calling methods on the active container instance. + * + * + * // 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); + * */ public static function __callStatic($method, $parameters) { @@ -59,6 +67,14 @@ public function __construct($registry = 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. + * + * + * // Register an object and its resolver + * IoC::container()->register('mailer', function($c) {return new Mailer;}); + * + * * @param string $name * @param Closure $resolver * @return void @@ -100,6 +116,11 @@ public function singleton($name, $resolver) * This method allows you to register an already existing object instance * with the container to be managed as a singleton instance. * + * + * // Register an instance as a singleton in the container + * IoC::container()->instance('mailer', new Mailer); + * + * * @param string $name * @param mixed $instance * @return void @@ -110,7 +131,12 @@ public function instance($name, $instance) } /** - * Resolve an object. + * Resolve an object instance from the container. + * + * + * // Get an instance of the "mailer" object registered in the container + * $mailer = IoC::container()->resolve('mailer'); + * * * @param string $name * @return mixed @@ -129,14 +155,4 @@ public function resolve($name) return (isset($this->registry[$name]['singleton'])) ? $this->singletons[$name] = $object : $object; } - /** - * Magic Method for resolving classes out of the IoC container. - */ - public function __get($key) - { - if ($this->registered($key)) return $this->resolve($key); - - throw new \Exception("Attempting to resolve undefined class [$key]."); - } - } \ No newline at end of file