1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Conveyor package. |
5
|
|
|
* |
6
|
|
|
* (c) Jeroen Fiege <[email protected]> |
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 Webcreate\Conveyor\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Pass to add tagged tasks to the TaskFactory |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
class TaskCompilerPass implements CompilerPassInterface |
|
|
|
|
22
|
|
|
{ |
23
|
3 |
|
public function process(ContainerBuilder $container) |
24
|
|
|
{ |
25
|
3 |
|
if (!$container->hasDefinition('task.factory')) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
3 |
|
$definition = $container->getDefinition('task.factory'); |
30
|
3 |
|
$taggedServices = $container->findTaggedServiceIds('task'); |
31
|
|
|
|
32
|
3 |
|
foreach ($taggedServices as $id => $tagsAttributes) { |
33
|
3 |
|
foreach ($tagsAttributes as $attributes) { |
34
|
3 |
|
$taskDefinition = $container->getDefinition($id); |
35
|
3 |
|
$taskDefinition->setScope('prototype'); |
36
|
|
|
|
37
|
|
|
$attributes += array( |
38
|
3 |
|
'alias' => $id, |
39
|
|
|
'configuration' => false, |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
// validate configuration attribute |
43
|
3 |
|
if (false !== $attributes['configuration']) { |
44
|
3 |
|
$ref = new \ReflectionClass($attributes['configuration']); |
45
|
3 |
|
if (!$ref->implementsInterface('Symfony\Component\Config\Definition\ConfigurationInterface')) { |
46
|
|
|
throw new \Exception(sprintf('The tag attribute "configuration" for service "%s" must implement "Symfony\Component\Config\Definition\ConfigurationInterface"', $id)); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
$definition->addMethodCall( |
51
|
3 |
|
'addTask', |
52
|
3 |
|
array($id, $attributes['alias'], $attributes['configuration']) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
} |
56
|
3 |
|
} |
57
|
|
|
} |
58
|
|
|
|
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.