GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#761)
by Peter
04:02
created

functions.php ➔ upload()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5.0384

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 24
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 36
ccs 23
cts 26
cp 0.8846
crap 5.0384
rs 8.439
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
use Deployer\Deployer;
8
use Deployer\Server\Local;
9
use Deployer\Server\Remote;
10
use Deployer\Server\Builder;
11
use Deployer\Server\Configuration;
12
use Deployer\Server\Environment;
13
use Deployer\Task\Task as TheTask;
14
use Deployer\Task\Context;
15
use Deployer\Task\GroupTask;
16
use Deployer\Task\Scenario\GroupScenario;
17
use Deployer\Task\Scenario\Scenario;
18
use Deployer\Type\Result;
19
use Deployer\Cluster\ClusterFactory;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
// There are two types of functions: Deployer dependent and Context dependent.
26
// Deployer dependent function uses in definition stage of recipe and may require Deployer::get() method.
27
// Context dependent function uses while task execution and must require only Context::get() method.
28
// But there is also a third type of functions: mixed. Mixed function uses in definition stage and in task
29
// execution stage. They are acts like two different function, but have same name. Example of such function
30
// is env() func. This function determine in which stage it was called by Context::get() method.
31
32
/**
33
 * @param string $name
34
 * @param string|null $host
35
 * @param int $port
36
 * @return Builder
37
 */
38
function server($name, $host = null, $port = 22)
39
{
40 2
    $deployer = Deployer::get();
41
42 2
    $env = new Environment();
43 2
    $config = new Configuration($name, $host, $port);
44
45 2
    if ($deployer->parameters->has('ssh_type') && $deployer->parameters->get('ssh_type') === 'ext-ssh2') {
46
        $server = new Remote\SshExtension($config);
47
    } else {
48 2
        $server = new Remote\PhpSecLib($config);
49
    }
50
51 4
    $deployer->servers->set($name, $server);
52 2
    $deployer->environments->set($name, $env);
53
54 2
    return new Builder($config, $env);
55
}
56
57
58
/**
59
 * @param string $name
60
 * @return Builder
61
 */
62
function localServer($name)
63
{
64 15
    $deployer = Deployer::get();
65
66 15
    $env = new Environment();
67 15
    $server = new Local();
68 15
    $config = new Configuration($name, 'localhost'); // Builder requires server configuration.
69
70 15
    $deployer->servers->set($name, $server);
71 15
    $deployer->environments->set($name, $env);
72
73 15
    return new Builder($config, $env);
74
}
75
76
/**
77
 * @param string $name Name of the cluster
78
 * @param array $nodes An array of nodes' host/ip
79
 * @param int $port Ssh port of the nodes
80
 *
81
 * Example:
82
 * You should pass a cluster name and nodes array.
83
 * Nodes array should be as following:
84
 * [ '192.168.1.1', 'example.com', '192.168.1.5' ]
85
 * @return \Deployer\Cluster\ClusterBuilder
86
 */
87
88
function cluster($name, $nodes, $port = 22)
89
{
90 2
    $deployer = Deployer::get();
91
    
92 2
    $cluster = ClusterFactory::create($deployer, $name, $nodes, $port);
93
    
94 2
    return $cluster->getBuilder();
95
}
96
97
98
/**
99
 * Load server list file.
100
 * @param string $file
101
 */
102
function serverList($file)
103 4
{
104 1
    $bootstrap = new \Deployer\Bootstrap\BootstrapByConfigFile();
105 1
    $bootstrap->setConfig($file);
106 1
    $bootstrap->parseConfig();
107 1
    $bootstrap->initServers();
108 1
    $bootstrap->initClusters();
109 1
}
110
111
/**
112
 * Define a new task and save to tasks list.
113
 *
114
 * @param string $name Name of current task.
115
 * @param callable|array $body Callable task or array of other tasks names.
116
 * @return TheTask
117
 * @throws InvalidArgumentException
118
 */
