【Laravel3.0.0源码阅读分析】控制反转类ioc.php

<?php namespace Laravel; use Closure;
// 控制反转类(Inversion of control)
class IoC {

	/**
	 * The registered dependencies.
	 * 已注册的依赖项。
	 * @var array
	 */
	public static $registry = array();

	/**
	 * The resolved singleton instances.
	 * 解析的单例实例。
	 * @var array
	 */
	public static $singletons = array();

	/**
	 * Register an object and its resolver.
	 * 注册一个对象及其解析器
	 * @param  string   $name
	 * @param  Closure  $resolver
	 * @param  bool     $singleton
	 * @return void
	 */
	public static function register($name, Closure $resolver, $singleton = false)
	{
		static::$registry[$name] = compact('resolver', 'singleton');
	}

	/**
	 * 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);
	}

	/**
	 * Register an object as a singleton.
	 * 将对象注册为单例。
	 * Singletons will only be instantiated the first time they are resolved.
	 * 单例只会在第一次解析时被实例化
	 * @param  string   $name
	 * @param  Closure  $resolver
	 * @return void
	 */
	public static function singleton($name, $resolver)
	{
		static::register($name, $resolver, true);
	}

	/**
	 * Register an existing instance as a singleton.
	 * 将现有实例注册为单例。
	 * <code>
	 *		// Register an instance as a singleton in the container
	 *		IoC::instance('mailer', new Mailer);
	 * </code>
	 *
	 * @param  string  $name
	 * @param  mixed   $instance
	 * @return void
	 */
	public static function instance($name, $instance)
	{
		static::$singletons[$name] = $instance;
	}

	/**
	 * Register a controller with the IoC container.
	 * 向 IoC 容器注册控制器。
	 * @param  string   $name
	 * @param  Closure  $resolver
	 * @return void
	 */
	public static function controller($name, $resolver)
	{
		static::register("controller: {$name}", $resolver);
	}

	/**
	 * Resolve a core Laravel class from the container.
	 * 容器解析核心 Laravel 类。
	 * <code>
	 *		// Resolve the "laravel.router" class from the container
	 *		$input = IoC::core('router');
	 *
	 *		// Equivalent resolution of the router using the "resolve" method
	 *		$input = IoC::resolve('laravel.router');
	 * </code>
	 *
	 * @param  string  $name
	 * @param  array   $parameters
	 * @return mixed
	 */
	public static function core($name, $parameters = array())
	{
		return static::resolve("laravel.{$name}", $parameters);
	}

	/**
	 * Resolve an object instance from the container.
	 * 从容器解析对象实例。
	 * <code>
	 *		// Get an instance of the "mailer" object registered in the container
	 *		$mailer = IoC::resolve('mailer');
	 *
	 *		// Get an instance of the "mailer" object and pass parameters to the resolver
	 *		$mailer = IoC::resolve('mailer', array('test'));
	 * </code>
	 *
	 * @param  string  $name
	 * @param  array   $parameters
	 * @return mixed
	 */
	public static function resolve($name, $parameters = array())
	{
		if (array_key_exists($name, static::$singletons))
		{
			return static::$singletons[$name];
		}

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

		// 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 brand new instance.
        // 如果解析器注册为单例解析器,我们将在容器中缓存对象的实例,以便我们下次可以解析它而不必实例化一个全新的实例。
		if (isset(static::$registry[$name]['singleton']))
		{
			return static::$singletons[$name] = $object;
		}

		return $object;
	}

}

github地址: https://github.com/liu-shilong/laravel3-scr    

上一篇:小柒同学的第三课 - 函数介绍


下一篇:asp.net把文件存入SqlServer数据库,并读取出来下载的过程