|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
|
|
|
|
|
|
6
|
|
|
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader; |
|
|
|
|
|
|
7
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
|
|
|
|
|
8
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; |
|
|
|
|
|
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
|
|
|
|
|
10
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
|
|
|
|
|
11
|
|
|
use Symfony\Component\DependencyInjection\TypedReference; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Just like AddConsoleCommandPass, but only loads 'dbconsole' commands (which use a different tag) |
|
15
|
|
|
*/ |
|
|
|
|
|
|
16
|
|
|
class AddDBConsoleCommandPass implements CompilerPassInterface |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
private $commandLoaderServiceId; |
|
|
|
|
|
|
20
|
|
|
private $commandTag; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
public function __construct(string $commandLoaderServiceId = 'dbconsole.command_loader', string $commandTag = 'dbconsole.command') |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
$this->commandLoaderServiceId = $commandLoaderServiceId; |
|
25
|
|
|
$this->commandTag = $commandTag; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function process(ContainerBuilder $container) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$commandServices = $container->findTaggedServiceIds($this->commandTag, true); |
|
31
|
|
|
$lazyCommandMap = []; |
|
32
|
|
|
$lazyCommandRefs = []; |
|
33
|
|
|
$serviceIds = []; |
|
34
|
|
|
|
|
35
|
|
|
foreach ($commandServices as $id => $tags) { |
|
36
|
|
|
$definition = $container->getDefinition($id); |
|
37
|
|
|
$class = $container->getParameterBag()->resolveValue($definition->getClass()); |
|
38
|
|
|
|
|
39
|
|
|
if (isset($tags[0]['command'])) { |
|
40
|
|
|
$commandName = $tags[0]['command']; |
|
41
|
|
|
} else { |
|
42
|
|
|
if (!$r = $container->getReflectionClass($class)) { |
|
43
|
|
|
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); |
|
44
|
|
|
} |
|
45
|
|
|
if (!$r->isSubclassOf(Command::class)) { |
|
46
|
|
|
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class)); |
|
47
|
|
|
} |
|
48
|
|
|
$commandName = $class::getDefaultName(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// the magic sauce! |
|
52
|
|
|
$commandName = str_replace('db3v4l:', '', $commandName); |
|
53
|
|
|
|
|
54
|
|
|
if (null === $commandName) { |
|
55
|
|
|
if (!$definition->isPublic() || $definition->isPrivate()) { |
|
56
|
|
|
$commandId = 'dbconsole.command.public_alias.'.$id; |
|
57
|
|
|
$container->setAlias($commandId, $id)->setPublic(true); |
|
58
|
|
|
$id = $commandId; |
|
59
|
|
|
} |
|
60
|
|
|
$serviceIds[] = $id; |
|
61
|
|
|
|
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
unset($tags[0]); |
|
66
|
|
|
$lazyCommandMap[$commandName] = $id; |
|
67
|
|
|
$lazyCommandRefs[$id] = new TypedReference($id, $class); |
|
68
|
|
|
$aliases = []; |
|
69
|
|
|
|
|
70
|
|
|
foreach ($tags as $tag) { |
|
71
|
|
|
if (isset($tag['command'])) { |
|
72
|
|
|
$aliases[] = $tag['command']; |
|
73
|
|
|
$lazyCommandMap[$tag['command']] = $id; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$definition->addMethodCall('setName', [$commandName]); |
|
78
|
|
|
|
|
79
|
|
|
if ($aliases) { |
|
80
|
|
|
$definition->addMethodCall('setAliases', [$aliases]); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$container |
|
85
|
|
|
->register($this->commandLoaderServiceId, ContainerCommandLoader::class) |
|
86
|
|
|
->setPublic(true) |
|
87
|
|
|
->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]); |
|
88
|
|
|
|
|
89
|
|
|
$container->setParameter('dbconsole.command.ids', $serviceIds); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|