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