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 ( 899190...25f427 )
by Anton
02:48
created

functions.php ➔ test()   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 1
dl 0
loc 4
ccs 0
cts 2
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\Type\Result;
19
use Monolog\Logger;
20
use Symfony\Component\Console\Question\ConfirmationQuestion;
21
use Symfony\Component\Console\Question\Question;
22
use Symfony\Component\Finder\Finder;
23
use Symfony\Component\Process\Exception\ProcessFailedException;
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|string $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);
151 2
    } elseif (is_string($body)) {
152 2
        $task = new T($name, function () use ($body) {
153
            run($body);
154 1
        });
155 1
    } else {
156
        throw new \InvalidArgumentException('Task should be an closure or array of other tasks.');
157
    }
158
159
    $deployer->tasks->set($name, $task);
160
161
    if (!empty(desc())) {
162
        $task->desc(desc());
163
        desc(''); // Clear title.
164
    }
165 1
166 1
    return $task;
167 1
}
168
169 1
/**
170 1
 * Call that task before specified task runs.
171
 *
172
 * @param string $it
173
 * @param string $that
174
 */
175
function before($it, $that)
176
{
177
    $deployer = Deployer::get();
178
    $beforeTask = $deployer->tasks->get($it);
179
180
    $beforeTask->addBefore($that);
181
}
182
183
/**
184 14
 * Call that task after specified task runs.
185 14
 *
186 14
 * @param string $it
187 14
 * @param string $that
188
 */
189
function after($it, $that)
190
{
191
    $deployer = Deployer::get();
192
    $afterTask = $deployer->tasks->get($it);
193
194
    $afterTask->addAfter($that);
195
}
196
197
/**
198
 * Setup which task run on failure of first.
199
 *
200 14
 * @param string $it
201 14
 * @param string $that
202 14
 */
203 14
function onFailure($it, $that)
204
{
205
    $deployer = Deployer::get();
206
    $deployer['onFailure']->set($it, $that);
207
}
208
209
/**
210
 * Add users arguments.
211
 *
212 2
 * Note what Deployer already has one argument: "stage".
213 2
 *
214
 * @param string $name
215
 * @param int $mode
216
 * @param string $description
217
 * @param mixed $default
218
 */
219
function argument($name, $mode = null, $description = '', $default = null)
220
{
221
    Deployer::get()->getConsole()->getUserDefinition()->addArgument(
222
        new InputArgument($name, $mode, $description, $default)
223
    );
224
}
225
226
/**
227
 * Add users options.
228
 *
229
 * @param string $name
230
 * @param string $shortcut
231
 * @param int $mode
232
 * @param string $description
233
 * @param mixed $default
234
 */
235
function option($name, $shortcut = null, $mode = null, $description = '', $default = null)
236 12
{
237
    Deployer::get()->getConsole()->getUserDefinition()->addOption(
238
        new InputOption($name, $shortcut, $mode, $description, $default)
239
    );
240
}
241
242
/**
243
 * Change the current working directory.
244
 *
245
 * @param string $path
246
 */
247 12
function cd($path)
248 12
{
249 12
    set('working_path', parse($path));
250
}
251 12
252 11
/**
253 11
 * Execute a callback within a specific directory and revert back to the initial working directory.
254
 *
255 12
 * @param string $path
256
 * @param callable $callback
257
 */
258
function within($path, $callback)
259 12
{
260
    $lastWorkingPath = workingPath();
261 12
    set('working_path', $path);
262
    $callback();
263
    set('working_path', $lastWorkingPath);
264
}
265
266
/**
267 12
 * Return the current working path.
268
 *
269
 * @return string
270
 */
271
function workingPath()
272
{
273
    return get('working_path', get(Environment::DEPLOY_PATH, ''));
274
}
275
276
/**
277
 * Run command on server.
278
 *
279 5
 * @param string $command
280
 * @return Result
281 5
 */
282
function run($command)
283
{
284
    $server = Context::get()->getServer();
285 5
    $serverName = $server->getConfiguration()->getName();
286 5
    $command = parse($command);
287
    $workingPath = workingPath();
288 4
289
    if (!empty($workingPath)) {
290
        $command = "cd $workingPath && ($command)";
291
    }
292
293
    if (isVeryVerbose()) {
294
        writeln("[$serverName] <fg=red>></fg=red> $command");
295 5
    }
296
297 5
    logger("[$serverName] > $command");
298
299
    if ($server instanceof Local) {
300
        $output = $server->mustRun($command, function ($type, $buffer) use ($serverName) {
301 5 View Code Duplication
            if (isDebug()) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
302
                output()->writeln(array_map(function ($line) use ($serverName) {
303
                    return output()->isDecorated()
304
                        ? "[$serverName] \033[1;30m< $line\033[0m"
305
                        : "[$serverName] < $line";
306
                }, explode("\n", rtrim($buffer))), OutputInterface::OUTPUT_RAW);
307
            }
308
        });
309
    } else {
310
        $output = $server->run($command);
311 View Code Duplication
        if (isDebug() && !empty($output)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
312 1
            output()->writeln(array_map(function ($line) use ($serverName) {
313 1
                return output()->isDecorated()
314 1
                    ? "[$serverName] \033[1;30m< $line\033[0m"
315
                    : "[$serverName] < $line";
316 1
            }, explode("\n", rtrim($output))), OutputInterface::OUTPUT_RAW);
317 1
        }
318
    }
