Files
kiri-core/kiri-task/TaskContainer.php
T

73 lines
1.2 KiB
PHP
Raw Normal View History

2022-01-17 18:45:00 +08:00
<?php
namespace Kiri\Task;
2022-03-03 18:30:59 +08:00
use Exception;
2022-02-17 17:44:28 +08:00
use JetBrains\PhpStorm\Pure;
2022-01-17 18:45:00 +08:00
use Kiri\Abstracts\Component;
use Kiri\Core\HashMap;
use Psr\Container\ContainerExceptionInterface;
2022-06-16 17:38:23 +08:00
use Kiri\Di\ContainerInterface;
2022-01-17 18:45:00 +08:00
use Psr\Container\NotFoundExceptionInterface;
use Swoole\Server;
2022-06-16 17:38:23 +08:00
class TaskContainer extends Component
2022-01-17 18:45:00 +08:00
{
private HashMap $hashMap;
/**
2022-03-03 18:30:59 +08:00
* @param ContainerInterface $container
* @param array $config
* @throws Exception
2022-01-17 18:45:00 +08:00
*/
2022-03-03 18:30:59 +08:00
public function __construct(public ContainerInterface $container, array $config = [])
2022-01-17 18:45:00 +08:00
{
2022-03-03 18:30:59 +08:00
parent::__construct($config);
2022-01-17 18:45:00 +08:00
$this->hashMap = new HashMap();
}
/**
* @param string $key
* @param $handler
*/
public function add(string $key, $handler)
{
$this->hashMap->put($key, $handler);
}
2022-02-17 17:44:28 +08:00
/**
* @param string $key
* @return bool
*/
#[Pure] public function has(string $key): bool
{
return $this->hashMap->has($key);
}
2022-01-17 18:45:00 +08:00
/**
* @param string $key
* @return OnTaskInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function get(string $key): OnTaskInterface
{
$task = $this->hashMap->get($key);
if (is_string($task)) {
2022-03-03 18:30:59 +08:00
$task = $this->container->get($task);
2022-02-17 17:44:28 +08:00
if (!empty($task)) {
$this->add($key, $task);
}
2022-01-17 18:45:00 +08:00
}
return $task;
}
}