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 Sculpin\Bundle\SculpinBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Application; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
final class LoadCommandsToConsoleCompilerPass implements CompilerPassInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ContainerBuilder |
25
|
|
|
*/ |
26
|
|
|
private $containerBuilder; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function process(ContainerBuilder $containerBuilder) |
32
|
|
|
{ |
33
|
|
|
$this->containerBuilder = $containerBuilder; |
34
|
|
|
|
35
|
|
|
$consoleApplication = $this->getDefinitionByType(Application::class); |
36
|
|
|
|
37
|
|
|
foreach ($this->findDefinitionsByType(Command::class) as $name => $definition) { |
38
|
|
|
$consoleApplication->addMethodCall('add', [new Reference($name)]); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool|Definition |
44
|
|
|
*/ |
45
|
|
|
private function getDefinitionByType(string $type) |
46
|
|
|
{ |
47
|
|
|
foreach ($this->containerBuilder->getDefinitions() as $definition) { |
48
|
|
|
if (is_subclass_of($definition->getClass(), $type)) { |
|
|
|
|
49
|
|
|
return $definition; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Definition[] |
58
|
|
|
*/ |
59
|
|
|
private function findDefinitionsByType(string $type) : array |
60
|
|
|
{ |
61
|
|
|
$commands = []; |
62
|
|
|
foreach ($this->containerBuilder->getDefinitions() as $name => $definition) { |
63
|
|
|
if (is_subclass_of($definition->getClass(), $type)) { |
|
|
|
|
64
|
|
|
$commands[$name] = $definition; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $commands; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|