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
Push — master ( 374f40...22ad38 )
by Anton
02:19
created

functions.php ➔ dispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
namespace Deployer;
8
9
use Deployer\Builder\BuilderInterface;
10
use Deployer\Server\Local;
11
use Deployer\Server\Remote;
12
use Deployer\Server\Builder;
13
use Deployer\Server\Configuration;
14
use Deployer\Server\Environment;
15
use Deployer\Task\Task as T;
16
use Deployer\Task\Context;
17
use Deployer\Task\GroupTask;
18
use Deployer\Task\Scenario\GroupScenario;
19
use Deployer\Task\Scenario\Scenario;
20
use Deployer\Type\Result;
21
use Symfony\Component\Console\Question\ConfirmationQuestion;
22
use Symfony\Component\Console\Question\Question;
23
use Symfony\Component\Finder\Finder;
24
use Symfony\Component\Process\Process;
25
use Deployer\Cluster\ClusterFactory;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
31
// There are two types of functions: Deployer dependent and Context dependent.
32
// Deployer dependent function uses in definition stage of recipe and may require Deployer::get() method.
33
// Context dependent function uses while task execution and must require only Context::get() method.
34
// But there is also a third type of functions: mixed. Mixed function uses in definition stage and in task
35
// execution stage. They are acts like two different function, but have same name. Example of such function
36
// is set() func. This function determine in which stage it was called by Context::get() method.
37
38
/**
39
 * @param string $name
40
 * @param string|null $host
41 2
 * @param int $port
42
 * @return BuilderInterface
43 2
 */
44 2
function server($name, $host = null, $port = 22)
45
{
46 2
    $deployer = Deployer::get();
47
48
    $env = new Environment();
49 2
    $config = new Configuration($name, $host, $port);
50
51
    if (get('ssh_type') === 'ext-ssh2') {
52 2
        $server = new Remote\SshExtension($config);
53 2
    } elseif (get('ssh_type') && get('ssh_type') === 'native') {
54
        $server = new Remote\NativeSsh($config);
55 2
    } else {
56
        $server = new Remote\PhpSecLib($config);
57
    }
58
59
    $deployer->servers->set($name, $server);
60
    $deployer->environments->set($name, $env);
61
62
    return new Builder($config, $env);
63
}
64
65 15
66
/**
67 15
 * @param string $name
68 15
 * @return BuilderInterface
69 15
 */
70
function localServer($name)
71 15
{
72 15
    $deployer = Deployer::get();
73
74 15
    $env = new Environment();
75
    $config = new Configuration($name, 'localhost'); // Builder requires server configuration.
76
    $server = new Local($config);
77
78
    $deployer->servers->set($name, $server);
79
    $deployer->environments->set($name, $env);
80
81
    return new Builder($config, $env);
82
}
83
84
/**
85
 * @param string $name Name of the cluster
86
 * @param array $nodes An array of nodes' host/ip
87
 * @param int $port Ssh port of the nodes
88
 *
89
 * Example:
90
 * You should pass a cluster name and nodes array.
91 2
 * Nodes array should be as following:
92
 * [ '192.168.1.1', 'example.com', '192.168.1.5' ]
93 2
 * @return BuilderInterface
94
 */
95 2
function cluster($name, $nodes, $port = 22)
96
{
97
    $deployer = Deployer::get();
98
99
    $cluster = ClusterFactory::create($deployer, $name, $nodes, $port);
100
101
    return $cluster->getBuilder();
102
}
103
104
105 1
/**
106 1
 * Load server list file.
107 1
 * @param string $file
108 1
 */
109 1
function serverList($file)
110 1
{
111
    $bootstrap = new Bootstrap\BootstrapByConfigFile();
112
    $bootstrap->setConfig($file);
113
    $bootstrap->parseConfig();
114
    $bootstrap->initServers();
115
    $bootstrap->initClusters();
116
}
117
118
/**
119
 * Set task description.
120
 *
121
 * @param ?string $title
0 ignored issues
show
Documentation introduced by
The doc-type ?string could not be parsed: Unknown type name "?string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
122 17
 * @return ?string
0 ignored issues
show
Documentation introduced by
The doc-type ?string could not be parsed: Unknown type name "?string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
123
 */
