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