319 1
320 1
    if (!empty(rtrim($output))) {
321 1
        logger("[$serverName] < $output");
322
    }
323 1
324
    return new Result($output);
325 1
}
326 1
327 1
/**
328 1
 * Execute commands on local machine.
329 1
 * @param string $command Command to run locally.
330
 * @param int $timeout (optional) Override process command timeout in seconds.
331
 * @return Result Output of command.
332 1
 * @throws \RuntimeException
333 1
 */
334
function runLocally($command, $timeout = 60)
335
{
336
    $command = parse($command);
337 1
338 1
    if (isVeryVerbose()) {
339 1
        writeln("[localhost] <fg=red>></fg=red> : $command");
340 1
    }
341 1
342 1
    logger("[localhost] > $command");
343
344
    $process = new Process($command);
345 1
    $process->setTimeout($timeout);
346
    $process->run(function ($type, $buffer) {
347
        if (isDebug()) {
348
            if ('err' === $type) {
349
                write("<fg=red>></fg=red> $buffer");
350
            } else {
351
                write("<fg=green>></fg=green> $buffer");
352
            }
353
        }
354
    });
355 2
356 2
    if (!$process->isSuccessful()) {
357 2
        throw new ProcessFailedException($process);
358
    }
359 2
360 2
    $output = $process->getOutput();
361
362
    logger("[localhost] < $output");
363
364
    return new Result($output);
365
}
366
367
/**
368 4
 * Run test command.
369 4
 * Example:
370
 *
371
 *     test('[ -d {{release_path}} ]')
372
 *
373
 * @param string $command
374
 * @return bool
375
 */
376
function test($command)
377
{
378
    return run("if $command; then echo 'true'; fi")->toBool();
379
}
380
381
/**
382
 * Upload file or directory to current server.
383
 * @param string $local
384
 * @param string $remote
385
 * @throws \RuntimeException
386 14
 */
387 14
function upload($local, $remote)
388
{
389
    $server = Context::get()->getServer();
390
    $local = parse($local);
391
    $remote = parse($remote);
392
393
    if (is_file($local)) {
394
        writeln("Upload file <info>$local</info> to <info>$remote</info>");
395 4
396
        $server->upload($local, $remote);
397
    } elseif (is_dir($local)) {
398
        writeln("Upload from <info>$local</info> to <info>$remote</info>");
399
400
        $finder = new Finder();
401
        $files = $finder
402
            ->files()
403
            ->ignoreUnreadableDirs()
404
            ->ignoreVCS(true)
405
            ->ignoreDotFiles(false)
406
            ->in($local);
407
408
        /** @var $file \Symfony\Component\Finder\SplFileInfo */
409
        foreach ($files as $file) {
410
            if (isDebug()) {
411
                writeln("Uploading <info>{$file->getRealPath()}</info>");
412
            }
413
414
            $server->upload(
415
                $file->getRealPath(),
416
                $remote . '/' . $file->getRelativePathname()
417
            );
418
        }
419
    } else {
420
        throw new \RuntimeException("Uploading path '$local' does not exist.");
421
    }
422
}
423
424
/**
425
 * Download file from remote server.
426
 *
427
 * @param string $local
428
 * @param string $remote
429
 */
430
function download($local, $remote)
431
{
432
    $server = Context::get()->getServer();
433
    $local = parse($local);
434
    $remote = parse($remote);
435
436
    $server->download($local, $remote);
437
}
438
439
/**
440
 * Writes a message to the output and adds a newline at the end.
441
 * @param string|array $message
442
 */
443
function writeln($message)
444
{
445
    output()->writeln($message);
446
}
447
448
/**
449
 * Writes a message to the output.
450
 * @param string $message
451
 */
452
function write($message)
453
{
454
    output()->write($message);
455
}
456
457
/**
458
 * @param string $message
459
 * @param int $level
460
 */
461
function logger($message, $level = Logger::DEBUG)
462
{
463
    if (is_array($message)) {
464
        $message = join("\n", $message);
465
    }
466
467
    Deployer::get()->getLogger()->log($level, $message);
468
}
469
470
/**
471
 * Setup configuration option.
472
 *
473
 * @param string $name
474
 * @param mixed $value
475
 */
