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 ( 15ad93...82a2dc )
by Anton
05:46
created

functions.php ➔ add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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