Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / drush / drush / src / Drush.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drush.
6  */
7 namespace Drush;
8
9 use Drush\SiteAlias\SiteAliasManager;
10 use League\Container\ContainerInterface;
11 use Psr\Log\LoggerInterface;
12 use SebastianBergmann\Version;
13 use Symfony\Component\Console\Application;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Output\OutputInterface;
16
17 // TODO: Not sure if we should have a reference to PreflightArgs here.
18 // Maybe these constants should be in config, and PreflightArgs can
19 // reference them from there as well.
20 use Drush\Preflight\PreflightArgs;
21
22 /**
23  * Static Service Container wrapper.
24  *
25  * This code is analogous to the \Drupal class in Drupal 8.
26  *
27  * We would like to move Drush towards the model of using constructor
28  * injection rather than globals. This class serves as a unified global
29  * accessor to arbitrary services for use by legacy Drush code.
30  *
31  * Advice from Drupal 8's 'Drupal' class:
32  *
33  * This class exists only to support legacy code that cannot be dependency
34  * injected. If your code needs it, consider refactoring it to be object
35  * oriented, if possible. When this is not possible, and your code is more
36  * than a few non-reusable lines, it is recommended to instantiate an object
37  * implementing the actual logic.
38  *
39  * @code
40  *   // Legacy procedural code.
41  *   $object = drush_get_context('DRUSH_CLASS_LABEL');
42  *
43  * Better:
44  *   $object = Drush::service('label');
45  *
46  * @endcode
47  */
48 class Drush
49 {
50
51     /**
52      * The version of Drush from the drush.info file, or FALSE if not read yet.
53      *
54      * @var string|FALSE
55      */
56     protected static $version = false;
57     protected static $majorVersion = false;
58     protected static $minorVersion = false;
59
60     /**
61      * The currently active container object, or NULL if not initialized yet.
62      *
63      * @var \League\Container\ContainerInterface|null
64      */
65     protected static $container;
66
67     /**
68      * The Robo Runner -- manages and constructs all commandfile classes
69      *
70      * @var \Robo\Runner
71      */
72     protected static $runner;
73
74     /**
75      * Return the current Drush version.
76      *
77      * n.b. Called before the DI container is initialized.
78      * Do not log, etc. here.
79      */
80     public static function getVersion()
81     {
82         if (!static::$version) {
83             $drush_info = static::drushReadDrushInfo();
84             $instance = new Version($drush_info['drush_version'], dirname(__DIR__));
85             static::$version = $instance->getversion();
86         }
87         return static::$version;
88     }
89
90     public static function getMajorVersion()
91     {
92         if (!static::$majorVersion) {
93             $drush_version = static::getVersion();
94             $version_parts = explode('.', $drush_version);
95             static::$majorVersion = $version_parts[0];
96         }
97         return static::$majorVersion;
98     }
99
100     public static function getMinorVersion()
101     {
102         if (!static::$minorVersion) {
103             $drush_version = static::getVersion();
104             $version_parts = explode('.', $drush_version);
105             static::$minorVersion = $version_parts[1];
106         }
107         return static::$minorVersion;
108     }
109
110     /**
111      * Sets a new global container.
112      *
113      * @param \League\Container\Container $container
114      *   A new container instance to replace the current.
115      */
116     public static function setContainer(ContainerInterface $container)
117     {
118         static::$container = $container;
119     }
120
121     /**
122      * Unsets the global container.
123      */
124     public static function unsetContainer()
125     {
126         static::$container = null;
127     }
128
129     /**
130      * Returns the currently active global container.
131      *
132      * @return \League\Container\ContainerInterface|null
133      *
134      * @throws RuntimeException
135      */
136     public static function getContainer()
137     {
138         if (static::$container === null) {
139             debug_print_backtrace();
140             throw new \RuntimeException('Drush::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.');
141         }
142         return static::$container;
143     }
144
145     /**
146      * Returns TRUE if the container has been initialized, FALSE otherwise.
147      *
148      * @return bool
149      */
150     public static function hasContainer()
151     {
152         return static::$container !== null;
153     }
154
155     /**
156      * Get the current Symfony Console Application.
157      *
158      * @return Application
159      */
160     public static function getApplication()
161     {
162         return self::getContainer()->get('application');
163     }
164
165     /**
166      * Return the Robo runner.
167      *
168      * @return \Robo\Runner
169      */
170     public static function runner()
171     {
172         if (!isset(static::$runner)) {
173             static::$runner = new \Robo\Runner();
174         }
175         return static::$runner;
176     }
177
178     /**
179      * Retrieves a service from the container.
180      *
181      * Use this method if the desired service is not one of those with a dedicated
182      * accessor method below. If it is listed below, those methods are preferred
183      * as they can return useful type hints.
184      *
185      * @param string $id
186      *   The ID of the service to retrieve.
187      *
188      * @return mixed
189      *   The specified service.
190      */
191     public static function service($id)
192     {
193         return static::getContainer()->get($id);
194     }
195
196     /**
197      * Indicates if a service is defined in the container.
198      *
199      * @param string $id
200      *   The ID of the service to check.
201      *
202      * @return bool
203      *   TRUE if the specified service exists, FALSE otherwise.
204      */
205     public static function hasService($id)
206     {
207         // Check hasContainer() first in order to always return a Boolean.
208         return static::hasContainer() && static::getContainer()->has($id);
209     }
210
211     /**
212      * Return command factory
213      *
214      * @return \Consolidation\AnnotatedCommand\AnnotatedCommandFactory
215      */
216     public static function commandFactory()
217     {
218         return static::service('commandFactory');
219     }
220
221     /**
222      * Return the Drush logger object.
223      *
224      * @return LoggerInterface
225      */
226     public static function logger()
227     {
228         return static::service('logger');
229     }
230
231     /**
232      * Return the configuration object
233      *
234      * @return \Drush\Config\DrushConfig
235      */
236     public static function config()
237     {
238         return static::service('config');
239     }
240
241     /**
242      * @return SiteAliasManager
243      */
244     public static function aliasManager()
245     {
246         return static::service('site.alias.manager');
247     }
248
249     /**
250      * Return the input object
251      *
252      * @return InputInterface
253      */
254     public static function input()
255     {
256         return static::service('input');
257     }
258
259     /**
260      * Return the output object
261      *
262      * @return OutputInterface
263      */
264     public static function output()
265     {
266         return static::service('output');
267     }
268
269     /**
270      * Return 'true' if we are in simulated mode
271      */
272     public static function simulate()
273     {
274         return \Drush\Drush::config()->get(\Robo\Config\Config::SIMULATE);
275     }
276
277     /**
278      * Return 'true' if we are in backend mode
279      */
280     public static function backend()
281     {
282         return \Drush\Drush::config()->get(PreflightArgs::BACKEND);
283     }
284
285     /**
286      * Return 'true' if we are in affirmative mode
287      */
288     public static function affirmative()
289     {
290         if (!static::hasService('input')) {
291             throw new \Exception('No input service available.');
292         }
293         return Drush::input()->getOption('yes') || (Drush::backend() && !Drush::negative());
294     }
295
296     /**
297      * Return 'true' if we are in negative mode
298      */
299     public static function negative()
300     {
301         if (!static::hasService('input')) {
302             throw new \Exception('No input service available.');
303         }
304         return Drush::input()->getOption('no');
305     }
306
307     /**
308      * Return 'true' if we are in verbose mode
309      */
310     public static function verbose()
311     {
312         if (!static::hasService('output')) {
313             return false;
314         }
315         return \Drush\Drush::output()->isVerbose();
316     }
317
318     /**
319      * Return 'true' if we are in debug mode
320      */
321     public static function debug()
322     {
323         if (!static::hasService('output')) {
324             return false;
325         }
326         return \Drush\Drush::output()->isDebug();
327     }
328
329     /**
330      * Return the Bootstrap Manager.
331      *
332      * @return \Drush\Boot\BootstrapManager
333      */
334     public static function bootstrapManager()
335     {
336         return static::service('bootstrap.manager');
337     }
338
339     /**
340      * Return the Bootstrap object.
341      *
342      * @return \Drush\Boot\Boot
343      */
344     public static function bootstrap()
345     {
346         return static::bootstrapManager()->bootstrap();
347     }
348
349     public static function redispatchOptions($input = null)
350     {
351         $input = $input ?: static::input();
352
353         // $input->getOptions() returns an associative array of option => value
354         $options = $input->getOptions();
355
356         // The 'runtime.options' config contains a list of option names on th cli
357         $optionNamesFromCommandline = static::config()->get('runtime.options');
358
359         // Remove anything in $options that was not on the cli
360         $options = array_intersect_key($options, array_flip($optionNamesFromCommandline));
361
362         // Add in the 'runtime.context' items, which includes --include, --alias-path et. al.
363         return $options + array_filter(static::config()->get(PreflightArgs::DRUSH_RUNTIME_CONTEXT_NAMESPACE));
364     }
365
366     /**
367      * Read the drush info file.
368      */
369     private static function drushReadDrushInfo()
370     {
371         $drush_info_file = dirname(__FILE__) . '/../drush.info';
372
373         return parse_ini_file($drush_info_file);
374     }
375 }