119
function task($name, $body)
120
{
121 17
    $deployer = Deployer::get();
122
123 17
    if ($body instanceof \Closure) {
124 17
        $task = new TheTask($name, $body);
125 17
        $scenario = new Scenario($name);
126 17
    } elseif (is_array($body)) {
127 3
        $task = new GroupTask();
128
        $scenario = new GroupScenario(array_map(function ($name) use ($deployer) {
129 3
            return $deployer->scenarios->get($name);
130 3
        }, $body));
131 3
    } else {
132 1
        throw new InvalidArgumentException('Task should be an closure or array of other tasks.');
133
    }
134
135 17
    $deployer->tasks->set($name, $task);
136 17
    $deployer->scenarios->set($name, $scenario);
137
138 17
    return $task;
139
}
140
141
/**
142
 * Call that task before specified task runs.
143
 *
144
 * @param string $it
145
 * @param string $that
146
 */
147
function before($it, $that)
148
{
149 15
    $deployer = Deployer::get();
150 2
    $beforeScenario = $deployer->scenarios->get($it);
151 2
    $scenario = $deployer->scenarios->get($that);
152
153 1
    $beforeScenario->addBefore($scenario);
154 1
}
155
156
/**
157
 * Call that task after specified task runs.
158
 *
159
 * @param string $it
160
 * @param string $that
161
 */
162
function after($it, $that)
163
{
164 1
    $deployer = Deployer::get();
165 1
    $afterScenario = $deployer->scenarios->get($it);
166 1
    $scenario = $deployer->scenarios->get($that);
167
168 1
    $afterScenario->addAfter($scenario);
169 1
}
170
171
/**
172
 * Add users arguments.
173
 *
174
 * Note what Deployer already has one argument: "stage".
175
 *
176
 * @param string $name
177
 * @param int $mode
178
 * @param string $description
179
 * @param mixed $default
180
 */
181
function argument($name, $mode = null, $description = '', $default = null)
182
{
183 14
    Deployer::get()->getConsole()->getUserDefinition()->addArgument(
184 14
        new InputArgument($name, $mode, $description, $default)
185 14
    );
186 14
}
187
188
/**
189
 * Add users options.
190
 *
191
 * @param string $name
192
 * @param string $shortcut
193
 * @param int $mode
194
 * @param string $description
195
 * @param mixed $default
196
 */
197
function option($name, $shortcut = null, $mode = null, $description = '', $default = null)
198
{
199 14
    Deployer::get()->getConsole()->getUserDefinition()->addOption(
200 14
        new InputOption($name, $shortcut, $mode, $description, $default)
201 14
    );
202 14
}
203
204
/**
205
 * Change the current working directory.
206
 *
207
 * @param string $path
208
 */
209
function cd($path)
210
{
211 2
    env('working_path', env()->parse($path));
212 2
}
213
214
/**
215
 * Execute a callback within a specific directory and revert back to the initial working directory.
216
 *
217
 * @param string $path
218
 * @param callable $callback
219
 */
220
function within($path, $callback)
221
{
222
    $lastWorkingPath = workingPath();
223
    env()->set('working_path', $path);
224
    $callback();
225
    env()->set('working_path', $lastWorkingPath);
226
}
227
228
/**
229
 * Return the current working path.
230
 *
231
 * @return string
232
 */
233
function workingPath()
234
{
235 12
    return env()->get('working_path', env()->get(Environment::DEPLOY_PATH, ''));
236
}
237
238
/**
239
 * Run command on server.
240
 *
241
 * @param string $command
242
 * @return Result
243
 */
244
function run($command)
245
{
246 12
    $server = Context::get()->getServer();
247 12
    $command = env()->parse($command);
248 12
    $workingPath = workingPath();
249
250 12
    if (!empty($workingPath)) {
251 11
        $command = "cd $workingPath && $command";
252 11
    }
253
254 12
    if (isVeryVerbose()) {
255
        writeln("<fg=red>></fg=red> $command");
256
    }
257
258 12
    $output = $server->run($command);
259
260 12
    if (isDebug() && !empty($output)) {
261
        output()->writeln(array_map(function ($line) {
262
            return "\033[1;30m< $line\033[0m";
263
        }, explode("\n", $output)), OutputInterface::OUTPUT_RAW);
264
    }
265
266 12
    return new Result($output);
267
}
268
269
/**
270
 * Execute commands on local machine.
271
 * @param string $command Command to run locally.
272
 * @param int $timeout (optional) Override process command timeout in seconds.
273
 * @return Result Output of command.
274
 * @throws \RuntimeException
275
 */
