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 ( 6650cb...0d37f5 )
by Anton
02:20
created

functions.php ➔ desc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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