2 namespace Drush\Drupal;
4 use Drush\Log\LogLevel;
5 use Symfony\Component\DependencyInjection\ContainerBuilder;
6 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7 use Symfony\Component\DependencyInjection\Reference;
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.
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'.
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.
31 * http://symfony.com/doc/2.7/components/dependency_injection/tags.html#create-a-compilerpass
33 class FindCommandsCompilerPass implements CompilerPassInterface
35 protected $storageClassId;
38 public function __construct($storageClassId, $tagId)
40 $this->storageClassId = $storageClassId;
41 $this->tagId = $tagId;
44 public function process(ContainerBuilder $container)
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);
55 $definition = $container->findDefinition(
59 $taggedServices = $container->findTaggedServiceIds(
62 foreach ($taggedServices as $id => $tags) {
63 drush_log(dt("Found tagged service !id", ['!id' => $id]), LogLevel::DEBUG_NOTIFY);
64 $definition->addMethodCall(
65 'addCommandReference',