1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Queue\Interceptor\Consume; |
6
|
|
|
|
7
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
use Spiral\Core\CoreInterface; |
9
|
|
|
use Spiral\Interceptors\Context\CallContext; |
10
|
|
|
use Spiral\Interceptors\HandlerInterface; |
11
|
|
|
use Spiral\Queue\Event\JobProcessed; |
12
|
|
|
use Spiral\Queue\Event\JobProcessing; |
13
|
|
|
use Spiral\Queue\HandlerRegistryInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @internal |
17
|
|
|
* @psalm-type TParameters = array{ |
18
|
|
|
* driver: non-empty-string, |
19
|
|
|
* queue: non-empty-string, |
20
|
|
|
* id: non-empty-string, |
21
|
|
|
* payload: mixed, |
22
|
|
|
* headers: array |
23
|
|
|
* } |
24
|
|
|
*/ |
25
|
12 |
|
final class Core implements CoreInterface, HandlerInterface |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
public function __construct( |
28
|
|
|
private readonly HandlerRegistryInterface $registry, |
29
|
12 |
|
private readonly ?EventDispatcherInterface $dispatcher = null |
30
|
|
|
) { |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
10 |
|
* @param-assert TParameters $parameters |
35
|
|
|
* @deprecated |
36
|
10 |
|
*/ |
37
|
10 |
|
public function callAction(string $controller, string $action, array $parameters = []): mixed |
38
|
10 |
|
{ |
39
|
|
|
\assert(\is_string($parameters['driver'])); |
40
|
10 |
|
\assert(\is_string($parameters['queue'])); |
41
|
|
|
\assert(\is_string($parameters['id'])); |
42
|
|
|
|
43
|
10 |
|
$this->dispatchEvent(JobProcessing::class, $controller, $parameters); |
44
|
10 |
|
|
45
|
10 |
|
/** @psalm-suppress TooManyArguments */ |
46
|
|
|
$this->registry |
47
|
10 |
|
->getHandler($controller) |
48
|
|
|
->handle($controller, $parameters['id'], $parameters['payload'], $parameters['headers'] ?? []); |
|
|
|
|
49
|
10 |
|
|
50
|
|
|
$this->dispatchEvent(JobProcessed::class, $controller, $parameters); |
51
|
|
|
|
52
|
|
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function handle(CallContext $context): mixed |
56
|
10 |
|
{ |
57
|
|
|
$args = $context->getArguments(); |
58
|
10 |
|
$controller = $context->getTarget()->getPath()[0]; |
59
|
10 |
|
$action = $context->getTarget()->getPath()[1]; |
60
|
10 |
|
|
61
|
10 |
|
return $this->callAction($controller, $action, $args); |
|
|
|
|
62
|
10 |
|
} |
63
|
10 |
|
|
64
|
10 |
|
/** |
65
|
10 |
|
* @param class-string $event |
|
|
|
|
66
|
|
|
* @param-assert TParameters $parameters |
67
|
|
|
*/ |
68
|
|
|
private function dispatchEvent(string $event, string $name, array $parameters): void |
69
|
|
|
{ |
70
|
|
|
$this->dispatcher?->dispatch(new $event( |
71
|
|
|
name: $name, |
72
|
|
|
driver: $parameters['driver'], |
73
|
|
|
queue: $parameters['queue'], |
74
|
|
|
id: $parameters['id'], |
75
|
|
|
payload: $parameters['payload'], |
76
|
|
|
headers: $parameters['headers'] ?? [] |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.