1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Moodle component manager. |
5
|
|
|
* |
6
|
|
|
* @author Luke Carrier <[email protected]> |
7
|
|
|
* @copyright 2016 Luke Carrier |
8
|
|
|
* @license GPL-3.0+ |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ComponentManager\DependencyInjection; |
12
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Add tagged console commands. |
20
|
|
|
*/ |
21
|
|
|
class ConsoleCommandsPass implements CompilerPassInterface { |
22
|
|
|
/** |
23
|
|
|
* Console command class. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const CONSOLE_COMMAND = 'Symfony\\Component\\Console\\Command\\Command'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Method to call on console application to add commands. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const ADD_METHOD = 'add'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Console application ID. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $appId; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Tag name. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $tagName; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Initialiser. |
52
|
|
|
* |
53
|
|
|
* @param string|null $tagName |
54
|
|
|
* @param string|null $appId |
55
|
|
|
*/ |
56
|
|
|
public function __construct($tagName=null, $appId=null) { |
57
|
|
|
$this->tagName = ($tagName === null) ? 'console.command' : $tagName; |
58
|
|
|
$this->appId = ($appId === null) ? 'console.application' : $appId; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc CompilerPassInterface |
63
|
|
|
*/ |
64
|
|
|
public function process(ContainerBuilder $container) { |
65
|
|
|
$services = $container->findTaggedServiceIds($this->tagName); |
66
|
|
|
$appDefinition = $container->getDefinition($this->appId); |
67
|
|
|
|
68
|
|
|
foreach ($services as $id => $tags) { |
69
|
|
|
$definition = $container->getDefinition($id); |
70
|
|
|
|
71
|
|
|
if ($definition->isAbstract()) { |
72
|
|
|
throw new InvalidArgumentException(sprintf( |
73
|
|
|
'The service "%s" tagged "%s" must not be abstract.', |
74
|
|
|
$id, $this->tagName)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$class = $container->getParameterBag()->resolveValue( |
78
|
|
|
$definition->getClass()); |
79
|
|
|
if (!is_subclass_of($class, static::CONSOLE_COMMAND)) { |
80
|
|
|
throw new InvalidArgumentException(sprintf( |
81
|
|
|
'The service "%s" tagged "%s" must be a subclass of "%s".', |
82
|
|
|
$id, $this->tagName, static::CONSOLE_COMMAND)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$appDefinition->addMethodCall( |
86
|
|
|
static::ADD_METHOD, [new Reference($id)]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|