-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
DependencyInjection.php
187 lines (160 loc) · 7.94 KB
/
DependencyInjection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace Drush\Runtime;
use Drush\Log\Logger;
use League\Container\Container;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Robo\Robo;
use Drush\Formatters\DrushFormatterManager;
use Drush\Boot\AutoloaderAwareInterface;
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
use Consolidation\SiteProcess\ProcessManagerAwareInterface;
use Drush\Command\GlobalOptionsEventListener;
use Drush\Drush;
use Drush\Symfony\DrushStyleInjector;
use Drush\Cache\CommandCache;
use DrupalFinder\DrupalFinder;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Application;
use Consolidation\Config\ConfigInterface;
use Composer\Autoload\ClassLoader;
use League\Container\ContainerInterface;
use Consolidation\SiteAlias\SiteAliasManager;
use Drush\Command\DrushCommandInfoAlterer;
use Consolidation\Config\Util\ConfigOverlay;
use Drush\Config\DrushConfig;
use Drush\SiteAlias\ProcessManager;
/**
* Prepare our Dependency Injection Container
*/
class DependencyInjection
{
protected $handlers = [];
public function desiredHandlers($handlerList): void
{
$this->handlers = $handlerList;
}
/**
* Set up our dependency injection container.
*/
public function initContainer(
Application $application,
ConfigInterface $config,
InputInterface $input,
OutputInterface $output,
ClassLoader $loader,
DrupalFinder $drupalFinder,
SiteAliasManager $aliasManager
): Container {
// Create default input and output objects if they were not provided
if (!$input) {
$input = new StringInput('');
}
if (!$output) {
$output = new ConsoleOutput();
}
// Set up our dependency injection container.
$container = new Container();
// With league/container 3.x, first call wins, so add Drush services first.
$this->addDrushServices($container, $loader, $drupalFinder, $aliasManager, $config, $output);
// Robo has the same signature for configureContainer in 1.x, 2.x and 3.x.
Robo::configureContainer($container, $application, $config, $input, $output);
$container->add('container', $container);
// Store the container in the \Drush object
Drush::setContainer($container);
// Change service definitions as needed for our application.
$this->alterServicesForDrush($container, $application);
// Inject needed services into our application object.
$this->injectApplicationServices($container, $application);
return $container;
}
/**
* Make sure we are notified on exit, and when bad things happen.
*/
public function installHandlers($container): void
{
foreach ($this->handlers as $handlerId) {
$handler = $container->get($handlerId);
$handler->installHandler();
}
}
// Add Drush Services to league/container 3.x
protected function addDrushServices($container, ClassLoader $loader, DrupalFinder $drupalFinder, SiteAliasManager $aliasManager, DrushConfig $config, OutputInterface $output): void
{
// Override Robo's logger with a LoggerManager that delegates to the Drush logger.
$container->share('logger', '\Drush\Log\DrushLoggerManager')
->addMethodCall('setLogOutputStyler', ['logStyler'])
->addMethodCall('add', ['drush', new Logger($output)]);
$container->share('loader', $loader);
$container->share('site.alias.manager', $aliasManager);
// Fetch the runtime config, where -D et. al. are stored, and
// add a reference to it to the container.
$container->share('config.runtime', $config->getContext(ConfigOverlay::PROCESS_CONTEXT));
// Override Robo's formatter manager with our own
// @todo not sure that we'll use this. Maybe remove it.
$container->share('formatterManager', DrushFormatterManager::class)
->addMethodCall('addDefaultFormatters', [])
->addMethodCall('addDefaultSimplifiers', []);
// Add some of our own objects to the container
$container->share('bootstrap.drupal8', 'Drush\Boot\DrupalBoot8');
$container->share('bootstrap.manager', 'Drush\Boot\BootstrapManager')
->addMethodCall('setDrupalFinder', [$drupalFinder]);
// TODO: Can we somehow add these via discovery (e.g. backdrop extension?)
$container->extend('bootstrap.manager')
->addMethodCall('add', ['bootstrap.drupal8']);
$container->share('bootstrap.hook', 'Drush\Boot\BootstrapHook')
->addArgument('bootstrap.manager');
$container->share('tildeExpansion.hook', 'Drush\Runtime\TildeExpansionHook');
$container->share('process.manager', ProcessManager::class)
->addMethodCall('setConfig', ['config'])
->addMethodCall('setConfigRuntime', ['config.runtime']);
$container->share('redispatch.hook', 'Drush\Runtime\RedispatchHook')
->addArgument('process.manager');
// Robo does not manage the command discovery object in the container,
// but we will register and configure one for our use.
// TODO: Some old adapter code uses this, but the Symfony dispatcher does not.
// See Application::commandDiscovery().
$container->share('commandDiscovery', 'Consolidation\AnnotatedCommand\CommandFileDiscovery')
->addMethodCall('addSearchLocation', ['CommandFiles'])
->addMethodCall('setSearchPattern', ['#.*(Commands|CommandFile).php$#']);
// Error and Shutdown handlers
$container->share('errorHandler', 'Drush\Runtime\ErrorHandler');
$container->share('shutdownHandler', 'Drush\Runtime\ShutdownHandler');
// Add inflectors. @see \Drush\Boot\BaseBoot::inflect
$container->inflector(AutoloaderAwareInterface::class)
->invokeMethod('setAutoloader', ['loader']);
$container->inflector(SiteAliasManagerAwareInterface::class)
->invokeMethod('setSiteAliasManager', ['site.alias.manager']);
$container->inflector(ProcessManagerAwareInterface::class)
->invokeMethod('setProcessManager', ['process.manager']);
}
protected function alterServicesForDrush($container, Application $application): void
{
$paramInjection = $container->get('parameterInjection');
$paramInjection->register('Symfony\Component\Console\Style\SymfonyStyle', new DrushStyleInjector());
// Add our own callback to the hook manager
$hookManager = $container->get('hookManager');
$hookManager->addCommandEvent(new GlobalOptionsEventListener());
$hookManager->addInitializeHook($container->get('redispatch.hook'));
$hookManager->addInitializeHook($container->get('bootstrap.hook'));
$hookManager->addPreValidator($container->get('tildeExpansion.hook'));
$factory = $container->get('commandFactory');
$factory->setIncludeAllPublicMethods(false);
$factory->setIgnoreCommandsInTraits(true);
$factory->addCommandInfoAlterer(new DrushCommandInfoAlterer());
$commandProcessor = $container->get('commandProcessor');
$commandProcessor->setPassExceptions(true);
ProcessManager::addTransports($container->get('process.manager'));
}
protected function injectApplicationServices($container, Application $application): void
{
$application->setLogger($container->get('logger'));
$application->setBootstrapManager($container->get('bootstrap.manager'));
$application->setAliasManager($container->get('site.alias.manager'));
$application->setRedispatchHook($container->get('redispatch.hook'));
$application->setTildeExpansionHook($container->get('tildeExpansion.hook'));
$application->setDispatcher($container->get('eventDispatcher'));
$application->setConfig($container->get('config'));
}
}