yii2框架随笔26
继续来看Container.php
<?php /** * Registers a class definition with this container. * 用这个容器注册一个类定义。 * If a class definition with the same name already exists, it will be overwritten with the new one. * 如果已存在具有相同名称的类的定义,它将被新的覆盖。 * You may use [[has()]] to check if a class definition already exists. * 您可以使用[[has()]]检查一个类定义是否已经存在. * @param string $class class name, interface name or alias name * @param mixed $definition the definition associated with `$class`. It can be one of the followings: * @param array $params the list of constructor parameters. The parameters will be passed to the class * constructor when [[get()]] is called. * @return static the container itself */ public function set($class, $definition = [], array $params = []) { // 处理definition,并存入到_definitions中 $this->_definitions[$class] = $this->normalizeDefinition($class, $definition); // 将$params存入_params中 $this->_params[$class] = $params; unset($this->_singletons[$class]); return $this; } /** * Registers a class definition with this container and marks the class as a singleton class. * 用这个容器注册一个类定义,并将该类定义为一个单独的类。 * This method is similar to [[set()]] except that classes registered via this method will only have one * instance. Each time [[get()]] is called, the same instance of the specified class will be returned. * 这种方法类似于 [[set()]]除了类注册通过这种方法只有一个实例。每一次[[get()]]之称,指定类的同一实例将返回 * @param string $class class name, interface name or alias name * @param mixed $definition the definition associated with `$class`. See [[set()]] for more details. * @param array $params the list of constructor parameters. The parameters will be passed to the class * constructor when [[get()]] is called. * @return static the container itself * @see set() */ public function setSingleton($class, $definition = [], array $params = []) { // 同上 $this->_definitions[$class] = $this->normalizeDefinition($class, $definition); $this->_params[$class] = $params; // 这里是单例,表示这个还会继续用到,所以不用unset,只需赋为空即可 $this->_singletons[$class] = null; return $this; } /** * Returns a value indicating whether the container has the definition of the specified name. * 返回一个值,该值指示容器是否具有指定名称的定义。 * @param string $class class name, interface name or alias name * @return boolean whether the container has the definition of the specified name.. * @see set() */ public function has($class) { // 判断是否已经定义了某个服务或组件 return isset($this->_definitions[$class]); } /** * Returns a value indicating whether the given name corresponds to a registered singleton. * 返回一个值,该值指示给定的名称是否对应于已注册的单体. * @param string $class class name, interface name or alias name * @param boolean $checkInstance whether to check if the singleton has been instantiated. * @return boolean whether the given name corresponds to a registered singleton. If `$checkInstance` is true, * the method should return a value indicating whether the singleton has been instantiated. */ public function hasSingleton($class, $checkInstance = false) { // 当 $checkInstance === false 时,用于判断是否已经定义了某个服务或组件 // 当 $checkInstance === true 时,用于判断是否已经有了某人服务或组件的实例 return $checkInstance ? isset($this->_singletons[$class]) : array_key_exists($class, $this->_singletons); } /** * Removes the definition for the specified name. * @param string $class class name, interface name or alias name */ public function clear($class) { // 清空相应类的definition和singleton unset($this->_definitions[$class], $this->_singletons[$class]); } /** * Normalizes the class definition. * 将类定义. * @param string $class class name * @param string|array|callable $definition the class definition * @return array the normalized class definition * @throws InvalidConfigException if the definition is invalid. */ protected function normalizeDefinition($class, $definition) { if (empty($definition)) { // 定义为空 return ['class' => $class]; } elseif (is_string($definition)) { // $definition是字符串,就表明它是类名,$class是简称 return ['class' => $definition]; } elseif (is_callable($definition, true) || is_object($definition)) { // 是个PHP callable或者是个对象,就直接返回 return $definition; } elseif (is_array($definition)) { // 是个数组 if (!isset($definition['class'])) { // 定义中不存在class键,$class必须是带namespace的类名 if (strpos($class, '\\') !== false) { $definition['class'] = $class; } else { throw new InvalidConfigException("A class definition requires a \"class\" member."); } } return $definition; } else { throw new InvalidConfigException("Unsupported definition type for \"$class\": " . gettype($definition)); } } /** * Returns the list of the object definitions or the loaded shared objects. * 返回对象定义或加载的共享对象的列表。 * @return array the list of the object definitions or the loaded shared objects (type or ID => definition or instance). */ public function getDefinitions() { return $this->_definitions; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步