1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TMV\Laminas\Messenger\Bridge\Doctrine\Factory\Middleware; |
6
|
|
|
|
7
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
8
|
|
|
use Psr\Container\ContainerInterface; |
9
|
|
|
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; |
10
|
|
|
use TMV\Laminas\Messenger\ConfigProvider; |
11
|
|
|
use TMV\Laminas\Messenger\Exception\InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
abstract class AbstractDoctrineMiddlewareFactory |
14
|
|
|
{ |
15
|
|
|
protected ?string $name; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @final |
19
|
|
|
*/ |
20
|
|
|
public function __construct(?string $connectionName = null) |
21
|
|
|
{ |
22
|
|
|
$this->name = $connectionName; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getName(): ?string |
26
|
|
|
{ |
27
|
|
|
return $this->name; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
abstract public function __invoke(ContainerInterface $container): MiddlewareInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array{logger?: string|null} |
34
|
|
|
*/ |
35
|
|
|
protected function getConfig(ContainerInterface $container): array |
36
|
|
|
{ |
37
|
|
|
/** @var array{logger?: string|null} $config */ |
38
|
|
|
$config = $container->get('config')[ConfigProvider::CONFIG_KEY] ?? []; |
39
|
|
|
|
40
|
|
|
return $config; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array{connection_registry?: string|null, manager_registry: string} |
45
|
|
|
*/ |
46
|
|
|
protected function getDoctrineConfig(ContainerInterface $container): array |
47
|
|
|
{ |
48
|
|
|
/** @var array{connection_registry?: string|null, manager_registry: string} $config */ |
49
|
|
|
$config = $this->getConfig($container)['doctrine']; |
50
|
|
|
|
51
|
|
|
return $config; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getManagerRegistry(ContainerInterface $container): ManagerRegistry |
55
|
|
|
{ |
56
|
|
|
$config = $this->getDoctrineConfig($container); |
57
|
|
|
/** @var ManagerRegistry $managerRegistry */ |
58
|
|
|
$managerRegistry = $container->get($config['manager_registry']); |
59
|
|
|
|
60
|
|
|
return $managerRegistry; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function __set_state(array $data): static |
64
|
|
|
{ |
65
|
|
|
return new static($data['name'] ?? null); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @psalm-api |
70
|
|
|
* |
71
|
|
|
* @param array<int, mixed> $arguments |
72
|
|
|
*/ |
73
|
|
|
public static function __callStatic(string $name, array $arguments): MiddlewareInterface |
74
|
|
|
{ |
75
|
|
|
if (! array_key_exists(0, $arguments) || ! $arguments[0] instanceof ContainerInterface) { |
76
|
|
|
throw new InvalidArgumentException(sprintf( |
77
|
|
|
'The first argument must be of type %s', |
78
|
|
|
ContainerInterface::class |
79
|
|
|
)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return (new static($name))($arguments[0]); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|