Yii2 依赖注入
2019-08-25 22:22:29 7 举报
AI智能生成
Yii2 依赖注入
作者其他创作
大纲/内容
private $_singletons = [];
private $_definitions = [];
private $_params = [];
private $_reflections = [];
private $_dependencies = [];
set($class, $definition = [], array $params = [])
$this->_definitions[$class] = $this->normalizeDefinition($class, $definition);
$this->_params[$class] = $params;
unset($this->_singletons[$class]);
setSingleton($class, $definition = [], array $params = [])
$this->_definitions[$class] = $this->normalizeDefinition($class, $definition);
$this->_params[$class] = $params;
$this->_singletons[$class] = null;
protected normalizeDefinition($class, $definition)
if (empty($definition))
return ['class' => $class];
elseif (is_string($definition))
return ['class' => $definition];
elseif (is_callable($definition, true) || is_object($definition))
return $definition;
elseif (is_array($definition))
if (!isset($definition['class']))
$definition['class'] = $class;
return $definition;
setDefinitions(array $definitions)
foreach ($definitions as $class => $definition)
if (count($definition) === 2 && array_values($definition) === $definition)
$this->set($class, $definition[0], $definition[1]);
continue;
$this->set($class, $definition);
setSingletons(array $singletons)
foreach ($singletons as $class => $definition)
if (count($definition) === 2 && array_values($definition) === $definition)
$this->setSingleton($class, $definition[0], $definition[1]);
continue;
$this->setSingleton($class, $definition);
invoke(callable $callback, $params = [])
if (is_callable($callback))
return call_user_func_array($callback, $this->resolveCallableDependencies($callback, $params));
else
return call_user_func_array($callback, $params);
resolveCallableDependencies(callable $callback, $params = [])
if (is_array($callback))
$reflection = new \ReflectionMethod($callback[0], $callback[1]);
else
$reflection = new \ReflectionFunction($callback);
$args = [];
$associative = ArrayHelper::isAssociative($params);
foreach ($reflection->getParameters() as $param)
foreach ($params as $value)
$args[] = $value;
return $args;
protected function mergeParams($class, $params)
if (empty($this->_params[$class]))
return $params;
elseif (empty($params))
return $this->_params[$class];
else
$ps = $this->_params[$class];
foreach ($params as $index => $value)
$ps[$index] = $value;
return $ps;
get($class, $params = [], $config = [])
if (isset($this->_singletons[$class]))
return $this->_singletons[$class];
elseif (!isset($this->_definitions[$class]))
return $this->build($class, $params, $config);
$definition = $this->_definitions[$class];
if (is_callable($definition, true))
$params = $this->resolveDependencies($this->mergeParams($class, $params));
$object = call_user_func($definition, $this, $params, $config);
elseif (is_array($definition))
$concrete = $definition['class'];
unset($definition['class']);
$config = array_merge($definition, $config);
$params = $this->mergeParams($class, $params);
if ($concrete === $class)
$object = $this->build($class, $params, $config);
else
$object = $this->get($concrete, $params, $config);
elseif (is_object($definition))
return $this->_singletons[$class] = $definition;
if (array_key_exists($class, $this->_singletons))
$this->_singletons[$class] = $object;
return $object;
protected function build($class, $params, $config)
list ($reflection, $dependencies) = $this->getDependencies($class);
foreach ($params as $index => $param)
$dependencies[$index] = $param;
$dependencies = $this->resolveDependencies($dependencies, $reflection);
if (empty($config))
return $reflection->newInstanceArgs($dependencies);
if (!empty($dependencies) && $reflection->implementsInterface('yii\base\Configurable'))
$dependencies[count($dependencies) - 1] = $config;
return $reflection->newInstanceArgs($dependencies);
else
$object = $reflection->newInstanceArgs($dependencies);
foreach ($config as $name => $value)
$object->$name = $value;
return $object;
protected getDependencies($class)
if (isset($this->_reflections[$class]))
return [$this->_reflections[$class], $this->_dependencies[$class]];
$dependencies = [];
$reflection = new ReflectionClass($class);
$constructor = $reflection->getConstructor();
if ($constructor !== null)
foreach ($constructor->getParameters() as $param)
if ($param->isDefaultValueAvailable())
$dependencies[] = $param->getDefaultValue();
else
$c = $param->getClass();
$dependencies[] = Instance::of($c === null ? null : $c->getName());
$this->_reflections[$class] = $reflection;
$this->_dependencies[$class] = $dependencies;
return [$reflection, $dependencies];
protected resolveDependencies($dependencies, $reflection = null)
foreach ($dependencies as $index => $dependency)
if ($dependency instanceof Instance)
if ($dependency->id !== null)
$dependencies[$index] = $this->get($dependency->id);
return $dependencies;
has($class)
return isset($this->_definitions[$class]);
clear($class)
unset($this->_definitions[$class], $this->_singletons[$class]);
hasSingleton($class, $checkInstance = false)
return $checkInstance ? isset($this->_singletons[$class]) : array_key_exists($class, $this->_singletons);
getDefinitions()
return $this->_definitions;
0 条评论
下一页