|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Queue; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
|
9
|
|
|
use Spiral\Core\CompatiblePipelineBuilder; |
|
10
|
|
|
use Spiral\Core\Container\Autowire; |
|
11
|
|
|
use Spiral\Core\Exception\Container\ContainerException; |
|
12
|
|
|
use Spiral\Core\FactoryInterface; |
|
13
|
|
|
use Spiral\Interceptors\PipelineBuilderInterface; |
|
14
|
|
|
use Spiral\Queue\Config\QueueConfig; |
|
15
|
|
|
use Spiral\Queue\Interceptor\Push\Core as PushCore; |
|
16
|
|
|
|
|
17
|
|
|
final class QueueManager implements QueueConnectionProviderInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var QueueInterface[] */ |
|
20
|
|
|
private array $pipelines = []; |
|
21
|
|
|
private PipelineBuilderInterface $builder; |
|
22
|
|
|
|
|
23
|
8 |
|
public function __construct( |
|
24
|
|
|
private readonly QueueConfig $config, |
|
25
|
|
|
private readonly ContainerInterface $container, |
|
26
|
|
|
private readonly FactoryInterface $factory, |
|
27
|
|
|
?EventDispatcherInterface $dispatcher = null, |
|
28
|
|
|
?PipelineBuilderInterface $builder = null, |
|
29
|
|
|
) { |
|
30
|
8 |
|
$this->builder = $builder ?? new CompatiblePipelineBuilder($dispatcher); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
6 |
|
public function getConnection(?string $name = null): QueueInterface |
|
34
|
|
|
{ |
|
35
|
6 |
|
$name ??= $this->getDefaultDriver(); |
|
36
|
|
|
// Replaces alias with real pipeline name |
|
37
|
6 |
|
$name = $this->config->getAliases()[$name] ?? $name; |
|
38
|
|
|
|
|
39
|
6 |
|
if (!isset($this->pipelines[$name])) { |
|
40
|
6 |
|
$this->pipelines[$name] = $this->resolveConnection($name); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
6 |
|
return $this->pipelines[$name]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @throws Exception\InvalidArgumentException |
|
48
|
|
|
* @throws Exception\NotSupportedDriverException |
|
49
|
|
|
*/ |
|
50
|
6 |
|
private function resolveConnection(string $name): QueueInterface |
|
51
|
|
|
{ |
|
52
|
6 |
|
$config = $this->config->getConnection($name); |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
6 |
|
$driver = $this->factory->make($config['driver'], $config); |
|
56
|
|
|
|
|
57
|
6 |
|
$list = []; |
|
58
|
6 |
|
foreach ($this->config->getPushInterceptors() as $interceptor) { |
|
59
|
|
|
$list[] = \is_string($interceptor) || $interceptor instanceof Autowire |
|
60
|
|
|
? $this->container->get($interceptor) |
|
|
|
|
|
|
61
|
|
|
: $interceptor; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
6 |
|
return new Queue($this->builder->withInterceptors(...$list)->build(new PushCore($driver))); |
|
65
|
|
|
} catch (ContainerException $e) { |
|
66
|
|
|
throw new Exception\NotSupportedDriverException( |
|
67
|
|
|
\sprintf( |
|
68
|
|
|
'Driver `%s` is not supported. Connection `%s` cannot be created. Reason: `%s`', |
|
69
|
|
|
$config['driver'], |
|
70
|
|
|
$name, |
|
71
|
|
|
$e->getMessage() |
|
72
|
|
|
), |
|
73
|
|
|
$e->getCode(), |
|
74
|
|
|
$e |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @throws Exception\InvalidArgumentException |
|
81
|
|
|
*/ |
|
82
|
2 |
|
private function getDefaultDriver(): string |
|
83
|
|
|
{ |
|
84
|
2 |
|
return $this->config->getDefaultDriver(); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|