Version 1
[yaffs-website] / vendor / drush / drush / lib / Drush / Drupal / FindCommandsCompilerPass.php
1 <?php
2 namespace Drush\Drupal;
3
4 use Drush\Log\LogLevel;
5 use Symfony\Component\DependencyInjection\ContainerBuilder;
6 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7 use Symfony\Component\DependencyInjection\Reference;
8
9 /**
10  * This compiler pass is added to Drupal's ContainerBuilder by our own
11  * subclass of DrupalKernel.  Our DrupalKernel subclass knows which
12  * compiler passes to add because they are registered to it via its
13  * 'alter()' method. This happens in DrupalBoot8 immediately after the
14  * DrupalKernel object is created.
15  *
16  * Having been thus added, this compiler pass will then be called during
17  * $kernel->boot(), when Drupal's dependency injection container is being
18  * compiled.  Since we cannot use the container at this point (since its
19  * initialization is not yet complete), we instead alter the definition of
20  * a storage class in the container to add more setter injection method
21  * calls to 'addCommandReference'.
22  *
23  * Later, after the container has been completely initialized, we can
24  * fetch the storage class from the DI container (perhaps also via
25  * injection from a reference in the container).  At that point, we can
26  * request the list of Console commands that were added via the
27  * (delayed) call(s) to addCommandReference.
28  *
29  * Documentation:
30  *
31  * http://symfony.com/doc/2.7/components/dependency_injection/tags.html#create-a-compilerpass
32  */
33 class FindCommandsCompilerPass implements CompilerPassInterface
34 {
35     protected $storageClassId;
36     protected $tagId;
37
38     public function __construct($storageClassId, $tagId)
39     {
40         $this->storageClassId = $storageClassId;
41         $this->tagId = $tagId;
42     }
43
44     public function process(ContainerBuilder $container)
45     {
46         drush_log(dt("process !storage !tag", ['!storage' => $this->storageClassId, '!tag' => $this->tagId]), LogLevel::DEBUG);
47         // We expect that our called registered the storage
48         // class under the storage class id before adding this
49         // compiler pass, but we will test this presumption to be sure.
50         if (!$container->has($this->storageClassId)) {
51             drush_log(dt("storage class not registered"), LogLevel::DEBUG);
52             return;
53         }
54
55         $definition = $container->findDefinition(
56             $this->storageClassId
57         );
58
59         $taggedServices = $container->findTaggedServiceIds(
60             $this->tagId
61         );
62         foreach ($taggedServices as $id => $tags) {
63             drush_log(dt("found tagged service !id", ['!id' => $id]), LogLevel::DEBUG);
64             $definition->addMethodCall(
65                 'addCommandReference',
66                 array(new Reference($id))
67             );
68         }
69     }
70 }