124 17
function desc($title = null)
125 17
{
126 17
    static $store = null;
127 17
128 3
    if ($title === null) {
129
        return $store;
130 3
    } else {
131 3
        return $store = $title;
132 3
    }
133 1
}
134
135
/**
136 17
 * Define a new task and save to tasks list.
137 17
 *
138
 * @param string $name Name of current task.
139 17
 * @param callable|array $body Callable task or array of other tasks names.
140
 * @return Task\Task
141
 * @throws \InvalidArgumentException
142
 */
143
function task($name, $body)
144
{
145
    $deployer = Deployer::get();
146
147
    if ($body instanceof \Closure) {
148
        $task = new T($name, $body);
149 14
    } elseif (is_array($body)) {
150 1
        $task = new GroupTask($name, $body);
0 ignored issues
show
Documentation introduced by
$body is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
151 2
    } else {
152 2
        throw new \InvalidArgumentException('Task should be an closure or array of other tasks.');
153
    }
154 1
155 1
    $deployer->tasks->set($name, $task);
156
157
    if (!empty(desc())) {
158
        $task->desc(desc());
159
        desc(''); // Clear title.
160
    }
161
162
    return $task;
163
}
164
165 1
/**
166 1
 * Call that task before specified task runs.
167 1
 *
168
 * @param string $it
169 1
 * @param string $that
170 1
 */
171
function before($it, $that)
172
{
173
    $deployer = Deployer::get();
174
    $beforeTask = $deployer->tasks->get($it);
175
176
    $beforeTask->addBefore($that);
177
}
178
179
/**
180
 * Call that task after specified task runs.
181
 *
182
 * @param string $it
183
 * @param string $that
184 14
 */
185 14
function after($it, $that)
186 14
{
187 14
    $deployer = Deployer::get();
188
    $afterTask = $deployer->tasks->get($it);
189
190
    $afterTask->addAfter($that);
191
}
192
193
/**
194
 * Add users arguments.
195
 *
196
 * Note what Deployer already has one argument: "stage".
197
 *
198
 * @param string $name
199
 * @param int $mode
200 14
 * @param string $description
201 14
 * @param mixed $default
202 14
 */
203 14
function argument($name, $mode = null, $description = '', $default = null)
204
{
205
    Deployer::get()->getConsole()->getUserDefinition()->addArgument(
206
        new InputArgument($name, $mode, $description, $default)
207
    );
208
}
209
210
/**
211
 * Add users options.
212 2
 *
213 2
 * @param string $name
214
 * @param string $shortcut
215
 * @param int $mode
216
 * @param string $description
217
 * @param mixed $default
218
 */
219
function option($name, $shortcut = null, $mode = null, $description = '', $default = null)
220
{
221
    Deployer::get()->getConsole()->getUserDefinition()->addOption(
222
        new InputOption($name, $shortcut, $mode, $description, $default)
223
    );
224
}
225
226
/**
227
 * Change the current working directory.
228
 *
229
 * @param string $path
230
 */
231
function cd($path)
232
{
233
    set('working_path', Context::get()->getEnvironment()->parse($path));
234
}
235
236 12
/**
237
 * Execute a callback within a specific directory and revert back to the initial working directory.
238
 *
239
 * @param string $path
240
 * @param callable $callback
241
 */
242
function within($path, $callback)
243
{
244
    $lastWorkingPath = workingPath();
245
    set('working_path', $path);
246
    $callback();
247 12
    set('working_path', $lastWorkingPath);
248 12
}
249 12
250
/**
251 12
 * Return the current working path.
252 11
 *
253 11
 * @return string
254
 */
255 12
function workingPath()
256
{
257
    return get('working_path', get(Environment::DEPLOY_PATH, ''));
258
}
259 12
260
/**
261 12
 * Run command on server.
262
 *
263
 * @param string $command
264
 * @return Result
265
 */
