function drush_init_dependency_injection_container

master preflight.inc drush_init_dependency_injection_container($input = null, $output = null)

Set up our dependency injection container.

The Drupal6 boot service is needed in order to show the D6 deprecation message.

1 call to drush_init_dependency_injection_container()
drush_preflight_prepare in includes/preflight.inc
Prepare Drush for preflight.

File

includes/preflight.inc, line 250
Preflight, postflight and shutdown code.

Code

function drush_init_dependency_injection_container($input = null, $output = null) {
  // Create default input and output objects if they were not provided
  if (!$input) {
    $input = new StringInput('');
  }
  if (!$output) {
    $output = new \Symfony\Component\Console\Output\ConsoleOutput();
  }
  // Set up our dependency injection container.
  $container = new \League\Container\Container();
  $roboConfig = new \Robo\Config(); // TODO: make a global Drush config class derived from \Robo\Config. Then use $drushConfig here instead of $roboConfig

  $application = new \Symfony\Component\Console\Application('Drush Commandline Tool', \Drush::getVersion());

  \Robo\Robo::configureContainer($container, $application, $roboConfig, $input, $output);
  $container->add('container', $container);

  // Override Robo's logger with our own
  $container->share('logger', 'Drush\Log\Logger')->withArgument('output')->withMethodCall('setLogOutputStyler', ['logStyler']);

  // Override Robo's formatter manager with our own
  // @todo not sure that we'll use this. Maybe remove it.
  $container->share('formatterManager', \Drush\Formatters\DrushFormatterManager::)->withMethodCall('addDefaultFormatters', [])->withMethodCall('addDefaultSimplifiers', []);

  // Add some of our own objects to the container
  $container->share('bootstrap.default', 'Drush\Boot\EmptyBoot');
  $container->share('bootstrap.drupal6', 'Drush\Boot\DrupalBoot6');
  $container->share('bootstrap.drupal7', 'Drush\Boot\DrupalBoot7');
  $container->share('bootstrap.drupal8', 'Drush\Boot\DrupalBoot8');
  $container->share('bootstrap.manager', 'Drush\Boot\BootstrapManager')->withArgument('bootstrap.default');
  $container->extend('bootstrap.manager')->withMethodCall('add', ['bootstrap.drupal6'])->withMethodCall('add', ['bootstrap.drupal7'])->withMethodCall('add', ['bootstrap.drupal8']);

  // Robo does not manage the command discovery object in the container,
  // but we will register and configure one for our use.
  $container->share('commandDiscovery', 'Consolidation\AnnotatedCommand\CommandFileDiscovery')->withMethodCall('addSearchLocation', ['CommandFiles'])->withMethodCall('setSearchPattern', ['#.*(Commands|CommandFile).php$#']);

  // Store the container in the \Drush object
  \Drush::setContainer($container);
  \Robo\Robo::setContainer($container);

  // Add our own callback to the hook manager
  $hookManager = $container->get('hookManager');
  $hookManager->addOutputExtractor(new Drush\Backend\BackendResultSetter());
  // @todo: do we need both backend result setters? The one below should be removed at some point.
  $hookManager->add('annotatedcomand_adapter_backend_result', HookManager::EXTRACT_OUTPUT);

  $factory = $container->get('commandFactory');
  $factory->setIncludeAllPublicMethods(false);

  // It is necessary to set the dispatcher when using configureContainer
  $eventDispatcher = $container->get('eventDispatcher');
  $eventDispatcher->addSubscriber(new \Drush\Command\GlobalOptionsEventListener());
  $application->setDispatcher($eventDispatcher);

  return $container;
}