|
1
|
|
|
<?php |
|
2
|
|
|
namespace Consolidation\Bootstrap; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Log\LoggerInterface; |
|
5
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
6
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
7
|
|
|
|
|
8
|
|
|
class BootstrapManager implements LoggerAwareInterface |
|
9
|
|
|
{ |
|
10
|
|
|
use LoggerAwareTrait; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var Consolidation\Bootstrap\BootInterface[] |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $bootstrapCandidates = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var BootstrapCurrator |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $currator; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Constructor. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function setBootstrapCurrator(BootstrapCurrator $currator) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->currator = $currator; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getBootstrapCurrator() |
|
35
|
|
|
{ |
|
36
|
|
|
if (!isset($this->currator)) { |
|
37
|
|
|
$this->currator = new BootstrapCurrator(); |
|
38
|
|
|
} |
|
39
|
|
|
return $this->currator; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Add a bootstrap selection object to the list of candidates |
|
44
|
|
|
* |
|
45
|
|
|
* @param BootstrapSelectionInterface |
|
46
|
|
|
* List of boot candidates |
|
47
|
|
|
*/ |
|
48
|
|
|
public function add(BootstrapSelectionInterface $candidate) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->bootstrapCandidates[] = $candidate; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Register a factory with an object-creation listener, and |
|
55
|
|
|
* the bootstrap manager will ensure that the bootstrap object |
|
56
|
|
|
* will be set on any object that implements BootstrapAwareInterface. |
|
57
|
|
|
* This is an alternative to using dependency injection container |
|
58
|
|
|
* inflection (e.g. if the commandfile objects are not always |
|
59
|
|
|
* created via the container). |
|
60
|
|
|
* |
|
61
|
|
|
* @param mixed $factory A factory object that creates or uses |
|
62
|
|
|
* bootstrap-aware objects. |
|
63
|
|
|
* @param string $listenerMethod The method to call to register a |
|
64
|
|
|
* listener callback function. |
|
65
|
|
|
* @example $bootstrapManager->registerFactory($annotationCommandFactory); |
|
66
|
|
|
*/ |
|
67
|
|
|
public function registerFactory($factory, $listenerMethod = 'addListener') |
|
68
|
|
|
{ |
|
69
|
|
|
$factory->$listenerMethod( |
|
70
|
|
|
function ($object) { |
|
71
|
|
|
if ($object instanceof BootstrapAwareInterface) { |
|
72
|
|
|
$object->setBootstrapCurrator($this->getBootstrapCurrator()); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Look up the best bootstrap class for the given location |
|
80
|
|
|
* from the set of available candidates. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $path Path to the desired framework |
|
83
|
|
|
* |
|
84
|
|
|
* @return null|BootInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
public function selectBootstrap($path) |
|
87
|
|
|
{ |
|
88
|
|
|
if ($this->getBootstrapCurrator()->hasBootstrap()) { |
|
89
|
|
|
throw new \RuntimeException('Bootstrap object already selected.'); |
|
90
|
|
|
} |
|
91
|
|
|
foreach ($this->bootstrapCandidates as $candidate) { |
|
92
|
|
|
if ($candidate->isValid($path)) { |
|
93
|
|
|
return $this->setSelectedBootstrap($candidate, $path); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the bootstrap object for the selected framework. |
|
101
|
|
|
* |
|
102
|
|
|
* @param BootstrapSelectionInterface $candidate A valid selection object. |
|
103
|
|
|
* |
|
104
|
|
|
* @return BootInterface |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function setSelectedBootstrap(BootstrapSelectionInterface $candidate, $path) |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->setBootstrap($candidate->getBootstrap($path)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Record the bootstrap object that was selected. |
|
113
|
|
|
* |
|
114
|
|
|
* @param BootInterface $bootstrap |
|
115
|
|
|
* |
|
116
|
|
|
* @return BootInterface |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function setBootstrap(BootInterface $bootstrap) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->getBootstrapCurrator()->setBootstrap($bootstrap); |
|
121
|
|
|
return $bootstrap; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|