Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3123 - Global D8 container is not available during container rebuild. #3124

Merged
merged 8 commits into from
Nov 4, 2017
14 changes: 14 additions & 0 deletions src/Drupal/DrupalKernel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Drush\Drupal;

use Drupal\Core\Site\Settings;
use Drush\Log\LogLevel;
use Drupal\Core\DrupalKernel as DrupalDrupalKernel;
use Drupal\Core\DependencyInjection\ServiceModifierInterface;
Expand Down Expand Up @@ -45,6 +46,19 @@ protected function initializeContainer()
$container_definition = $this->getCachedContainerDefinition();

if ($this->shouldDrushInvalidateContainer()) {
// Normally when the container is being rebuilt, the existing
// container is still available for use until the newly built one
// replaces it. Certain contrib modules rely on services (like State
// or the config factory) being available for things like defining
// event subscriptions.
// @see https://github.com/drush-ops/drush/issues/3123
if (isset($container_definition)) {
$class = Settings::get('container_base_class', '\Drupal\Core\DependencyInjection\Container');
$container = new $class($container_definition);
$this->attachSynthetic($container);
\Drupal::setContainer($container);
}

$this->invalidateContainer();
}
return parent::initializeContainer();
Expand Down
63 changes: 63 additions & 0 deletions tests/containerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Unish;

use Webmozart\PathUtil\Path;

/**
* Tests the Drush override of DrupalKernel.
*
* @group base
*
* @see https://github.com/drush-ops/drush/issues/3123
*/
class containerTest extends CommandUnishTestCase {

/**
* Tests that the existing container is available while Drush rebuilds it.
*/
public function testContainer() {
$this->setUpDrupal(1, TRUE);
$root = $this->webroot();
$options = array(
'root' => $root,
'uri' => $this->getUri(),
'yes' => NULL,
);

// Copy the 'woot' module over to the Drupal site we just set up.
$this->setupModulesForTests($root);

// Enable our module.
$this->drush('pm-enable', ['woot'], $options);

// Set up for a config import with just one small piece.
$this->drush('config-export', array(), $options);
$this->drush('config-set', array('system.site', 'name', 'config_test'), $options);

// Trigger the container rebuild we need.
$this->drush('cr', [], $options);
$this->drush('cron', [], $options);

// If the event was registered successfully, then upon a config import, we
// should get the error message.
$this->drush('config-import', [], $options, NULL, NULL, CommandUnishTestCase::EXIT_ERROR);
$this->assertContains("woot config error", $this->getErrorOutput(), 'Event was successfully registered.');
}

/**
* Sets up the woot module for the test.
*
* @param string $root
* The web root.
*/
public function setupModulesForTests($root) {
$wootModule = Path::join(__DIR__, '/resources/modules/d8/woot');
// We install into Unish so that we aren't cleaned up. That causes
// container to go invalid after tearDownAfterClass().
$targetDir = Path::join($root, 'modules/unish/woot');
$this->mkdir($targetDir);
$this->recursive_copy($wootModule, $targetDir);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Drupal\woot\EventSubscriber;

use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\ConfigImporterEvent;
use Drupal\Core\Config\ConfigImportValidateEventSubscriberBase;

/**
* Subscribes to Symfony events and maps them to Rules events.
*/
class ConfigSubscriber extends ConfigImportValidateEventSubscriberBase {

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [];

// In this example, we would use information from the State API to determine
// what events we should subscribe to. Suffice it to say we trust that the
// State API works correctly, so we're only going to check if the service is
// available here to make our point.
if (\Drupal::hasService('state')) {
$events[ConfigEvents::IMPORT_VALIDATE][] = 'onConfigImporterValidate';
}

return $events;
}

/**
* {@inheritdoc}
*/
public function onConfigImporterValidate(ConfigImporterEvent $event) {
// Always log an error.
$importer = $event->getConfigImporter();
$importer->logError($this->t('woot config error'));
}

}
5 changes: 5 additions & 0 deletions tests/resources/modules/d8/woot/woot.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
config.config_subscriber:
class: Drupal\woot\EventSubscriber\ConfigSubscriber
tags:
- { name: event_subscriber }