276
function runLocally($command, $timeout = 60)
277
{
278 5
    $command = env()->parse($command);
279
280 5
    if (isVeryVerbose()) {
281
        writeln("<comment>Run locally</comment>: $command");
282
    }
283
284 5
    $process = new Symfony\Component\Process\Process($command);
285 5
    $process->setTimeout($timeout);
286
    $process->run(function ($type, $buffer) {
287 4
        if (isDebug()) {
288
            if ('err' === $type) {
289
                write("<fg=red>></fg=red> $buffer");
290
            } else {
291
                write("<fg=green>></fg=green> $buffer");
292
            }
293
        }
294 5
    });
295
296 5
    if (!$process->isSuccessful()) {
297
        throw new \RuntimeException($process->getErrorOutput());
298
    }
299
300 5
    return new Result($process->getOutput());
301
}
302
303
/**
304
 * Upload file or directory to current server.
305
 * @param string $local
306
 * @param string $remote
307
 * @throws \RuntimeException
308
 */
309
function upload($local, $remote)
310
{
311 1
    $server = Context::get()->getServer();
312 1
    $local = env()->parse($local);
313 1
    $remote = env()->parse($remote);
314
315 1
    if (is_file($local)) {
316 1
        writeln("Upload file <info>$local</info> to <info>$remote</info>");
317
318 1
        $server->upload($local, $remote);
319 1
    } elseif (is_dir($local)) {
320 1
        writeln("Upload from <info>$local</info> to <info>$remote</info>");
321
322 1
        $finder = new Symfony\Component\Finder\Finder();
323
        $files = $finder
324 1
            ->files()
325 1
            ->ignoreUnreadableDirs()
326 1
            ->ignoreVCS(true)
327 1
            ->ignoreDotFiles(false)
328 1
            ->in($local);
329
330
        /** @var $file \Symfony\Component\Finder\SplFileInfo */
331 1
        foreach ($files as $file) {
332 1
            if (isDebug()) {
333
                writeln("Uploading <info>{$file->getRealPath()}</info>");
334
            }
335
            
336 1
            $server->upload(
337 1
                $file->getRealPath(),
338 1
                $remote . '/' . $file->getRelativePathname()
339 1
            );
340 1
        }
341 1
    } else {
342
        throw new \RuntimeException("Uploading path '$local' does not exist.");
343
    }
344 1
}
345
346
/**
347
 * Download file from remote server.
348
 *
349
 * @param string $local
350
 * @param string $remote
351
 */
352
function download($local, $remote)
353
{
354 2
    $server = Context::get()->getServer();
355 2
    $local = env()->parse($local);
356 2
    $remote = env()->parse($remote);
357
358 2
    $server->download($local, $remote);
359 2
}
360
361
/**
362
 * Writes a message to the output and adds a newline at the end.
363
 * @param string|array $message
364
 */
365
function writeln($message)
366
{
367 4
    output()->writeln($message);
368 4
}
369
370
/**
371
 * Writes a message to the output.
372
 * @param string $message
373
 */
374
function write($message)
375
{
376
    output()->write($message);
377
}
378
379
/**
380
 * @param string $key
381
 * @param mixed $value
382
 */
383
function set($key, $value)
384
{
385 14
    Deployer::get()->parameters->set($key, $value);
386 14
}
387
388
/**
389
 * @param string $key
390
 * @return mixed
391
 */
392
function get($key)
393
{
394 4
    return Deployer::get()->parameters->get($key);
395
}
396
397
/**
398
 * @param string $key
399
 * @return boolean
400
 */
