|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PsyshBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Théo FIDRY <[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 Fidry\PsyshBundle\DependencyInjection\Compiler; |
|
13
|
|
|
|
|
14
|
|
|
use Psy\Command\Command as PsyshCommand; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Compiler pass allowing to add Psysh commands dynamically |
|
21
|
|
|
* |
|
22
|
|
|
* @author Jérôme Vieilledent <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class AddPsyshCommandPass implements CompilerPassInterface |
|
25
|
|
|
{ |
|
26
|
|
|
public function process(ContainerBuilder $container) |
|
27
|
|
|
{ |
|
28
|
|
|
if (!$container->has('psysh.shell')) { |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// Workaround to avoid Psysh commands to be registered as regular console commands |
|
33
|
|
|
// (conflict with service autoconfiguration as Psysh commands inherit from \Symfony\Component\Console\Command\Command as well |
|
34
|
|
|
// Note that this compiler pass must run with a higher priority than AddConsoleCommandPass to be efficient. |
|
35
|
|
|
foreach ($container->findTaggedServiceIds('console.command') as $id => $attributes) { |
|
36
|
|
|
$definition = $container->findDefinition($id); |
|
37
|
|
|
if (is_subclass_of($definition->getClass(), PsyshCommand::class)) { |
|
|
|
|
|
|
38
|
|
|
$definition->clearTag('console.command'); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$commands = []; |
|
43
|
|
|
foreach ($container->findTaggedServiceIds('psysh.command') as $id => $attributes) { |
|
44
|
|
|
$commands[] = new Reference($id); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$shellRef = $container->findDefinition('psysh.shell'); |
|
49
|
|
|
$shellRef->addMethodCall('addCommands', [$commands]); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|