This commit is contained in:
as2252258@163.com
2021-08-09 00:15:03 +08:00
parent 8c74ed4d00
commit 94a9184bf0
8 changed files with 705 additions and 804 deletions
+86
View File
@@ -0,0 +1,86 @@
<?php
namespace Snowflake\Di;
use Annotation\Inject;
use Snowflake\Abstracts\Component;
use Snowflake\Snowflake;
/**
* 服务定位器
*/
class LocalService extends Component
{
private array $_components = [];
private array $_definition = [];
/**
* @param $name
* @param $define
*/
public function set($name, $define)
{
unset($this->_components[$name]);
$this->_definition[$name] = $define;
}
/**
* @throws \Exception
*/
public function get(string $name, $throwException = true)
{
if (isset($this->_components[$name])) {
return $this->_components[$name];
}
if (isset($this->_definitions[$name])) {
$definition = $this->_definitions[$name];
if (is_object($definition) && !$definition instanceof \Closure) {
return $this->_components[$name] = $definition;
}
return $this->_components[$name] = Snowflake::createObject($definition);
} else if ($throwException) {
throw new \Exception("Unknown component ID: $name");
}
return null;
}
/**
* @param array $components
*/
public function setComponents(array $components)
{
foreach ($components as $name => $component) {
$this->set($name, $component);
}
}
/**
* @param $id
* @return bool
*/
public function has($id): bool
{
return isset($this->_components[$id], $this->_definition[$id]);
}
/**
* @param $id
*/
public function remove($id): void
{
unset($this->_components[$id], $this->_definition[$id]);
}
}
-165
View File
@@ -1,165 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/25 0025
* Time: 18:29
*/
declare(strict_types=1);
namespace Snowflake\Di;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Component;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
/**
* Class Service
* @package Snowflake\Snowflake\Di
*/
class Service extends Component
{
private array $_components = [];
private array $_definition = [];
private array $_ids = [];
protected array $_alias = [];
/**
* @param $id
* @param bool $try
* @return mixed
* @throws Exception
*/
public function get($id, bool $try = true): mixed
{
if (isset($this->_components[$id])) {
return $this->_components[$id];
}
$config = $this->_getConfig($id, $try);
if (!is_object($config)) {
$config = Snowflake::createObject($config);
}
return $this->_components[$id] = $config;
}
/**
* @param $id
* @param $try
* @return mixed
* @throws ComponentException
*/
private function _getConfig($id, $try): mixed
{
$config = $this->_definition[$id] ?? $this->_alias[$id] ?? null;
if (is_null($config)) {
if ($try === false) {
return null;
}
throw new ComponentException("Unknown component ID: $id");
}
return $config;
}
/**
* @param string $className
* @param string $alias
*/
public function setAlias(string $className, string $alias)
{
$this->_alias[$className] = $alias;
}
/**
* @param $id
* @param $definition
*
* @return mixed
* @throws Exception
*/
public function set($id, $definition): void
{
if ($definition === NULL) {
$this->remove($id);
return;
}
$this->_ids[] = $id;
unset($this->_components[$id]);
if (is_object($definition)) {
$this->_components[$id] = $definition;
$this->_definition[$id] = $definition;
} else if (!is_array($definition)) {
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
} else {
$this->_definition[$id] = $definition;
}
}
/**
* @param $id
* @return bool
*/
#[Pure] public function has($id): bool
{
return in_array($id, $this->_ids);
}
/**
* @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
{
$component = $this->_components[$id];
$className = $component::class;
unset($component, $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]);
}
Snowflake::getDi()->unset($className);
return $this->has($id);
}
}