401
function has($key)
402
{
403
    return Deployer::get()->parameters->has($key);
404
}
405
406
/**
407
 * @param string $message
408
 * @param string|null $default
409
 * @return string
410
 * @codeCoverageIgnore
411
 */
412
function ask($message, $default = null)
413
{
414
    if (isQuiet()) {
415
        return $default;
416
    }
417
418
    $helper = Deployer::get()->getHelper('question');
419
420
    $message = "<question>$message" . (($default === null) ? "" : " [$default]") . "</question> ";
421
422
    $question = new \Symfony\Component\Console\Question\Question($message, $default);
423
424
    return $helper->ask(input(), output(), $question);
425
}
426
427
/**
428
 * @param string $message
429
 * @param bool $default
430
 * @return bool
431
 * @codeCoverageIgnore
432
 */
433
function askConfirmation($message, $default = false)
434
{
435
    if (isQuiet()) {
436
        return $default;
437
    }
438
439
    $helper = Deployer::get()->getHelper('question');
440
441
    $yesOrNo = $default ? 'Y/n' : 'y/N';
442
    $message = "<question>$message [$yesOrNo]</question> ";
443
444
    $question = new \Symfony\Component\Console\Question\ConfirmationQuestion($message, $default);
445
446
    return $helper->ask(input(), output(), $question);
447
}
448
449
/**
450
 * @param string $message
451
 * @return string
452
 * @codeCoverageIgnore
453
 */
454
function askHiddenResponse($message)
455
{
456
    if (isQuiet()) {
457
        return '';
458
    }
459
460
    $helper = Deployer::get()->getHelper('question');
461
462
    $message = "<question>$message</question> ";
463
464
    $question = new \Symfony\Component\Console\Question\Question($message);
465
    $question->setHidden(true);
466
    $question->setHiddenFallback(false);
467
468
    return $helper->ask(input(), output(), $question);
469
}
470
471
/**
472
 * @return InputInterface
473
 */
474
function input()
475
{
476
    return Context::get()->getInput();
477
}
478
479
480
/**
481
 * @return OutputInterface
482
 */
483
function output()
484
{
485 17
    return Context::get()->getOutput();
486
}
487
488
/**
489
 * @return bool
490
 */
491
function isQuiet()
492
{
493
    return OutputInterface::VERBOSITY_QUIET === output()->getVerbosity();
494
}
495
496
497
/**
498
 * @return bool
499
 */
500
function isVerbose()
501
{
502 1
    return OutputInterface::VERBOSITY_VERBOSE <= output()->getVerbosity();
503
}
504
505
506
/**
507
 * @return bool
508
 */
509
function isVeryVerbose()
510
{
511 16
    return OutputInterface::VERBOSITY_VERY_VERBOSE <= output()->getVerbosity();
512
}
513
514
515
/**
516
 * @return bool
517
 */
518
function isDebug()
519
{
520 17
    return OutputInterface::VERBOSITY_DEBUG <= output()->getVerbosity();
521
}
522
523
/**
524
 * Return current server env or set default values or get env value.
525
 * When set env value you can write over values line "{{name}}".
526
 *
527
 * @param string|null $name
528
 * @param mixed $value
529
 * @return Environment|mixed
530
 */
531
function env($name = null, $value = null)
532
{
533 19
    if (false === Context::get()) {
534 14
        Environment::setDefault($name, $value);
535 14
    } else {
536 19
        if (null === $name && null === $value) {
537 19
            return Context::get()->getEnvironment();
538 8
        } elseif (null !== $name && null === $value) {
539 6
            return Context::get()->getEnvironment()->get($name);
540
        } else {
541 2
            Context::get()->getEnvironment()->set($name, $value);
542
        }
543 2
        return null;
544
    }
545 14
}
546
547
/**
548
 * Check if command exist in bash.
549
 *
550
 * @param string $command
551
 * @return bool
552
 */
553
function commandExist($command)
554
{
555 2
    return run("if hash $command 2>/dev/null; then echo 'true'; fi")->toBool();
556
}
557