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
|
|
|
|
92
|
|
|
/****************************** |
93
|
|
|
* Logger * |
94
|
|
|
******************************/ |
95
|
|
|
|
96
|
|
|
$this['log_level'] = Logger::DEBUG; |
97
|
|
|
$this['log_handler'] = function () { |
98
|
|
|
return isset($this->config['log_file']) |
99
|
|
|
? new StreamHandler($this->config['log_file'], $this['log_level']) |
100
|
|
|
: new NullHandler($this['log_level']); |
101
|
|
|
}; |
102
|
|
|
$this['log'] = function () { |
103
|
|
|
$name = isset($this->config['log_name']) ? $this->config['log_name'] : 'Deployer'; |
104
|
|
|
return new Logger($name, [ |
105
|
|
|
$this['log_handler'] |
106
|
|
|
]); |
107
|
14 |
|
}; |
108
|
|
|
|
109
|
14 |
|
self::$instance = $this; |
110
|
|
|
} |
111
|
14 |
|
|
112
|
14 |
|
/** |
113
|
14 |
|
* @return Deployer |
114
|
|
|
*/ |
115
|
|
|
public static function get() |
116
|
14 |
|
{ |
117
|
14 |
|
return self::$instance; |
118
|
14 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $name |
122
|
|
|
* @param mixed $value |
123
|
|
|
*/ |
124
|
|
|
public static function setDefault($name, $value) |
125
|
|
|
{ |
126
|
|
|
Deployer::get()->config[$name] = $value; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $name |
131
|
|
|
* @param mixed $default |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public static function getDefault($name, $default = null) |
135
|
|
|
{ |
136
|
|
|
return self::hasDefault($name) ? Deployer::get()->config[$name] : $default; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $name |
141
|
35 |
|
* @return boolean |
142
|
|
|
*/ |
143
|
35 |
|
public static function hasDefault($name) |
144
|
35 |
|
{ |
145
|
|
|
return isset(Deployer::get()->config[$name]); |
146
|
1 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $name |
150
|
|
|
* @param array $array |
151
|
|
|
*/ |
152
|
|
|
public static function addDefault($name, $array) |
153
|
|
|
{ |
154
|
|
|
if (self::hasDefault($name)) { |
155
|
|
|
$config = self::getDefault($name); |
156
|
|
|
if (!is_array($config)) { |
157
|
|
|
throw new \RuntimeException("Configuration parameter `$name` isn't array."); |
158
|
|
|
} |
159
|
|
|
self::setDefault($name, array_merge($config, $array)); |
160
|
|
|
} else { |
161
|
|
|
self::setDefault($name, $array); |
162
|
14 |
|
} |
163
|
|
|
} |
164
|
14 |
|
|
165
|
|
|
/** |
166
|
|
|
* Run console application. |
167
|
|
|
*/ |
168
|
|
|
public function run() |
169
|
|
|
{ |
170
|
15 |
|
$this->addConsoleCommands(); |
171
|
|
|
|
172
|
15 |
|
$this->getConsole()->add(new WorkerCommand($this)); |
173
|
|
|
$this->getConsole()->add(new InitCommand()); |
174
|
|
|
|
175
|
|
|
$this->getConsole()->run($this->input, $this->output); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Transform tasks to console commands. |
180
|
|
|
*/ |
181
|
|
|
public function addConsoleCommands() |
182
|
|
|
{ |
183
|
|
|
$this->getConsole()->addUserArgumentsAndOptions(); |
184
|
|
|
|
185
|
|
|
foreach ($this->tasks as $name => $task) { |
186
|
|
|
if ($task->isPrivate()) { |
187
|
|
|
continue; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$this->getConsole()->add(new TaskCommand($name, $task->getDescription(), $this)); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $name |
196
|
|
|
* @return mixed |
197
|
|
|
* @throws \InvalidArgumentException |
198
|
|
|
*/ |
199
|
|
|
public function __get($name) |
200
|
|
|
{ |
201
|
|
|
if (isset($this[$name])) { |
202
|
|
|
return $this[$name]; |
203
|
|
|
} else { |
204
|
|
|
throw new \InvalidArgumentException("Property \"$name\" does not exist."); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return Application |
210
|
|
|
*/ |
211
|
|
|
public function getConsole() |
212
|
|
|
{ |
213
|
|
|
return $this['console']; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return Console\Input\InputInterface |
218
|
|
|
*/ |
219
|
|
|
public function getInput() |
220
|
|
|
{ |
221
|
|
|
return $this['input']; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return Console\Output\OutputInterface |
226
|
|
|
*/ |
227
|
|
|
public function getOutput() |
228
|
|
|
{ |
229
|
|
|
return $this['output']; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $name |
234
|
|
|
* @return Console\Helper\HelperInterface |
235
|
|
|
*/ |
236
|
|
|
public function getHelper($name) |
237
|
|
|
{ |
238
|
|
|
return $this->getConsole()->getHelperSet()->get($name); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return StageStrategy |
243
|
|
|
*/ |
244
|
|
|
public function getStageStrategy() |
245
|
|
|
{ |
246
|
|
|
return $this['stageStrategy']; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return Task\ScriptManager |
251
|
|
|
*/ |
252
|
|
|
public function getScriptManager() |
253
|
|
|
{ |
254
|
|
|
return $this['scriptManager']; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return LoggerInterface |
259
|
|
|
*/ |
260
|
|
|
public function getLogger() |
261
|
|
|
{ |
262
|
|
|
return $this['log']; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
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.