BootstrapManager.php

Namespace

Drush\Boot

Classes

Namesort descending Description
BootstrapManager

File

lib/Drush/Boot/BootstrapManager.php
View source
  1. <?php
  2. namespace Drush\Boot;
  3. use Psr\Log\LoggerInterface;
  4. use Psr\Log\LoggerAwareInterface;
  5. use Psr\Log\LoggerAwareTrait;
  6. class BootstrapManager implements LoggerAwareInterface {
  7. use LoggerAwareTrait;
  8. /**
  9. * @var \Drush\Boot\Boot[]
  10. */
  11. protected $bootstrapCandidates = [];
  12. /**
  13. * @var \Drush\Boot\Boot
  14. */
  15. protected $defaultBootstrapObject;
  16. /**
  17. * @var \Drush\Boot\Boot
  18. */
  19. protected $bootstrap;
  20. /**
  21. * @var string
  22. */
  23. protected $root;
  24. /**
  25. * @var string
  26. */
  27. protected $uri;
  28. /**
  29. * Constructor.
  30. *
  31. * @param \Drush\Boot\Boot
  32. * The default bootstrap object to use when there are
  33. * no viable candidates to use (e.g. no selected site)
  34. */
  35. public function __construct(Boot $default) {
  36. $this->defaultBootstrapObject = $default;
  37. }
  38. /**
  39. * Add a bootstrap object to the list of candidates
  40. *
  41. * @param \Drush\Boot\Boot|Array
  42. * List of boot candidates
  43. */
  44. public function add($candidateList) {
  45. foreach (func_get_args() as $candidate) {
  46. $this->bootstrapCandidates[] = $candidate;
  47. }
  48. }
  49. /**
  50. * Return the framework root selected by the user.
  51. */
  52. public function getRoot() {
  53. return $this->root;
  54. }
  55. public function setRoot($root) {
  56. // TODO: Throw if we already bootstrapped a framework?
  57. $this->root = $root;
  58. }
  59. /**
  60. * Return the framework root selected by the user.
  61. */
  62. public function getUri() {
  63. return $this->uri;
  64. }
  65. public function setUri($uri) {
  66. // TODO: Throw if we already bootstrapped a framework?
  67. $this->uri = $root;
  68. }
  69. /**
  70. * Return the bootstrap object in use. This will
  71. * be the latched bootstrap object if we have started
  72. * bootstrapping; otherwise, it will be whichever bootstrap
  73. * object is best for the selected root.
  74. *
  75. * @return \Drush\Boot\Boot
  76. */
  77. public function bootstrap() {
  78. if ($this->bootstrap) {
  79. return $this->bootstrap;
  80. }
  81. return $this->selectBootstrapClass();
  82. }
  83. /**
  84. * Look up the best bootstrap class for the given location
  85. * from the set of available candidates.
  86. *
  87. * @return \Drush\Boot\Boot
  88. */
  89. public function bootstrapObjectForRoot($path) {
  90. foreach ($this->bootstrapCandidates as $candidate) {
  91. if ($candidate->valid_root($path)) {
  92. return $candidate;
  93. }
  94. }
  95. return NULL;
  96. }
  97. /**
  98. * Select the bootstrap class to use. If this is called multiple
  99. * times, the bootstrap class returned might change on subsequent
  100. * calls, if the root directory changes. Once the bootstrap object
  101. * starts changing the state of the system, however, it will
  102. * be 'latched', and further calls to \Drush::bootstrapf()
  103. * will always return the same object.
  104. */
  105. protected function selectBootstrapClass() {
  106. // Once we have selected a Drupal root, we will reduce our bootstrap
  107. // candidates down to just the one used to select this site root.
  108. $bootstrap = $this->bootstrapObjectForRoot($this->root);
  109. // If we have not found a bootstrap class by this point,
  110. // then return our default bootstrap object. The default bootstrap object
  111. // should pass through all calls without doing anything that
  112. // changes state in a CMS-specific way.
  113. if ($bootstrap == NULL) {
  114. $bootstrap = $this->defaultBootstrapObject;
  115. }
  116. return $bootstrap;
  117. }
  118. /**
  119. * Once bootstrapping has started, we stash the bootstrap
  120. * object being used, and do not allow it to change any
  121. * longer.
  122. */
  123. public function latch($bootstrap) {
  124. $this->bootstrap = $bootstrap;
  125. }
  126. }