1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is a part of Sculpin. |
5
|
|
|
* |
6
|
|
|
* (c) Dragonfly Development Inc. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symplify\PHP7_Sculpin\Bundle\StandaloneBundle\DI; |
13
|
|
|
|
14
|
|
|
use Nette\DI\Compiler; |
15
|
|
|
use Nette\DI\CompilerExtension; |
16
|
|
|
use Nette\DI\ServiceDefinition; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
19
|
|
|
|
20
|
|
|
final class SculpinStandaloneExtension extends CompilerExtension |
21
|
|
|
{ |
22
|
|
|
public function loadConfiguration() |
23
|
|
|
{ |
24
|
|
|
Compiler::loadDefinitions( |
25
|
|
|
$this->getContainerBuilder(), |
26
|
|
|
$this->loadFromFile(__DIR__ . '/../Resources/config/services.yml') |
|
|
|
|
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function beforeCompile() |
31
|
|
|
{ |
32
|
|
|
$this->collectServicesByType(EventDispatcherInterface::class, EventSubscriberInterface::class, 'addSubscriber'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function collectServicesByType(string $collectorType, string $collectedType, string $setterMethodName) |
36
|
|
|
{ |
37
|
|
|
$eventDispatcherDefinition = $this->findOneByType($collectorType); |
38
|
|
|
|
39
|
|
|
$eventSubscriberDefinitions = $this->getContainerBuilder() |
40
|
|
|
->findByType($collectedType); |
41
|
|
|
|
42
|
|
|
foreach ($eventSubscriberDefinitions as $eventSubscriberName => $eventSubscriberDefinition) { |
43
|
|
|
$eventDispatcherDefinition->addSetup($setterMethodName, ['@'.$eventSubscriberName]); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function findOneByType(string $class) : ServiceDefinition |
48
|
|
|
{ |
49
|
|
|
$definitionName = $this->getContainerBuilder() |
50
|
|
|
->getByType($class); |
51
|
|
|
|
52
|
|
|
return $this->getContainerBuilder() |
53
|
|
|
->getDefinition($definitionName); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.