1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Factory\Repository\Persistence; |
6
|
|
|
|
7
|
|
|
use Arp\DoctrineEntityRepository\Persistence\PersistService; |
8
|
|
|
use Arp\DoctrineEntityRepository\Persistence\PersistServiceInterface; |
9
|
|
|
use Arp\LaminasDoctrine\Factory\Service\EntityManagerFactoryProviderTrait; |
10
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
11
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
12
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
13
|
|
|
use Laminas\ServiceManager\ServiceLocatorInterface; |
14
|
|
|
use Psr\Container\ContainerInterface; |
15
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
16
|
|
|
use Psr\Log\NullLogger; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Alex Patterson <[email protected]> |
20
|
|
|
* @package Arp\LaminasDoctrine\Factory\Repository\Persistence |
21
|
|
|
*/ |
22
|
|
|
final class PersistServiceFactory extends AbstractFactory |
23
|
|
|
{ |
24
|
|
|
use EntityManagerFactoryProviderTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param ContainerInterface&ServiceLocatorInterface $container |
28
|
|
|
* @param string $requestedName |
29
|
|
|
* @param array<mixed>|null $options |
30
|
|
|
* |
31
|
|
|
* @return PersistServiceInterface |
32
|
|
|
* |
33
|
|
|
* @throws ServiceNotCreatedException |
34
|
|
|
* @throws ServiceNotFoundException |
35
|
|
|
*/ |
36
|
|
|
public function __invoke( |
37
|
|
|
ContainerInterface $container, |
38
|
|
|
string $requestedName, |
39
|
|
|
array $options = null |
40
|
|
|
): PersistServiceInterface { |
41
|
|
|
$options = array_replace_recursive($this->getServiceOptions($container, $requestedName), $options ?? []); |
42
|
|
|
|
43
|
|
|
$entityName = $options['entity_name'] ?? null; |
44
|
|
|
if (empty($entityName)) { |
45
|
|
|
throw new ServiceNotCreatedException( |
46
|
|
|
sprintf( |
47
|
|
|
'The required \'entity_name\' configuration option is missing for service \'%s\'', |
48
|
|
|
$requestedName |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$entityManager = $options['entity_manager'] ?? null; |
54
|
|
|
if (empty($entityManager)) { |
55
|
|
|
throw new ServiceNotCreatedException( |
56
|
|
|
sprintf( |
57
|
|
|
'The required \'entity_manager\' configuration option is missing for service \'%s\'', |
58
|
|
|
$requestedName |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return new PersistService( |
64
|
|
|
$entityName, |
65
|
|
|
$this->getEntityManager($container, $entityManager, $requestedName), |
66
|
|
|
$this->getEventDispatcher($container, $options['event_dispatcher'] ?? [], $requestedName), |
67
|
|
|
new NullLogger() |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param ServiceLocatorInterface $container |
73
|
|
|
* @param EventDispatcherInterface|string|array<mixed> $eventDispatcher |
74
|
|
|
* @param string $serviceName |
75
|
|
|
* |
76
|
|
|
* @return EventDispatcherInterface |
77
|
|
|
* |
78
|
|
|
* @throws ServiceNotCreatedException |
79
|
|
|
* @throws ServiceNotFoundException |
80
|
|
|
*/ |
81
|
|
|
private function getEventDispatcher( |
82
|
|
|
ServiceLocatorInterface $container, |
83
|
|
|
$eventDispatcher, |
84
|
|
|
string $serviceName |
85
|
|
|
): EventDispatcherInterface { |
86
|
|
|
if (is_string($eventDispatcher)) { |
87
|
|
|
$eventDispatcher = $this->getService($container, $eventDispatcher, $serviceName); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (is_array($eventDispatcher)) { |
91
|
|
|
$eventDispatcher = $this->buildService($container, 'EntityEventDispatcher', $eventDispatcher, $serviceName); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (!$eventDispatcher instanceof EventDispatcherInterface) { |
95
|
|
|
throw new ServiceNotCreatedException( |
96
|
|
|
sprintf( |
97
|
|
|
'The event dispatcher must be an object of type \'%s\'; \'%s\' provided for service \'%s\'', |
98
|
|
|
EventDispatcherInterface::class, |
99
|
|
|
is_object($eventDispatcher) ? get_class($eventDispatcher) : gettype($eventDispatcher), |
100
|
|
|
$serviceName |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $eventDispatcher; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|