1 | <?php |
||
58 | class Deployer extends Container |
||
59 | { |
||
60 | /** |
||
61 | * Global instance of deployer. It's can be accessed only after constructor call. |
||
62 | * @var Deployer |
||
63 | */ |
||
64 | private static $instance; |
||
65 | |||
66 | /** |
||
67 | * @param Application $console |
||
68 | */ |
||
69 | 29 | public function __construct(Application $console) |
|
70 | { |
||
71 | 29 | parent::__construct(); |
|
72 | |||
73 | /****************************** |
||
74 | * Console * |
||
75 | ******************************/ |
||
76 | |||
77 | $this['console'] = function () use ($console) { |
||
78 | 14 | $console->catchIO(function ($input, $output) { |
|
79 | 14 | $this['input'] = $input; |
|
80 | 14 | $this['output'] = new OutputWatcher($output); |
|
81 | 14 | return [$this['input'], $this['output']]; |
|
82 | 14 | }); |
|
83 | 14 | return $console; |
|
84 | }; |
||
85 | |||
86 | /****************************** |
||
87 | * Config * |
||
88 | ******************************/ |
||
89 | |||
90 | 29 | $this['config'] = function () { |
|
91 | 29 | return new Collection(); |
|
92 | }; |
||
93 | 29 | $this->config['ssh_multiplexing'] = true; |
|
94 | 29 | $this->config['default_stage'] = null; |
|
95 | |||
96 | /****************************** |
||
97 | * Core * |
||
98 | ******************************/ |
||
99 | |||
100 | 10 | $this['pop'] = function ($c) { |
|
101 | 10 | return new ProcessOutputPrinter($c['output'], $c['logger']); |
|
102 | }; |
||
103 | $this['sshClient'] = function ($c) { |
||
104 | return new Ssh\Client($c['output'], $c['pop'], $c['config']['ssh_multiplexing']); |
||
105 | }; |
||
106 | $this['rsync'] = function ($c) { |
||
107 | return new Rsync($c['pop']); |
||
108 | }; |
||
109 | 10 | $this['processRunner'] = function ($c) { |
|
110 | 10 | return new ProcessRunner($c['pop']); |
|
111 | }; |
||
112 | 18 | $this['tasks'] = function () { |
|
113 | 18 | return new Task\TaskCollection(); |
|
114 | }; |
||
115 | 18 | $this['hosts'] = function () { |
|
116 | 18 | return new Host\HostCollection(); |
|
117 | }; |
||
118 | 16 | $this['scriptManager'] = function ($c) { |
|
119 | 16 | return new Task\ScriptManager($c['tasks']); |
|
120 | }; |
||
121 | 14 | $this['hostSelector'] = function ($c) { |
|
122 | 14 | $defaultStage = $c['config']['default_stage']; |
|
123 | 14 | if (is_object($defaultStage) && ($defaultStage instanceof \Closure)) { |
|
124 | $defaultStage = call_user_func($defaultStage); |
||
125 | } |
||
126 | 14 | return new Host\HostSelector($c['hosts'], $defaultStage); |
|
127 | }; |
||
128 | 9 | $this['fail'] = function () { |
|
129 | 9 | return new Collection(); |
|
130 | }; |
||
131 | 14 | $this['informer'] = function ($c) { |
|
132 | 14 | return new Informer($c['output']); |
|
133 | }; |
||
134 | 11 | $this['seriesExecutor'] = function ($c) { |
|
135 | 11 | return new SeriesExecutor($c['input'], $c['output'], $c['informer']); |
|
136 | }; |
||
137 | 3 | $this['parallelExecutor'] = function ($c) { |
|
138 | 3 | return new ParallelExecutor($c['input'], $c['output'], $c['informer'], $c['console']); |
|
139 | }; |
||
140 | |||
141 | /****************************** |
||
142 | * Logger * |
||
143 | ******************************/ |
||
144 | |||
145 | 11 | $this['log_handler'] = function () { |
|
146 | 11 | return !empty($this->config['log_file']) |
|
147 | ? new FileHandler($this->config['log_file']) |
||
148 | 11 | : new NullHandler(); |
|
149 | }; |
||
150 | 11 | $this['logger'] = function () { |
|
151 | 11 | return new Logger($this['log_handler']); |
|
152 | }; |
||
153 | |||
154 | /****************************** |
||
155 | * Init command * |
||
156 | ******************************/ |
||
157 | |||
158 | 14 | $this['init_command'] = function () { |
|
159 | 14 | return new InitCommand(); |
|
160 | }; |
||
161 | |||
162 | 29 | self::$instance = $this; |
|
163 | 29 | } |
|
164 | |||
165 | /** |
||
166 | * @return Deployer |
||
167 | */ |
||
168 | 31 | public static function get() |
|
172 | |||
173 | /** |
||
174 | * @param string $name |
||
175 | * @param mixed $value |
||
176 | */ |
||
177 | 13 | public static function setDefault($name, $value) |
|
181 | |||
182 | /** |
||
183 | * @param string $name |
||
184 | * @param mixed $default |
||
185 | * @return mixed |
||
186 | */ |
||
187 | 15 | public static function getDefault($name, $default = null) |
|
191 | |||
192 | /** |
||
193 | * @param string $name |
||
194 | * @return boolean |
||
195 | */ |
||
196 | 15 | public static function hasDefault($name) |
|
200 | |||
201 | /** |
||
202 | * @param string $name |
||
203 | * @param array $array |
||
204 | */ |
||
205 | 2 | public static function addDefault($name, $array) |
|
217 | |||
218 | /** |
||
219 | * Init console application |
||
220 | */ |
||
221 | 14 | public function init() |
|
232 | |||
233 | /** |
||
234 | * Transform tasks to console commands. |
||
235 | */ |
||
236 | 14 | public function addConsoleCommands() |
|
248 | |||
249 | /** |
||
250 | * @param string $name |
||
251 | * @return mixed |
||
252 | * @throws \InvalidArgumentException |
||
253 | */ |
||
254 | 34 | public function __get($name) |
|
262 | |||
263 | /** |
||
264 | * @return Application |
||
265 | */ |
||
266 | 14 | public function getConsole() |
|
270 | |||
271 | /** |
||
272 | * @return Console\Input\InputInterface |
||
273 | */ |
||
274 | public function getInput() |
||
278 | |||
279 | /** |
||
280 | * @return Console\Output\OutputInterface |
||
281 | */ |
||
282 | public function getOutput() |
||
286 | |||
287 | /** |
||
288 | * @param string $name |
||
289 | * @return Console\Helper\HelperInterface |
||
290 | */ |
||
291 | public function getHelper($name) |
||
295 | |||
296 | /** |
||
297 | * Run Deployer |
||
298 | * |
||
299 | * @param string $version |
||
300 | * @param string $deployFile |
||
301 | */ |
||
302 | public static function run($version, $deployFile) |
||
333 | |||
334 | /** |
||
335 | * Collect anonymous stats about Deployer usage for improving developer experience. |
||
336 | * If you are not comfortable with this, you will always be able to disable this |
||
337 | * by setting `allow_anonymous_stats` to false in your deploy.php file. |
||
338 | * |
||
339 | * @param CommandEvent $commandEvent |
||
340 | * @codeCoverageIgnore |
||
341 | */ |
||
342 | public function collectAnonymousStats(CommandEvent $commandEvent) |
||
377 | } |
||
378 |