Completed
Push — master ( 8b8afe...5f2bbe )
by Greg
02:21
created

src/Application.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Robo;
3
4
use SelfUpdate\SelfUpdateCommand;
5
use Symfony\Component\Console\Application as SymfonyApplication;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class Application extends SymfonyApplication
10
{
11
    /**
12
     * @param string $name
13
     * @param string $version
14
     */
15
    public function __construct($name, $version)
16
    {
17
        parent::__construct($name, $version);
18
19
        $this->getDefinition()
20
            ->addOption(
21
                new InputOption('--simulate', null, InputOption::VALUE_NONE, 'Run in simulated mode (show what would have happened).')
22
            );
23
        $this->getDefinition()
24
            ->addOption(
25
                new InputOption('--progress-delay', null, InputOption::VALUE_REQUIRED, 'Number of seconds before progress bar is displayed in long-running task collections. Default: 2s.', Config::DEFAULT_PROGRESS_DELAY)
26
            );
27
28
        $this->getDefinition()
29
            ->addOption(
30
                new InputOption('--define', '-D', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Define a configuration item value.', [])
31
            );
32
    }
33
34
    /**
35
     * @param string $roboFile
36
     * @param string $roboClass
37
     */
38
    public function addInitRoboFileCommand($roboFile, $roboClass)
39
    {
40
        $createRoboFile = new Command('init');
41
        $createRoboFile->setDescription("Intitalizes basic RoboFile in current dir");
42
        $createRoboFile->setCode(function () use ($roboClass, $roboFile) {
43
            $output = Robo::output();
44
            $output->writeln("<comment>  ~~~ Welcome to Robo! ~~~~ </comment>");
45
            $output->writeln("<comment>  ". basename($roboFile) ." will be created in the current directory </comment>");
46
            file_put_contents(
47
                $roboFile,
48
                '<?php'
49
                . "\n/**"
50
                . "\n * This is project's console commands configuration for Robo task runner."
51
                . "\n *"
52
                . "\n * @see http://robo.li/"
53
                . "\n */"
54
                . "\nclass " . $roboClass . " extends \\Robo\\Tasks\n{\n    // define public methods as commands\n}"
55
            );
56
            $output->writeln("<comment>  Edit this file to add your commands! </comment>");
57
        });
58
        $this->add($createRoboFile);
59
    }
60
61
    /**
62
     * Add self update command, do nothing if null is provided
63
     *
64
     * @param string $repository GitHub Repository for self update
65
     */
66
    public function addSelfUpdateCommand($repository = null)
67
    {
68
        if (!$repository || empty(\Phar::running())) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $repository of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
69
            return;
70
        }
71
        $selfUpdateCommand = new SelfUpdateCommand($this->getName(), $this->getVersion(), $repository);
72
        $this->add($selfUpdateCommand);
73
    }
74
}
75