Completed
Push — master ( 301e45...c7ba3c )
by Luke
11:03 queued 07:43
created

ConsoleCommandsPass::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.5806
cc 4
eloc 17
nc 4
nop 1
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
     * @override \CompilerPassInterface
63
     */
64
    public function process(ContainerBuilder $container) {
65
        $services      = $container->findTaggedServiceIds($this->tagName);
66
        $appDefinition = $container->getDefinition($this->appId);
67
68
        // TODO: verify $appDefinition's class is actually legit
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
69
70
        foreach ($services as $id => $tags) {
71
            $definition = $container->getDefinition($id);
72
73
            if ($definition->isAbstract()) {
74
                throw new InvalidArgumentException(sprintf(
75
                        'The service "%s" tagged "%s" must not be abstract.',
76
                        $id, $this->tagName));
77
            }
78
79
            $class = $container->getParameterBag()->resolveValue(
80
                    $definition->getClass());
81
            if (!is_subclass_of($class, static::CONSOLE_COMMAND)) {
82
                throw new InvalidArgumentException(sprintf(
83
                        'The service "%s" tagged "%s" must be a subclass of "%s".',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 83 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
84
                        $id, $this->tagName, static::CONSOLE_COMMAND));
85
            }
86
87
            $appDefinition->addMethodCall(
88
                    static::ADD_METHOD, [new Reference($id)]);
89
        }
90
    }
91
}
92