forked from acquia/blt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlt.php
249 lines (222 loc) · 7.86 KB
/
Blt.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace Acquia\Blt\Robo;
use Acquia\Blt\Robo\Common\Executor;
use Acquia\Blt\Robo\Datastore\FileStore;
use Acquia\Blt\Robo\Filesets\FilesetManager;
use Acquia\Blt\Robo\Inspector\Inspector;
use Acquia\Blt\Robo\Inspector\InspectorAwareInterface;
use Acquia\Blt\Robo\Log\BltLogStyle;
use Acquia\Blt\Robo\Wizards\SetupWizard;
use Acquia\Blt\Robo\Wizards\TestsWizard;
use Consolidation\AnnotatedCommand\CommandFileDiscovery;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Robo\Collection\CollectionBuilder;
use Robo\Common\ConfigAwareTrait;
use Robo\Config\Config;
use Robo\Robo;
use Robo\Runner as RoboRunner;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* The BLT Robo application.
*/
class Blt implements ContainerAwareInterface, LoggerAwareInterface {
use ConfigAwareTrait;
use ContainerAwareTrait;
use LoggerAwareTrait;
/**
* The Robo task runner.
*
* @var \Robo\Runner
*/
private $runner;
/**
* An array of Robo commands available to the application.
*
* @var string[]
*/
private $commands = [];
/**
* Object constructor.
*
* @param \Robo\Config\Config $config
* The BLT configuration.
* @param \Symfony\Component\Console\Input\InputInterface $input
* The input.
* @param \Symfony\Component\Console\Output\OutputInterface $output
* The output.
*/
public function __construct(
Config $config,
InputInterface $input = NULL,
OutputInterface $output = NULL
) {
$this->setConfig($config);
$application = new Application('BLT', $config->get('version'));
$container = Robo::createDefaultContainer($input, $output, $application,
$config);
$this->setContainer($container);
$this->addDefaultArgumentsAndOptions($application);
$this->configureContainer($container);
$this->addBuiltInCommandsAndHooks();
$this->addPluginsCommandsAndHooks();
$this->runner = new RoboRunner();
$this->runner->setContainer($container);
$this->setLogger($container->get('logger'));
$this->validateEnvironment();
}
/**
* Add the commands and hooks which are shipped with core BLT.
*/
private function addBuiltInCommandsAndHooks() {
$commands = $this->getCommands([
'path' => __DIR__ . '/Commands',
'namespace' => 'Acquia\Blt\Robo\Commands',
]);
$hooks = $this->getHooks([
'path' => __DIR__ . '/Hooks',
'namespace' => 'Acquia\Blt\Robo\Hooks',
]);
$this->commands = array_merge($commands, $hooks);
}
/**
* Registers custom commands and hooks defined project.
*/
private function addPluginsCommandsAndHooks() {
$commands = $this->getCommands([
'path' => $this->getConfig()->get('repo.root') . '/blt/src/Commands',
'namespace' => 'Acquia\Blt\Custom\Commands',
]);
$hooks = $this->getHooks([
'path' => $this->getConfig()->get('repo.root') . '/blt/src/Hooks',
'namespace' => 'Acquia\Blt\Custom\Hooks',
]);
$plugin_commands_hooks = array_merge($commands, $hooks);
$this->commands = array_merge($this->commands, $plugin_commands_hooks);
}
/**
* Discovers command classes using CommandFileDiscovery.
*
* @param string[] $options
* Elements as follow
* string path The full path to the directory to search for commands
* string namespace The full namespace for the command directory.
*
* @return array
* An array of Command classes
*/
private function getCommands(
array $options = ['path' => NULL, 'namespace' => NULL]
) {
$discovery = new CommandFileDiscovery();
$discovery
->setSearchPattern('*Command.php')
->setSearchLocations([])
->addExclude('Internal');
return $discovery->discover($options['path'], $options['namespace']);
}
/**
* Discovers hooks using CommandFileDiscovery.
*
* @param string[] $options
* Elements as follow
* string path The full path to the directory to search for commands
* string namespace The full namespace for the command directory.
*
* @return array
* An array of Hook classes
*/
private function getHooks(
array $options = ['path' => NULL, 'namespace' => NULL]
) {
$discovery = new CommandFileDiscovery();
$discovery->setSearchPattern('*Hook.php')->setSearchLocations([]);
return $discovery->discover($options['path'], $options['namespace']);
}
/**
* Add any global arguments or options that apply to all commands.
*
* @param \Symfony\Component\Console\Application $app
* The Symfony application.
*/
private function addDefaultArgumentsAndOptions(Application $app) {
$app->getDefinition()->addOption(new InputOption('--yes', '-y',
InputOption::VALUE_NONE, 'Answer all confirmations with "yes"'));
$app->getDefinition()
->addOption(
new InputOption('--define', '-D', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Define a configuration item value.', [])
);
}
/**
* Register the necessary classes for BLT.
*/
public function configureContainer($container) {
$container->share('logStyler', BltLogStyle::class);
// We create our own builder so that non-command classes are able to
// implement task methods, like taskExec(). Yes, there are now two builders
// in the container. "collectionBuilder" used for the actual command that
// was executed, and "builder" to be used with non-command classes.
$blt_tasks = new BltTasks();
$builder = new CollectionBuilder($blt_tasks);
$blt_tasks->setBuilder($builder);
$container->add('builder', $builder);
$container->add('executor', Executor::class)
->withArgument('builder');
$container->share('inspector', Inspector::class)
->withArgument('executor');
$container->inflector(InspectorAwareInterface::class)
->invokeMethod('setInspector', ['inspector']);
$container->add(SetupWizard::class)
->withArgument('executor');
$container->add(TestsWizard::class)
->withArgument('executor');
$container->share('filesetManager', FilesetManager::class);
/** @var \Consolidation\AnnotatedCommand\AnnotatedCommandFactory $factory */
$factory = $container->get('commandFactory');
// Tell the command loader to only allow command functions that have a
// name/alias.
$factory->setIncludeAllPublicMethods(FALSE);
$factory->addCommandInfoAlterer(new BltCommandInfoAlterer());
// Install our command cache into the command factory.
$commandCacheDir = $this->getConfig()->get('blt.command-cache-dir');
if (file_exists($commandCacheDir) && is_writable($commandCacheDir)) {
$commandCacheDataStore = new FileStore($commandCacheDir);
$factory->setDataStore($commandCacheDataStore);
}
}
/**
* Runs the instantiated BLT application.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* An input object to run the application with.
* @param \Symfony\Component\Console\Output\OutputInterface $output
* An output object to run the application with.
*
* @return int
* The exiting status code of the application
*/
public function run(InputInterface $input, OutputInterface $output) {
$status_code = $this->runner->run($input, $output, NULL, $this->commands);
return $status_code;
}
/**
* Validates that environment is BLT compatible.
*/
protected function validateEnvironment() {
$this->warnIfXDebugLoaded();
}
/**
* Warns the user if the xDebug extension is loaded.
*/
protected function warnIfXdebugLoaded() {
$xdebug_loaded = extension_loaded('xdebug');
if ($xdebug_loaded) {
$this->logger->warning("The xDebug extension is loaded. This will significantly decrease performance.");
}
}
}