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\Collection\Collection; |
11
|
|
|
use Deployer\Console\InitCommand; |
12
|
|
|
use Deployer\Console\WorkerCommand; |
13
|
|
|
use Deployer\Console\Application; |
14
|
|
|
use Deployer\Server; |
15
|
|
|
use Deployer\Stage\StageStrategy; |
16
|
|
|
use Deployer\Task; |
17
|
|
|
use Deployer\Console\TaskCommand; |
18
|
|
|
use Monolog\Handler\NullHandler; |
19
|
|
|
use Monolog\Handler\StreamHandler; |
20
|
|
|
use Monolog\Logger; |
21
|
|
|
use Pimple\Container; |
22
|
|
|
use Psr\Log\LoggerInterface; |
23
|
|
|
use Symfony\Component\Console; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @property Task\TaskCollection|Task\Task[] tasks |
27
|
|
|
* @property Server\ServerCollection|Server\ServerInterface[] servers |
28
|
|
|
* @property Server\EnvironmentCollection|Server\Environment[] environments |
29
|
|
|
* @property Collection 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
|
|
|
* Console * |
50
|
|
|
******************************/ |
51
|
|
|
|
52
|
|
|
$this['console'] = function () use ($console) { |
53
|
|
|
return $console; |
54
|
|
|
}; |
55
|
|
|
$this['input'] = function () use ($input) { |
56
|
|
|
return $input; |
57
|
|
|
}; |
58
|
|
|
$this['output'] = function () use ($output) { |
59
|
|
|
return $output; |
60
|
|
|
}; |
61
|
|
|
|
62
|
|
|
/****************************** |
63
|
|
|
* Config * |
64
|
|
|
******************************/ |
65
|
35 |
|
|
66
|
|
|
$this['config'] = function () { |
67
|
35 |
|
return new Collection(); |
68
|
35 |
|
}; |
69
|
35 |
|
$this->config['ssh_type'] = 'phpseclib'; |
70
|
|
|
$this->config['default_stage'] = null; |
71
|
35 |
|
|
72
|
35 |
|
/****************************** |
73
|
35 |
|
* Core * |
74
|
35 |
|
******************************/ |
75
|
35 |
|
|
76
|
35 |
|
$this['tasks'] = function () { |
77
|
|
|
return new Task\TaskCollection(); |
78
|
35 |
|
}; |
79
|
|
|
$this['servers'] = function () { |
80
|
35 |
|
return new Server\ServerCollection(); |
81
|
35 |
|
}; |
82
|
|
|
$this['environments'] = function () { |
83
|
|
|
return new Server\EnvironmentCollection(); |
84
|
|
|
}; |
85
|
|
|
$this['scriptManager'] = function ($c) { |
86
|
22 |
|
return new Task\ScriptManager($c['tasks']); |
87
|
|
|
}; |
88
|
22 |
|
$this['stageStrategy'] = function ($c) { |
89
|
|
|
return new StageStrategy($c['servers'], $c['environments'], $c['config']['default_stage']); |
90
|
|
|
}; |
91
|
|
|
$this['onFailure'] = function () { |
92
|
|
|
return new Collection(); |
93
|
|
|
}; |
94
|
|
|
|
95
|
|
|
/****************************** |
96
|
|
|
* Logger * |
97
|
|
|
******************************/ |
98
|
|
|
|
99
|
|
|
$this['log_level'] = Logger::DEBUG; |
100
|
|
|
$this['log_handler'] = function () { |
101
|
|
|
return isset($this->config['log_file']) |
102
|
|
|
? new StreamHandler($this->config['log_file'], $this['log_level']) |
103
|
|
|
: new NullHandler($this['log_level']); |
104
|
|
|
}; |
105
|
|
|
$this['log'] = function () { |
106
|
|
|
$name = isset($this->config['log_name']) ? $this->config['log_name'] : 'Deployer'; |
107
|
14 |
|
return new Logger($name, [ |
108
|
|
|
$this['log_handler'] |
109
|
14 |
|
]); |
110
|
|
|
}; |
111
|
14 |
|
|
112
|
14 |
|
/****************************** |
113
|
14 |
|
* Init command * |
114
|
|
|
******************************/ |
115
|
|
|
|
116
|
14 |
|
$this['init_command'] = function () { |
117
|
14 |
|
return new InitCommand(); |
118
|
14 |
|
}; |
119
|
|
|
|
120
|
|
|
self::$instance = $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return Deployer |
125
|
|
|
*/ |
126
|
|
|
public static function get() |
127
|
|
|
{ |
128
|
|
|
return self::$instance; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $name |
133
|
|
|
* @param mixed $value |
134
|
|
|
*/ |
135
|
|
|
public static function setDefault($name, $value) |
136
|
|
|
{ |
137
|
|
|
Deployer::get()->config[$name] = $value; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
35 |
|
* @param string $name |
142
|
|
|
* @param mixed $default |
143
|
35 |
|
* @return mixed |
144
|
35 |
|
*/ |
145
|
|
|
public static function getDefault($name, $default = null) |
146
|
1 |
|
{ |
147
|
|
|
return self::hasDefault($name) ? Deployer::get()->config[$name] : $default; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $name |
152
|
|
|
* @return boolean |
153
|
|
|
*/ |
154
|
|
|
public static function hasDefault($name) |
155
|
|
|
{ |
156
|
|
|
return isset(Deployer::get()->config[$name]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $name |
161
|
|
|
* @param array $array |
162
|
14 |
|
*/ |
163
|
|
|
public static function addDefault($name, $array) |
164
|
14 |
|
{ |
165
|
|
|
if (self::hasDefault($name)) { |
166
|
|
|
$config = self::getDefault($name); |
167
|
|
|
if (!is_array($config)) { |
168
|
|
|
throw new \RuntimeException("Configuration parameter `$name` isn't array."); |
169
|
|
|
} |
170
|
15 |
|
self::setDefault($name, array_merge($config, $array)); |
171
|
|
|
} else { |
172
|
15 |
|
self::setDefault($name, $array); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Run console application. |
178
|
|
|
*/ |
179
|
|
|
public function run() |
180
|
|
|
{ |
181
|
|
|
$this->addConsoleCommands(); |
182
|
|
|
|
183
|
|
|
$this->getConsole()->add(new WorkerCommand($this)); |
184
|
|
|
$this->getConsole()->add($this['init_command']); |
185
|
|
|
|
186
|
|
|
$this->getConsole()->run($this->input, $this->output); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Transform tasks to console commands. |
191
|
|
|
*/ |
192
|
|
|
public function addConsoleCommands() |
193
|
|
|
{ |
194
|
|
|
$this->getConsole()->addUserArgumentsAndOptions(); |
195
|
|
|
|
196
|
|
|
foreach ($this->tasks as $name => $task) { |
197
|
|
|
if ($task->isPrivate()) { |
198
|
|
|
continue; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$this->getConsole()->add(new TaskCommand($name, $task->getDescription(), $this)); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param string $name |
207
|
|
|
* @return mixed |
208
|
|
|
* @throws \InvalidArgumentException |
209
|
|
|
*/ |
210
|
|
|
public function __get($name) |
211
|
|
|
{ |
212
|
|
|
if (isset($this[$name])) { |
213
|
|
|
return $this[$name]; |
214
|
|
|
} else { |
215
|
|
|
throw new \InvalidArgumentException("Property \"$name\" does not exist."); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return Application |
221
|
|
|
*/ |
222
|
|
|
public function getConsole() |
223
|
|
|
{ |
224
|
|
|
return $this['console']; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return Console\Input\InputInterface |
229
|
|
|
*/ |
230
|
|
|
public function getInput() |
231
|
|
|
{ |
232
|
|
|
return $this['input']; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return Console\Output\OutputInterface |
237
|
|
|
*/ |
238
|
|
|
public function getOutput() |
239
|
|
|
{ |
240
|
|
|
return $this['output']; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param string $name |
245
|
|
|
* @return Console\Helper\HelperInterface |
246
|
|
|
*/ |
247
|
|
|
public function getHelper($name) |
248
|
|
|
{ |
249
|
|
|
return $this->getConsole()->getHelperSet()->get($name); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return StageStrategy |
254
|
|
|
*/ |
255
|
|
|
public function getStageStrategy() |
256
|
|
|
{ |
257
|
|
|
return $this['stageStrategy']; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return Task\ScriptManager |
262
|
|
|
*/ |
263
|
|
|
public function getScriptManager() |
264
|
|
|
{ |
265
|
|
|
return $this['scriptManager']; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return LoggerInterface |
270
|
|
|
*/ |
271
|
|
|
public function getLogger() |
272
|
|
|
{ |
273
|
|
|
return $this['log']; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
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.