|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DoctrineModule\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\EventManager; |
|
8
|
|
|
use Doctrine\Common\EventSubscriber; |
|
9
|
|
|
use Interop\Container\ContainerInterface; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use Laminas\ServiceManager\ServiceLocatorInterface; |
|
12
|
|
|
use function class_exists; |
|
13
|
|
|
use function get_class; |
|
14
|
|
|
use function gettype; |
|
15
|
|
|
use function is_object; |
|
16
|
|
|
use function is_string; |
|
17
|
|
|
use function sprintf; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Factory responsible for creating EventManager instances |
|
21
|
|
|
*/ |
|
22
|
|
|
class EventManagerFactory extends AbstractFactory |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritDoc} |
|
26
|
|
|
*/ |
|
27
|
4 |
|
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null) |
|
28
|
|
|
{ |
|
29
|
4 |
|
$options = $this->getOptions($container, 'eventmanager'); |
|
30
|
4 |
|
$eventManager = new EventManager(); |
|
31
|
|
|
|
|
32
|
4 |
|
foreach ($options->getSubscribers() as $subscriberName) { |
|
33
|
4 |
|
$subscriber = $subscriberName; |
|
34
|
|
|
|
|
35
|
4 |
|
if (is_string($subscriber)) { |
|
36
|
3 |
|
if ($container->has($subscriber)) { |
|
37
|
1 |
|
$subscriber = $container->get($subscriber); |
|
38
|
2 |
|
} elseif (class_exists($subscriber)) { |
|
39
|
1 |
|
$subscriber = new $subscriber(); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
4 |
|
if ($subscriber instanceof EventSubscriber) { |
|
44
|
3 |
|
$eventManager->addEventSubscriber($subscriber); |
|
45
|
3 |
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$subscriberType = is_object($subscriberName) ? get_class($subscriberName) : $subscriberName; |
|
49
|
|
|
|
|
50
|
1 |
|
throw new InvalidArgumentException( |
|
51
|
1 |
|
sprintf( |
|
52
|
|
|
'Invalid event subscriber "%s" given, must be a service name, ' |
|
53
|
1 |
|
. 'class name or an instance implementing Doctrine\Common\EventSubscriber', |
|
54
|
1 |
|
is_string($subscriberType) ? $subscriberType : gettype($subscriberType) |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
return $eventManager; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritDoc} |
|
64
|
|
|
*/ |
|
65
|
4 |
|
public function createService(ServiceLocatorInterface $container) |
|
66
|
|
|
{ |
|
67
|
4 |
|
return $this($container, EventManager::class); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the class name of the options associated with this factory. |
|
72
|
|
|
*/ |
|
73
|
4 |
|
public function getOptionsClass() : string |
|
74
|
|
|
{ |
|
75
|
4 |
|
return 'DoctrineModule\Options\EventManager'; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|