Drush.php

  1. 8.0.x drush.php
  2. 6.x drush.php
  3. 7.x drush.php
  4. 3.x drush.php
  5. 4.x drush.php
  6. 5.x drush.php
  7. master lib/Drush.php
  8. master drush.php

Contains \Drush.

Classes

Namesort descending Description
Drush Static Service Container wrapper.

File

lib/Drush.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drush.
  5. */
  6. use League\Container\ContainerInterface;
  7. use SebastianBergmann\Version;
  8. /**
  9. * Static Service Container wrapper.
  10. *
  11. * This code is analagous to the \Drupal class in Drupal 8.
  12. *
  13. * We would like to move Drush towards the model of using constructor
  14. * injection rather than globals. This class serves as a unified global
  15. * accessor to arbitrary services for use by legacy Drush code.
  16. *
  17. * Advice from Drupal 8's 'Drupal' class:
  18. *
  19. * This class exists only to support legacy code that cannot be dependency
  20. * injected. If your code needs it, consider refactoring it to be object
  21. * oriented, if possible. When this is not possible, and your code is more
  22. * than a few non-reusable lines, it is recommended to instantiate an object
  23. * implementing the actual logic.
  24. *
  25. * @code
  26. * // Legacy procedural code.
  27. * $object = drush_get_context('DRUSH_CLASS_LABEL');
  28. *
  29. * Better:
  30. * $object = \Drush::service('label');
  31. *
  32. * @endcode
  33. */
  34. class Drush {
  35. /**
  36. * The version of Drush from the drush.info file, or FALSE if not read yet.
  37. *
  38. * @var string|FALSE
  39. */
  40. protected static $version = FALSE;
  41. protected static $majorVersion = FALSE;
  42. protected static $minorVersion = FALSE;
  43. /**
  44. * The currently active container object, or NULL if not initialized yet.
  45. *
  46. * @var League\Container\ContainerInterface|null
  47. */
  48. protected static $container;
  49. /**
  50. * The Robo Runner -- manages and constructs all commandfile classes
  51. *
  52. * @var \Robo\Runner
  53. */
  54. protected static $runner;
  55. /**
  56. * Return the current Drush version.
  57. *
  58. * n.b. Called before the DI container is initialized.
  59. * Do not log, etc. here.
  60. */
  61. public static function getVersion() {
  62. if (!static::$version) {
  63. $drush_info = static::drush_read_drush_info();
  64. $instance = new Version($drush_info['drush_version'], DRUSH_BASE_PATH);
  65. static::$version = $instance->getversion();
  66. }
  67. return static::$version;
  68. }
  69. public static function getMajorVersion() {
  70. if (!static::$majorVersion) {
  71. $drush_version = static::getVersion();
  72. $version_parts = explode('.', $drush_version);
  73. static::$majorVersion = $version_parts[0];
  74. }
  75. return static::$majorVersion;
  76. }
  77. public static function getMinorVersion() {
  78. if (!static::$minorVersion) {
  79. $drush_version = static::getVersion();
  80. $version_parts = explode('.', $drush_version);
  81. static::$minorVersion = $version_parts[1];
  82. }
  83. return static::$minorVersion;
  84. }
  85. /**
  86. * Sets a new global container.
  87. *
  88. * @param League\Container\Container $container
  89. * A new container instance to replace the current.
  90. */
  91. public static function setContainer(ContainerInterface $container) {
  92. static::$container = $container;
  93. }
  94. /**
  95. * Unsets the global container.
  96. */
  97. public static function unsetContainer() {
  98. static::$container = NULL;
  99. }
  100. /**
  101. * Returns the currently active global container.
  102. *
  103. * @return League\Container\Container|null
  104. *
  105. * @throws RuntimeException
  106. */
  107. public static function getContainer() {
  108. if (static::$container === NULL) {
  109. throw new \RuntimeException('\Drush::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.');
  110. }
  111. return static::$container;
  112. }
  113. /**
  114. * Returns TRUE if the container has been initialized, FALSE otherwise.
  115. *
  116. * @return bool
  117. */
  118. public static function hasContainer() {
  119. return static::$container !== NULL;
  120. }
  121. /**
  122. * Return the Robo runner.
  123. *
  124. * @return \Robo\Runner
  125. */
  126. public static function runner() {
  127. if (!isset(static::$runner)) {
  128. static::$runner = new \Robo\Runner();
  129. }
  130. return static::$runner;
  131. }
  132. /**
  133. * Retrieves a service from the container.
  134. *
  135. * Use this method if the desired service is not one of those with a dedicated
  136. * accessor method below. If it is listed below, those methods are preferred
  137. * as they can return useful type hints.
  138. *
  139. * @param string $id
  140. * The ID of the service to retrieve.
  141. *
  142. * @return mixed
  143. * The specified service.
  144. */
  145. public static function service($id) {
  146. return static::getContainer()->get($id);
  147. }
  148. /**
  149. * Indicates if a service is defined in the container.
  150. *
  151. * @param string $id
  152. * The ID of the service to check.
  153. *
  154. * @return bool
  155. * TRUE if the specified service exists, FALSE otherwise.
  156. */
  157. public static function hasService($id) {
  158. // Check hasContainer() first in order to always return a Boolean.
  159. return static::hasContainer() && static::getContainer()->has($id);
  160. }
  161. /**
  162. * Return command factory
  163. *
  164. * @return \Consolidation\AnnotatedCommand\AnnotatedCommandFactory
  165. */
  166. public static function commandFactory() {
  167. return static::service('commandFactory');
  168. }
  169. /**
  170. * Return the Drush logger object.
  171. *
  172. * @return LoggerInterface
  173. */
  174. public static function logger() {
  175. return static::service('logger');
  176. }
  177. /**
  178. * Return the Bootstrap Manager.
  179. *
  180. * @return Drush\Boot\BootstrapManager
  181. */
  182. public static function bootstrapManager() {
  183. return static::service('bootstrap.manager');
  184. }
  185. /**
  186. * Return the Bootstrap object.
  187. *
  188. * @return Drush\Boot\Boot
  189. */
  190. public static function bootstrap() {
  191. return static::bootstrapManager()->bootstrap();
  192. }
  193. /**
  194. * Read the drush info file.
  195. */
  196. private static function drush_read_drush_info() {
  197. $drush_info_file = dirname(__FILE__) . '/../drush.info';
  198. return parse_ini_file($drush_info_file);
  199. }
  200. }