1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Annotations\Builder; |
4
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Builder\ServiceDefinition; |
6
|
|
|
use BrainExe\Core\Annotations\Listen; |
7
|
|
|
use BrainExe\Core\DependencyInjection\CompilerPass\EventListenerCompilerPass; |
8
|
|
|
use Doctrine\Common\Annotations\Annotation; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use ReflectionMethod; |
11
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
12
|
|
|
|
13
|
|
|
class EventListener extends ServiceDefinition |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
2 |
View Code Duplication |
public function build(ReflectionClass $reflectionClass, Annotation $annotation) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** @var Definition $definition */ |
22
|
2 |
|
list($serviceId, $definition) = parent::build($reflectionClass, $annotation); |
23
|
|
|
|
24
|
2 |
|
$serviceId = sprintf('__Listener.%s', $serviceId); |
25
|
|
|
|
26
|
2 |
|
$definition->addTag(EventListenerCompilerPass::TAG); |
27
|
2 |
|
$definition->setShared(false); |
28
|
2 |
|
$definition->setPublic(true); |
29
|
|
|
|
30
|
2 |
|
return [$serviceId, $definition]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
2 |
|
public function processMethod(Definition $definition, ReflectionMethod $method) |
37
|
|
|
{ |
38
|
2 |
|
parent::processMethod($definition, $method); |
39
|
|
|
|
40
|
|
|
/** @var Listen $annotation */ |
41
|
2 |
|
$annotation = $this->reader->getMethodAnnotation($method, Listen::class); |
42
|
2 |
|
if ($annotation) { |
43
|
2 |
|
$definition->addTag(EventListenerCompilerPass::TAG_METHOD, [ |
44
|
2 |
|
'method' => $method->getName(), |
45
|
2 |
|
'event' => $annotation->event, |
46
|
2 |
|
'priority' => $annotation->priority, |
47
|
|
|
]); |
48
|
|
|
} |
49
|
2 |
|
} |
50
|
|
|
} |
51
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.