Repository navigation
Expand file tree
/
Copy pathPoolFactory.php
More file actions
71 lines (56 loc) · 1.53 KB
/
Copy pathPoolFactory.php
File metadata and controls
71 lines (56 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Pool\SimplePool;
use Psr\Container\ContainerInterface;
use function Hyperf\Support\make;
class PoolFactory
{
/**
* @var Pool[]
*/
protected array $pools = [];
protected array $configs = [];
public function __construct(protected ContainerInterface $container)
{
}
public function addConfig(Config $config)
{
$this->configs[$config->getName()] = $config;
return $this;
}
public function get(string $name, callable $callback, array $option = []): Pool
{
if (! $this->hasConfig($name)) {
$config = new Config($name, $callback, $option);
$this->addConfig($config);
}
$config = $this->getConfig($name);
if (! isset($this->pools[$name])) {
$this->pools[$name] = make(Pool::class, [
'callback' => $config->getCallback(),
'option' => $config->getOption(),
]);
}
return $this->pools[$name];
}
public function getPoolNames(): array
{
return array_keys($this->pools);
}
protected function hasConfig(string $name): bool
{
return isset($this->configs[$name]);
}
protected function getConfig(string $name): Config
{
return $this->configs[$name];
}
}