1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer; |
9
|
|
|
|
10
|
|
|
use Deployer\Console\InitCommand; |
11
|
|
|
use Deployer\Console\WorkerCommand; |
12
|
|
|
use Deployer\Console\Application; |
13
|
|
|
use Deployer\Server; |
14
|
|
|
use Deployer\Stage\StageStrategy; |
15
|
|
|
use Deployer\Task; |
16
|
|
|
use Deployer\Console\TaskCommand; |
17
|
|
|
use Deployer\Type\DotArray; |
18
|
|
|
use Monolog\Handler\StreamHandler; |
19
|
|
|
use Monolog\Logger; |
20
|
|
|
use Pimple\Container; |
21
|
|
|
use Symfony\Component\Console; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @property Task\TaskCollection|Task\Task[] tasks |
26
|
|
|
* @property Task\Scenario\ScenarioCollection|Task\Scenario\Scenario[] scenarios |
27
|
|
|
* @property Server\ServerCollection|Server\ServerInterface[] servers |
28
|
|
|
* @property Server\EnvironmentCollection|Server\Environment[] environments |
29
|
|
|
* @property DotArray config |
30
|
|
|
*/ |
31
|
|
|
class Deployer extends Container |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Global instance of deployer. It's can be accessed only after constructor call. |
35
|
|
|
* @var Deployer |
36
|
|
|
*/ |
37
|
|
|
private static $instance; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Application $console |
41
|
|
|
* @param Console\Input\InputInterface $input |
42
|
|
|
* @param Console\Output\OutputInterface $output |
43
|
|
|
*/ |
44
|
|
|
public function __construct(Application $console, Console\Input\InputInterface $input, Console\Output\OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
parent::__construct(); |
47
|
|
|
|
48
|
|
|
/****************************** |
49
|
|
|
* Dispatcher * |
50
|
|
|
******************************/ |
51
|
|
|
|
52
|
|
|
$this['dispatcher'] = function () { |
53
|
|
|
return new EventDispatcher(); |
54
|
|
|
}; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/****************************** |
58
|
|
|
* Console * |
59
|
|
|
******************************/ |
60
|
|
|
|
61
|
|
|
$this['console'] = function () use ($console) { |
62
|
|
|
return $console; |
63
|
|
|
}; |
64
|
|
|
$this['input'] = function () use ($input) { |
65
|
35 |
|
return $input; |
66
|
|
|
}; |
67
|
35 |
|
$this['output'] = function () use ($output) { |
68
|
35 |
|
return $output; |
69
|
35 |
|
}; |
70
|
|
|
|
71
|
35 |
|
/****************************** |
72
|
35 |
|
* Config * |
73
|
35 |
|
******************************/ |
74
|
35 |
|
|
75
|
35 |
|
$this['config'] = function () { |
76
|
35 |
|
return new DotArray(); |
77
|
|
|
}; |
78
|
35 |
|
$this->config['ssh_type'] = 'phpseclib'; |
79
|
|
|
$this->config['default_stage'] = null; |
80
|
35 |
|
|
81
|
35 |
|
|
82
|
|
|
/****************************** |
83
|
|
|
* Core * |
84
|
|
|
******************************/ |
85
|
|
|
|
86
|
22 |
|
$this['tasks'] = function () { |
87
|
|
|
return new Task\TaskCollection(); |
88
|
22 |
|
}; |
89
|
|
|
$this['scenarios'] = function () { |
90
|
|
|
return new Task\Scenario\ScenarioCollection(); |
91
|
|
|
}; |
92
|
|
|
$this['servers'] = function () { |
93
|
|
|
return new Server\ServerCollection(); |
94
|
|
|
}; |
95
|
|
|
$this['environments'] = function () { |
96
|
|
|
return new Server\EnvironmentCollection(); |
97
|
|
|
}; |
98
|
|
|
$this['stageStrategy'] = function ($c) { |
99
|
|
|
return new StageStrategy($c['servers'], $c['environments'], $c['config']['default_stage']); |
100
|
|
|
}; |
101
|
|
|
|
102
|
|
|
/****************************** |
103
|
|
|
* Logger * |
104
|
|
|
******************************/ |
105
|
|
|
|
106
|
|
|
$this['log_level'] = function ($c) { |
107
|
14 |
|
$parameters = $c['parameters']; |
108
|
|
|
return isset($parameters['log_level']) ? $parameters['log_level'] : Logger::ERROR; |
109
|
14 |
|
}; |
110
|
|
|
$this['log_handler'] = function ($c) { |
111
|
14 |
|
$parameters = $c['parameters']; |
112
|
14 |
|
return new StreamHandler($parameters['log_file'], $parameters['log_level']); |
113
|
14 |
|
}; |
114
|
|
|
$this['log'] = function ($c) { |
115
|
|
|
$parameters = $c['parameters']; |
116
|
14 |
|
$name = isset($parameters['log_name']) ? $parameters['log_name'] : 'Deployer'; |
117
|
14 |
|
return new Logger($name); |
118
|
14 |
|
}; |
119
|
|
|
|
120
|
|
|
self::$instance = $this; |
121
|
|
|
$this->getDispatcher()->dispatch('init'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return Deployer |
126
|
|
|
*/ |
127
|
|
|
public static function get() |
128
|
|
|
{ |
129
|
|
|
return self::$instance; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $name |
134
|
|
|
* @param mixed $value |
135
|
|
|
*/ |
136
|
|
|
public static function setDefault($name, $value) |
137
|
|
|
{ |
138
|
|
|
Deployer::get()->config[$name] = $value; |
139
|
|
|
} |
140
|
|
|
|
141
|
35 |
|
/** |
142
|
|
|
* @param string $name |
143
|
35 |
|
* @param mixed $default |
144
|
35 |
|
* @return mixed |
145
|
|
|
*/ |
146
|
1 |
|
public static function getDefault($name, $default = null) |
147
|
|
|
{ |
148
|
|
|
return self::hasDefault($name) ? Deployer::get()->config[$name] : $default; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $name |
153
|
|
|
* @return boolean |
154
|
|
|
*/ |
155
|
|
|
public static function hasDefault($name) |
156
|
|
|
{ |
157
|
|
|
return isset(Deployer::get()->config[$name]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $name |
162
|
14 |
|
* @param array $array |
163
|
|
|
*/ |
164
|
14 |
|
public static function addDefault($name, $array) |
165
|
|
|
{ |
166
|
|
|
if (self::hasDefault($name)) { |
167
|
|
|
$config = self::getDefault($name); |
168
|
|
|
if (!is_array($config)) { |
169
|
|
|
throw new \RuntimeException("Configuration parameter `$name` isn't array."); |
170
|
15 |
|
} |
171
|
|
|
self::setDefault($name, array_merge($config, $array)); |
172
|
15 |
|
} else { |
173
|
|
|
self::setDefault($name, $array); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Run console application. |
179
|
|
|
*/ |
180
|
|
|
public function run() |
181
|
|
|
{ |
182
|
|
|
$this->addConsoleCommands(); |
183
|
|
|
|
184
|
|
|
$this->getConsole()->add(new WorkerCommand($this)); |
185
|
|
|
$this->getConsole()->add(new InitCommand()); |
186
|
|
|
|
187
|
|
|
$this->getConsole()->run($this->input, $this->output); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Transform tasks to console commands. |
192
|
|
|
*/ |
193
|
|
|
public function addConsoleCommands() |
194
|
|
|
{ |
195
|
|
|
$this->getConsole()->addUserArgumentsAndOptions(); |
196
|
|
|
|
197
|
|
|
foreach ($this->tasks as $name => $task) { |
198
|
|
|
if ($task->isPrivate()) { |
199
|
|
|
continue; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this->getConsole()->add(new TaskCommand($name, $task->getDescription(), $this)); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $name |
208
|
|
|
* @return mixed |
209
|
|
|
* @throws \InvalidArgumentException |
210
|
|
|
*/ |
211
|
|
|
public function __get($name) |
212
|
|
|
{ |
213
|
|
|
if (isset($this[$name])) { |
214
|
|
|
return $this[$name]; |
215
|
|
|
} else { |
216
|
|
|
throw new \InvalidArgumentException("Property \"$name\" does not exist."); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return EventDispatcher |
222
|
|
|
*/ |
223
|
|
|
public function getDispatcher() |
224
|
|
|
{ |
225
|
|
|
return $this['dispatcher']; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return Application |
230
|
|
|
*/ |
231
|
|
|
public function getConsole() |
232
|
|
|
{ |
233
|
|
|
return $this['console']; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return Console\Input\InputInterface |
238
|
|
|
*/ |
239
|
|
|
public function getInput() |
240
|
|
|
{ |
241
|
|
|
return $this['input']; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return Console\Output\OutputInterface |
246
|
|
|
*/ |
247
|
|
|
public function getOutput() |
248
|
|
|
{ |
249
|
|
|
return $this['output']; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string $name |
254
|
|
|
* @return Console\Helper\HelperInterface |
255
|
|
|
*/ |
256
|
|
|
public function getHelper($name) |
257
|
|
|
{ |
258
|
|
|
return $this->getConsole()->getHelperSet()->get($name); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return StageStrategy |
263
|
|
|
*/ |
264
|
|
|
public function getStageStrategy() |
265
|
|
|
{ |
266
|
|
|
return $this['stageStrategy']; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.