1
|
|
|
<?php |
2
|
|
|
namespace Peridot\Console; |
3
|
|
|
|
4
|
|
|
use Evenement\EventEmitterInterface; |
5
|
|
|
use Peridot\Core\HasEventEmitterTrait; |
6
|
|
|
use Peridot\Runner\Context; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Environment is responsible for creating necessary objects and conditions |
10
|
|
|
* for Peridot to run. It creates the event emitter, input definition, and includes |
11
|
|
|
* user configuration from the Peridot configuration file. |
12
|
|
|
* |
13
|
|
|
* @package Peridot\Console |
14
|
|
|
*/ |
15
|
|
|
class Environment |
16
|
|
|
{ |
17
|
|
|
use HasEventEmitterTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var InputDefinition |
21
|
|
|
*/ |
22
|
|
|
protected $definition; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Environment options |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $options; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param InputDefinition $definition |
33
|
|
|
* @param EventEmitterInterface $emitter |
34
|
|
|
* @param array $options |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
InputDefinition $definition, |
38
|
|
|
EventEmitterInterface $emitter, |
39
|
|
|
array $options |
40
|
|
|
) { |
41
|
|
|
$this->definition = $definition; |
42
|
|
|
$this->eventEmitter = $emitter; |
43
|
|
|
$this->options = $options; |
44
|
|
|
$this->initializeContext($emitter); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Attempt to load a user configuration file into the Peridot |
49
|
|
|
* environment |
50
|
|
|
* |
51
|
|
|
* @param string $configuration The default configuration path |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function load($configuration) |
56
|
|
|
{ |
57
|
|
|
return $this->loadConfiguration($configuration); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return the InputDefinition used to define the available Peridot |
62
|
|
|
* options and arguments |
63
|
|
|
* |
64
|
|
|
* @return \Peridot\Console\InputDefinition |
65
|
|
|
*/ |
66
|
|
|
public function getDefinition() |
67
|
|
|
{ |
68
|
|
|
return $this->definition; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Load configuration |
73
|
|
|
* |
74
|
|
|
* @param $configuration |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
protected function loadConfiguration($configuration) |
78
|
|
|
{ |
79
|
|
|
if (! $this->wasGivenAConfigurationPath()) { |
80
|
|
|
return $this->includeConfiguration($configuration); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$files = array_filter(['c', 'configuration'], [$this, 'optionIsFile']); |
84
|
|
|
|
85
|
|
|
if (empty($files)) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->includeConfiguration($this->options[array_pop($files)]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Determine if the environment option identified by $key |
94
|
|
|
* is a file. |
95
|
|
|
* |
96
|
|
|
* @param $key |
97
|
|
|
*/ |
98
|
|
|
protected function optionIsFile($key) |
99
|
|
|
{ |
100
|
|
|
if (! array_key_exists($key, $this->options)) { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return is_file($this->options[$key]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Include the configuration file used to setup the peridot |
109
|
|
|
* environment |
110
|
|
|
* |
111
|
|
|
* @param $configuration |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
protected function includeConfiguration($configuration) |
115
|
|
|
{ |
116
|
|
|
if (file_exists($configuration)) { |
117
|
|
|
$callable = include $configuration; |
118
|
|
|
if (is_callable($callable)) { |
119
|
|
|
call_user_func($callable, $this->eventEmitter); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns true if the Environment was given a configuration path. |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
protected function wasGivenAConfigurationPath() |
132
|
|
|
{ |
133
|
|
|
return isset($this->options['c']) || isset($this->options['configuration']); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Initialize the Context with the same event emitter as the Environment. |
138
|
|
|
* |
139
|
|
|
* @param EventEmitterInterface $emitter |
140
|
|
|
*/ |
141
|
|
|
protected function initializeContext(EventEmitterInterface $emitter) |
142
|
|
|
{ |
143
|
|
|
Context::getInstance()->setEventEmitter($emitter); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|