266
function run($command)
267 12
{
268
    $server = Context::get()->getServer();
269
    $serverName = $server->getConfiguration()->getName();
270
    $command = Context::get()->getEnvironment()->parse($command);
271
    $workingPath = workingPath();
272
273
    if (!empty($workingPath)) {
274
        $command = "cd $workingPath && $command";
275
    }
276
277
    if (isVeryVerbose()) {
278
        writeln("[$serverName] <fg=red>></fg=red> $command");
279 5
    }
280
281 5
    $output = $server->run($command);
282
283
    if (isDebug() && !empty($output)) {
284
        output()->writeln(array_map(function ($line) use ($serverName) {
285 5
            return output()->isDecorated()
286 5
                ? "[$serverName] \033[1;30m< $line\033[0m"
287
                : "[$serverName] < $line";
288 4
        }, explode("\n", rtrim($output))), OutputInterface::OUTPUT_RAW);
289
    }
290
291
    return new Result($output);
292
}
293
294
/**
295 5
 * Execute commands on local machine.
296
 * @param string $command Command to run locally.
297 5
 * @param int $timeout (optional) Override process command timeout in seconds.
298
 * @return Result Output of command.
299
 * @throws \RuntimeException
300
 */
301 5
function runLocally($command, $timeout = 60)
302
{
303
    $command = Context::get()->getEnvironment()->parse($command);
304
305
    if (isVeryVerbose()) {
306
        writeln("<comment>Run locally</comment>: $command");
307
    }
308
309
    $process = new Process($command);
310
    $process->setTimeout($timeout);
311
    $process->run(function ($type, $buffer) {
312 1
        if (isDebug()) {
313 1
            if ('err' === $type) {
314 1
                write("<fg=red>></fg=red> $buffer");
315
            } else {
316 1
                write("<fg=green>></fg=green> $buffer");
317 1
            }
318
        }
319 1
    });
320 1
321 1
    if (!$process->isSuccessful()) {
322
        throw new \RuntimeException($process->getErrorOutput());
323 1
    }
324
325 1
    return new Result($process->getOutput());
326 1
}
327 1
328 1
329 1
/**
330
 * Upload file or directory to current server.
331
 * @param string $local
332 1
 * @param string $remote
333 1
 * @throws \RuntimeException
334
 */
335
function upload($local, $remote)
336
{
337 1
    $server = Context::get()->getServer();
338 1
    $local = Context::get()->getEnvironment()->parse($local);
339 1
    $remote = Context::get()->getEnvironment()->parse($remote);
340 1
341 1
    if (is_file($local)) {
342 1
        writeln("Upload file <info>$local</info> to <info>$remote</info>");
343
344
        $server->upload($local, $remote);
345 1
    } elseif (is_dir($local)) {
346
        writeln("Upload from <info>$local</info> to <info>$remote</info>");
347
348
        $finder = new Finder();
349
        $files = $finder
350
            ->files()
351
            ->ignoreUnreadableDirs()
352
            ->ignoreVCS(true)
353
            ->ignoreDotFiles(false)
354
            ->in($local);
355 2
356 2
        /** @var $file \Symfony\Component\Finder\SplFileInfo */
357 2
        foreach ($files as $file) {
358
            if (isDebug()) {
359 2
                writeln("Uploading <info>{$file->getRealPath()}</info>");
360 2
            }
361
362
            $server->upload(
363
                $file->getRealPath(),
364
                $remote . '/' . $file->getRelativePathname()
365
            );
366
        }
367
    } else {
368 4
        throw new \RuntimeException("Uploading path '$local' does not exist.");
369 4
    }
370
}
371
372
/**
373
 * Download file from remote server.
374
 *
375
 * @param string $local
376
 * @param string $remote
377
 */
378
function download($local, $remote)
379
{
380
    $server = Context::get()->getServer();
381
    $local = Context::get()->getEnvironment()->parse($local);
382
    $remote = Context::get()->getEnvironment()->parse($remote);
383
384
    $server->download($local, $remote);
385
}
386 14
387 14
/**
388
 * Writes a message to the output and adds a newline at the end.
389
 * @param string|array $message
390
 */
391
function writeln($message)
392
{
393
    output()->writeln($message);
394
}
395 4
396
/**
397
 * Writes a message to the output.
398
 * @param string $message
399
 */
400
function write($message)
401
{
402
    output()->write($message);
403
}
404
405
/**
406
 * Setup configuration option.
407
 *
408
 * @param string $name
409
 * @param mixed $value
410
 */
411
function set($name, $value)
412
{
413
    if (Context::get() === false) {
414
        Deployer::setDefault($name, $value);
415
    } else {
416
        Context::get()->getEnvironment()->set($name, $value);
417
    }
418
}
419
420
/**
421
 * Merge new config params to existing config array.
422
 *
423
 * @param string $name
424
 * @param array $array
425
 */
