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

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

概述<?phpnamespaceLaravel;useClosure;//控制反转类(Inversionofcontrol)classIoC{ /** *Theregistereddependencies. *已注册的依赖项。 *@vararray */ publicstatic$registry=array(); /** *Theresolvedsingletoninstances. *解析的
<?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    

总结

以上是内存溢出为你收集整理的【Laravel3.0.0源码阅读分析】控制反转类ioc.php全部内容,希望文章能够帮你解决【Laravel3.0.0源码阅读分析】控制反转类ioc.php所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1001769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存