3
3
namespace Drush \Boot ;
4
4
5
5
use Consolidation \AnnotatedCommand \AnnotationData ;
6
- use Robo \Common \ConfigAwareTrait ;
7
6
use DrupalFinder \DrupalFinder ;
8
7
use Drush \Log \LogLevel ;
8
+ use League \Container \ContainerAwareInterface ;
9
+ use League \Container \ContainerAwareTrait ;
9
10
use Psr \Log \LoggerAwareInterface ;
10
11
use Psr \Log \LoggerAwareTrait ;
12
+ use Robo \Common \ConfigAwareTrait ;
11
13
use Robo \Contract \ConfigAwareInterface ;
12
14
13
- class BootstrapManager implements LoggerAwareInterface, AutoloaderAwareInterface, ConfigAwareInterface
15
+ class BootstrapManager implements LoggerAwareInterface, AutoloaderAwareInterface, ConfigAwareInterface, ContainerAwareInterface
14
16
{
15
17
use LoggerAwareTrait;
16
18
use AutoloaderAwareTrait;
17
19
use ConfigAwareTrait;
20
+ use ContainerAwareTrait;
18
21
19
22
/**
20
23
* @var DrupalFinder
@@ -26,63 +29,37 @@ class BootstrapManager implements LoggerAwareInterface, AutoloaderAwareInterface
26
29
*/
27
30
protected $ bootstrapCandidates = [];
28
31
29
- /**
30
- * @var \Drush\Boot\Boot
31
- */
32
- protected $ defaultBootstrapObject ;
33
-
34
32
/**
35
33
* @var \Drush\Boot\Boot
36
34
*/
37
35
protected $ bootstrap ;
38
36
39
- /**
40
- * @var string
41
- */
42
- protected $ root ;
43
-
44
- /**
45
- * @var string
46
- */
47
- protected $ uri ;
48
-
49
37
/**
50
38
* @var int
51
39
*/
52
40
protected $ phase ;
53
41
54
- /**
55
- * Constructor.
56
- *
57
- * @param \Drush\Boot\Boot
58
- * The default bootstrap object to use when there are
59
- * no viable candidates to use (e.g. no selected site)
60
- */
61
- public function __construct (Boot $ default )
62
- {
63
- $ this ->defaultBootstrapObject = $ default ;
64
-
65
- // Reset our bootstrap phase to the beginning
66
- $ this ->setPhase (DRUSH_BOOTSTRAP_NONE );
67
- }
68
-
69
42
/**
70
43
* @return int
71
44
*/
72
45
public function getPhase ()
73
46
{
74
- return $ this ->phase ;
47
+ if (!$ this ->hasBootstrap ()) {
48
+ return DRUSH_BOOTSTRAP_NONE ;
49
+ }
50
+ return $ this ->bootstrap ()->getPhase ();
75
51
}
76
52
77
53
/**
78
54
* @param int $phase
79
55
*/
80
- public function setPhase ($ phase )
56
+ protected function setPhase ($ phase )
81
57
{
82
- $ this ->phase = $ phase ;
58
+ if ($ this ->bootstrap ) {
59
+ $ this ->bootstrap ()->setPhase ($ phase );
60
+ }
83
61
}
84
62
85
-
86
63
/**
87
64
* Add a bootstrap object to the list of candidates.
88
65
*
@@ -140,7 +117,10 @@ public function locateRoot($root, $start_path = null)
140
117
*/
141
118
public function getUri ()
142
119
{
143
- return $ this ->uri ;
120
+ if (!$ this ->hasBootstrap ()) {
121
+ return false ;
122
+ }
123
+ return $ this ->bootstrap ()->getUri ();
144
124
}
145
125
146
126
/**
@@ -158,10 +138,7 @@ public function setUri($uri)
158
138
{
159
139
// TODO: Throw if we already bootstrapped a framework?
160
140
// n.b. site-install needs to set the uri.
161
- $ this ->uri = $ uri ;
162
- if ($ this ->bootstrap ) {
163
- $ this ->bootstrap ->setUri ($ this ->getUri ());
164
- }
141
+ $ this ->bootstrap ()->setUri ($ uri );
165
142
}
166
143
167
144
/**
@@ -174,10 +151,23 @@ public function setUri($uri)
174
151
*/
175
152
public function bootstrap ()
176
153
{
177
- if ($ this ->bootstrap ) {
178
- return $ this ->bootstrap ;
154
+ if (! $ this ->bootstrap ) {
155
+ $ this ->bootstrap = $ this -> selectBootstrapClass () ;
179
156
}
180
- return $ this ->selectBootstrapClass ();
157
+ return $ this ->bootstrap ;
158
+ }
159
+
160
+ /**
161
+ * For use in testing
162
+ */
163
+ public function injectBootstrap ($ bootstrap )
164
+ {
165
+ $ this ->inflect ($ bootstrap );
166
+ $ this ->bootstrap = $ bootstrap ;
167
+
168
+ // Our bootstrap object is always a DrupalBoot8.
169
+ // TODO: make an API in the Boot interface to call.
170
+ $ bootstrap ->addDrupalModuleDrushCommands ($ this );
181
171
}
182
172
183
173
/**
@@ -195,7 +185,6 @@ public function bootstrapObjectForRoot($path)
195
185
if ($ candidate instanceof AutoloaderAwareInterface) {
196
186
$ candidate ->setAutoloader ($ this ->autoloader ());
197
187
}
198
- $ candidate ->setUri ($ this ->getUri ());
199
188
return $ candidate ;
200
189
}
201
190
}
@@ -214,16 +203,7 @@ protected function selectBootstrapClass()
214
203
{
215
204
// Once we have selected a Drupal root, we will reduce our bootstrap
216
205
// candidates down to just the one used to select this site root.
217
- $ bootstrap = $ this ->bootstrapObjectForRoot ($ this ->getRoot ());
218
- // If we have not found a bootstrap class by this point,
219
- // then return our default bootstrap object. The default bootstrap object
220
- // should pass through all calls without doing anything that
221
- // changes state in a CMS-specific way.
222
- if ($ bootstrap == null ) {
223
- $ bootstrap = $ this ->defaultBootstrapObject ;
224
- }
225
-
226
- return $ bootstrap ;
206
+ return $ this ->bootstrapObjectForRoot ($ this ->getRoot ());
227
207
}
228
208
229
209
/**
@@ -310,15 +290,23 @@ public function doBootstrap($phase, $phase_max = false, AnnotationData $annotati
310
290
if ($ result = $ this ->bootstrapValidate ($ phase_index )) {
311
291
if (method_exists ($ bootstrap , $ current_phase )) {
312
292
$ this ->logger ->log (LogLevel::BOOTSTRAP , 'Drush bootstrap phase: {function}() ' , ['function ' => $ current_phase ]);
313
- $ bootstrap ->{$ current_phase }($ annotationData );
293
+ $ bootstrap ->{$ current_phase }($ this , $ annotationData );
314
294
}
315
- $ this ->setPhase ($ phase_index );
295
+ $ bootstrap ->setPhase ($ phase_index );
316
296
}
317
297
}
318
298
}
319
299
return true ;
320
300
}
321
301
302
+ /**
303
+ * hasBootstrap determines whether the manager has a bootstrap object yet.
304
+ */
305
+ public function hasBootstrap ()
306
+ {
307
+ return $ this ->bootstrap != null ;
308
+ }
309
+
322
310
/**
323
311
* Determine whether a given bootstrap phase has been completed.
324
312
*
@@ -367,7 +355,7 @@ public function bootstrapValidate($phase)
367
355
if ($ phase_index > $ validated_phase ) {
368
356
$ current_phase .= 'Validate ' ;
369
357
if (method_exists ($ bootstrap , $ current_phase )) {
370
- $ result_cache [$ phase_index ] = $ bootstrap ->{$ current_phase }();
358
+ $ result_cache [$ phase_index ] = $ bootstrap ->{$ current_phase }($ this );
371
359
} else {
372
360
$ result_cache [$ phase_index ] = true ;
373
361
}
@@ -515,4 +503,46 @@ public function bootstrapMax($max_phase_index = false, AnnotationData $annotatio
515
503
516
504
return $ this ->getPhase ();
517
505
}
506
+
507
+ /**
508
+ * Allow those with an instance to us to the BootstrapManager to use its logger
509
+ */
510
+ public function logger ()
511
+ {
512
+ return $ this ->logger ;
513
+ }
514
+
515
+ public function inflect ($ object )
516
+ {
517
+ // See \Drush\Runtime\DependencyInjection::addDrushServices and
518
+ // \Robo\Robo\addInflectors
519
+ $ container = $ this ->getContainer ();
520
+ if ($ object instanceof \Robo \Contract \ConfigAwareInterface) {
521
+ $ object ->setConfig ($ container ->get ('config ' ));
522
+ }
523
+ if ($ object instanceof \Psr \Log \LoggerAwareInterface) {
524
+ $ object ->setLogger ($ container ->get ('logger ' ));
525
+ }
526
+ if ($ object instanceof \League \Container \ContainerAwareInterface) {
527
+ $ object ->setContainer ($ container ->get ('container ' ));
528
+ }
529
+ if ($ object instanceof \Symfony \Component \Console \Input \InputAwareInterface) {
530
+ $ object ->setInput ($ container ->get ('input ' ));
531
+ }
532
+ if ($ object instanceof \Robo \Contract \OutputAwareInterface) {
533
+ $ object ->setOutput ($ container ->get ('output ' ));
534
+ }
535
+ if ($ object instanceof \Robo \Contract \ProgressIndicatorAwareInterface) {
536
+ $ object ->setProgressIndicator ($ container ->get ('progressIndicator ' ));
537
+ }
538
+ if ($ object instanceof \Consolidation \AnnotatedCommand \Events \CustomEventAwareInterface) {
539
+ $ object ->setHookManager ($ container ->get ('hookManager ' ));
540
+ }
541
+ if ($ object instanceof \Robo \Contract \VerbosityThresholdInterface) {
542
+ $ object ->setOutputAdapter ($ container ->get ('outputAdapter ' ));
543
+ }
544
+ if ($ object instanceof \Consolidation \SiteAlias \SiteAliasManagerAwareInterface) {
545
+ $ object ->setSiteAliasManager ($ container ->get ('site.alias.manager ' ));
546
+ }
547
+ }
518
548
}
0 commit comments