426
function add($name, $array)
427
{
428
    if (Context::get() === false) {
429
        Deployer::addDefault($name, $array);
430
    } else {
431
        Context::get()->getEnvironment()->add($name, $array);
432
    }
433
}
434
435
/**
436
 * Get configuration value.
437
 *
438
 * @param string $name
439
 * @param mixed|null $default
440
 * @return mixed
441
 */
442
function get($name, $default = null)
443
{
444
    if (Context::get() === false) {
445
        return Deployer::getDefault($name, $default);
446
    } else {
447
        return Context::get()->getEnvironment()->get($name, $default);
448
    }
449
}
450
451
/**
452
 * Check if there is such configuration option.
453
 *
454
 * @param string $name
455
 * @return boolean
456
 */
457
function has($name)
458
{
459
    if (Context::get() === false) {
460
        return Deployer::hasDefault($name);
461
    } else {
462
        return Context::get()->getEnvironment()->has($name);
463
    }
464
}
465
466
/**
467
 * @param string $message
468
 * @param string|null $default
469
 * @return string
470
 * @codeCoverageIgnore
471
 */
472
function ask($message, $default = null)
473
{
474
    if (isQuiet()) {
475
        return $default;
476
    }
477
478
    $helper = Deployer::get()->getHelper('question');
479
480
    $message = "<question>$message" . (($default === null) ? "" : " [$default]") . "</question> ";
481
482
    $question = new Question($message, $default);
483
484
    return $helper->ask(input(), output(), $question);
485
}
486 17
487
/**
488
 * @param string $message
489
 * @param bool $default
490
 * @return bool
491
 * @codeCoverageIgnore
492
 */
493
function askConfirmation($message, $default = false)
494
{
495
    if (isQuiet()) {
496
        return $default;
497
    }
498
499
    $helper = Deployer::get()->getHelper('question');
500
501
    $yesOrNo = $default ? 'Y/n' : 'y/N';
502
    $message = "<question>$message [$yesOrNo]</question> ";
503 1
504
    $question = new ConfirmationQuestion($message, $default);
505
506
    return $helper->ask(input(), output(), $question);
507
}
508
509
/**
510
 * @param string $message
511
 * @return string
512 16
 * @codeCoverageIgnore
513
 */
514
function askHiddenResponse($message)
515
{
516
    if (isQuiet()) {
517
        return '';
518
    }
519
520
    $helper = Deployer::get()->getHelper('question');
521 17
522
    $message = "<question>$message</question> ";
523
524
    $question = new Question($message);
525
    $question->setHidden(true);
526
    $question->setHiddenFallback(false);
527
528
    return $helper->ask(input(), output(), $question);
529
}
530
531
/**
532
 * @return InputInterface
533
 */
534 19
function input()
535 14
{
536 14
    return Context::get()->getInput();
537 19
}
538 19
539 8
540 6
/**
541
 * @return OutputInterface
542 2
 */
543
function output()
544 2
{
545
    return Context::get()->getOutput();
546 14
}
547
548
/**
549
 * @return bool
550
 */
551
function isQuiet()
552
{
553
    return OutputInterface::VERBOSITY_QUIET === output()->getVerbosity();
554
}
555
556 2
557
/**
558
 * @return bool
559
 */
560
function isVerbose()
561
{
562
    return OutputInterface::VERBOSITY_VERBOSE <= output()->getVerbosity();
563
}
564
565
566
/**
567
 * @return bool
568
 */
569
function isVeryVerbose()
570
{
571
    return OutputInterface::VERBOSITY_VERY_VERBOSE <= output()->getVerbosity();
572
}
573
574
575
/**
576
 * @return bool
577
 */
578
function isDebug()
579
{
580
    return OutputInterface::VERBOSITY_DEBUG <= output()->getVerbosity();
581
}
582
583
/**
584
 * Deprecated, use set()/get().
585
 * @deprecated
586
 */
587
function env()
588
{
589
    throw new \RuntimeException('env() function deprecated. Please, use set() or get() instead of.');
590
}
591
592
/**
593
 * Check if command exist in bash.
594
 *
595
 * @param string $command
596
 * @return bool
597
 */
598
function commandExist($command)
599
{
600
    return run("if hash $command 2>/dev/null; then echo 'true'; fi")->toBool();
601
}
602