2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/4/25 0025
|
|
|
|
|
* Time: 18:29
|
|
|
|
|
*/
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
namespace Snowflake\Di;
|
|
|
|
|
|
|
|
|
|
|
2020-12-14 17:35:35 +08:00
|
|
|
use ReflectionException;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Snowflake\Exception\ComponentException;
|
|
|
|
|
use Snowflake\Abstracts\Component;
|
|
|
|
|
use Exception;
|
2020-12-14 17:35:35 +08:00
|
|
|
use Snowflake\Exception\NotFindClassException;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Service
|
2020-08-31 12:38:32 +08:00
|
|
|
* @package Snowflake\Snowflake\Di
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
|
|
|
|
class Service extends Component
|
|
|
|
|
{
|
|
|
|
|
|
2021-02-23 03:02:34 +08:00
|
|
|
private array $_components = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private array $_definition = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected array $_alias = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $id
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
public function get($id): mixed
|
|
|
|
|
{
|
2021-02-23 03:18:50 +08:00
|
|
|
if (isset($this->_components[$id])) {
|
|
|
|
|
return $this->_components[$id];
|
|
|
|
|
}
|
|
|
|
|
if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) {
|
|
|
|
|
throw new ComponentException("Unknown component ID: $id");
|
|
|
|
|
}
|
|
|
|
|
if (isset($this->_definition[$id])) {
|
2021-02-23 03:02:34 +08:00
|
|
|
$config = $this->_definition[$id];
|
|
|
|
|
if (is_object($config)) {
|
2021-02-23 03:18:50 +08:00
|
|
|
return $this->_components[$id] = $config;
|
2021-02-23 03:02:34 +08:00
|
|
|
}
|
2021-02-23 03:18:50 +08:00
|
|
|
$object = Snowflake::createObject($config);
|
|
|
|
|
} else {
|
|
|
|
|
$config = $this->_alias[$id];
|
|
|
|
|
|
|
|
|
|
$object = Snowflake::createObject($config);
|
2021-02-23 03:02:34 +08:00
|
|
|
}
|
2021-02-23 03:18:50 +08:00
|
|
|
return $this->_components[$id] = $object;
|
2021-02-23 03:02:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $className
|
|
|
|
|
* @param string $alias
|
|
|
|
|
*/
|
|
|
|
|
public function setAlias(string $className, string $alias)
|
|
|
|
|
{
|
|
|
|
|
$this->_alias[$className] = $alias;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $id
|
|
|
|
|
* @param $definition
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws ComponentException
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
|
|
|
|
*/
|
|
|
|
|
public function set($id, $definition): mixed
|
|
|
|
|
{
|
|
|
|
|
if ($definition === NULL) {
|
2021-02-23 03:18:50 +08:00
|
|
|
return $this->remove($id);
|
2021-02-23 03:02:34 +08:00
|
|
|
}
|
|
|
|
|
unset($this->_components[$id]);
|
|
|
|
|
if (is_object($definition) || is_callable($definition, TRUE)) {
|
2021-02-23 03:18:50 +08:00
|
|
|
return $this->_definition[$id] = $definition;
|
|
|
|
|
} else if (!is_array($definition)) {
|
2021-02-23 03:17:32 +08:00
|
|
|
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
|
2021-02-23 03:02:34 +08:00
|
|
|
}
|
2021-02-23 03:18:50 +08:00
|
|
|
if (!isset($definition['class'])) {
|
|
|
|
|
throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element.");
|
|
|
|
|
} else {
|
|
|
|
|
$this->_definition[$id] = $definition;
|
|
|
|
|
}
|
|
|
|
|
return $this->get($id);
|
2021-02-23 03:02:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $id
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function has($id): bool
|
|
|
|
|
{
|
|
|
|
|
return isset($this->_definition[$id]) || isset($this->_components[$id]) || isset($this->_alias[$id]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function setComponents(array $data)
|
|
|
|
|
{
|
|
|
|
|
foreach ($data as $key => $val) {
|
|
|
|
|
$this->set($key, $val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function __get($name): mixed
|
|
|
|
|
{
|
|
|
|
|
if ($this->has($name)) {
|
|
|
|
|
return $this->get($name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::__get($name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $id
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function remove($id): bool
|
|
|
|
|
{
|
|
|
|
|
unset($this->_components[$id]);
|
|
|
|
|
unset($this->_definition[$id]);
|
|
|
|
|
if (isset($this->_alias[$id])) {
|
|
|
|
|
unset($this->_components[$this->_alias[$id]]);
|
|
|
|
|
unset($this->_definition[$this->_alias[$id]]);
|
|
|
|
|
unset($this->_alias[$id]);
|
|
|
|
|
}
|
|
|
|
|
return $this->has($id);
|
|
|
|
|
}
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|