476
function set($name, $value)
477
{
478
    if (Context::get() === false) {
479
        Deployer::setDefault($name, $value);
480
    } else {
481
        Context::get()->getEnvironment()->set($name, $value);
482
    }
483
}
484
485
/**
486 17
 * Merge new config params to existing config array.
487
 *
488
 * @param string $name
489
 * @param array $array
490
 */
491
function add($name, $array)
492
{
493
    if (Context::get() === false) {
494
        Deployer::addDefault($name, $array);
495
    } else {
496
        Context::get()->getEnvironment()->add($name, $array);
497
    }
498
}
499
500
/**
501
 * Get configuration value.
502
 *
503 1
 * @param string $name
504
 * @param mixed|null $default
505
 * @return mixed
506
 */
507
function get($name, $default = null)
508
{
509
    if (Context::get() === false) {
510
        return Deployer::getDefault($name, $default);
511
    } else {
512 16
        return Context::get()->getEnvironment()->get($name, $default);
513
    }
514
}
515
516
/**
517
 * Check if there is such configuration option.
518
 *
519
 * @param string $name
520
 * @return boolean
521 17
 */
522
function has($name)
523
{
524
    if (Context::get() === false) {
525
        return Deployer::hasDefault($name);
526
    } else {
527
        return Context::get()->getEnvironment()->has($name);
528
    }
529
}
530
531
/**
532
 * @param string $message
533
 * @param string|null $default
534 19
 * @return string
535 14
 * @codeCoverageIgnore
536 14
 */
537 19
function ask($message, $default = null)
538 19
{
539 8
    if (isQuiet()) {
540 6
        return $default;
541
    }
542 2
543
    $helper = Deployer::get()->getHelper('question');
544 2
545
    $message = "<question>$message" . (($default === null) ? "" : " [$default]") . "</question> ";
546 14
547
    $question = new Question($message, $default);
548
549
    return $helper->ask(input(), output(), $question);
550
}
551
552
/**
553
 * @param string $message
554
 * @param bool $default
555
 * @return bool
556 2
 * @codeCoverageIgnore
557
 */
558
function askConfirmation($message, $default = false)
559
{
560
    if (isQuiet()) {
561
        return $default;
562
    }
563
564
    $helper = Deployer::get()->getHelper('question');
565
566
    $yesOrNo = $default ? 'Y/n' : 'y/N';
567
    $message = "<question>$message [$yesOrNo]</question> ";
568
569
    $question = new ConfirmationQuestion($message, $default);
570
571
    return $helper->ask(input(), output(), $question);
572
}
573
574
/**
575
 * @param string $message
576
 * @return string
577
 * @codeCoverageIgnore
578
 */
579
function askHiddenResponse($message)
580
{
581
    if (isQuiet()) {
582
        return '';
583
    }
584
585
    $helper = Deployer::get()->getHelper('question');
586
587
    $message = "<question>$message</question> ";
588
589
    $question = new Question($message);
590
    $question->setHidden(true);
591
    $question->setHiddenFallback(false);
592
593
    return $helper->ask(input(), output(), $question);
594
}
595
596
/**
597
 * @return InputInterface
598
 */
599
function input()
600
{
601
    return Context::get()->getInput();
602
}
603
604
605
/**
606
 * @return OutputInterface
607
 */
608
function output()
609
{
610
    return Context::get()->getOutput();
611
}
612
613
/**
614
 * @return bool
615
 */
616
function isQuiet()
617
{
618
    return OutputInterface::VERBOSITY_QUIET === output()->getVerbosity();
619
}
620
621
622
/**
623
 * @return bool
624
 */
625
function isVerbose()
626
{
627
    return OutputInterface::VERBOSITY_VERBOSE <= output()->getVerbosity();
628
}
629
630
631
/**
632
 * @return bool
633
 */
634
function isVeryVerbose()
635
{
636
    return OutputInterface::VERBOSITY_VERY_VERBOSE <= output()->getVerbosity();
637
}
638
639
640
/**
641
 * @return bool
642
 */
643
function isDebug()
644
{
645
    return OutputInterface::VERBOSITY_DEBUG <= output()->getVerbosity();
646
}
647
648
/**
649
 * Deprecated, use set()/get().
650
 * @deprecated
651
 */
652
function env()
653
{
654
    throw new \RuntimeException('env() function deprecated. Please, use set() or get() instead of.');
655
}
656
657
/**
658
 * Check if command exist in bash.
659
 *
660
 * @param string $command
661
 * @return bool
662
 */
663
function commandExist($command)
664
{
665
    return run("if hash $command 2>/dev/null; then echo 'true'; fi")->toBool();
666
}
667
668
/**
669
 * Parse set values.
670
 *
671
 * @param string $value
672
 * @return string
673
 */
674
function parse($value)
675
{
676
    return Context::get()->getEnvironment()->parse($value);
677
}
678