Completed
Pull Request — master (#822)
by Greg
02:22
created

Application::configureIO()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
namespace Robo;
3
4
use Robo\Contract\IOAwareInterface;
5
use Robo\Common\IO;
6
use SelfUpdate\SelfUpdateCommand;
7
use Symfony\Component\Console\Application as SymfonyApplication;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class Application extends SymfonyApplication implements IOAwareInterface
14
{
15
    use IO;
16
17
    /**
18
     * @param string $name
19
     * @param string $version
20
     */
21
    public function __construct($name, $version)
22
    {
23
        parent::__construct($name, $version);
24
25
        $this->getDefinition()
26
            ->addOption(
27
                new InputOption('--simulate', null, InputOption::VALUE_NONE, 'Run in simulated mode (show what would have happened).')
28
            );
29
        $this->getDefinition()
30
            ->addOption(
31
                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)
32
            );
33
34
        $this->getDefinition()
35
            ->addOption(
36
                new InputOption('--define', '-D', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Define a configuration item value.', [])
37
            );
38
    }
39
40
    /**
41
     * @param string $roboFile
42
     * @param string $roboClass
43
     */
44
    public function addInitRoboFileCommand($roboFile, $roboClass)
45
    {
46
        $createRoboFile = new Command('init');
47
        $createRoboFile->setDescription("Intitalizes basic RoboFile in current dir");
48
        $createRoboFile->setCode(function () use ($roboClass, $roboFile) {
49
            $output = Robo::output();
50
            $output->writeln("<comment>  ~~~ Welcome to Robo! ~~~~ </comment>");
51
            $output->writeln("<comment>  ". basename($roboFile) ." will be created in the current directory </comment>");
52
            file_put_contents(
53
                $roboFile,
54
                '<?php'
55
                . "\n/**"
56
                . "\n * This is project's console commands configuration for Robo task runner."
57
                . "\n *"
58
                . "\n * @see http://robo.li/"
59
                . "\n */"
60
                . "\nclass " . $roboClass . " extends \\Robo\\Tasks\n{\n    // define public methods as commands\n}"
61
            );
62
            $output->writeln("<comment>  Edit this file to add your commands! </comment>");
63
        });
64
        $this->add($createRoboFile);
65
    }
66
67
    /**
68
     * Add self update command, do nothing if null is provided
69
     *
70
     * @param string $repository GitHub Repository for self update
71
     */
72
    public function addSelfUpdateCommand($repository = null)
73
    {
74
        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...
75
            return;
76
        }
77
        $selfUpdateCommand = new SelfUpdateCommand($this->getName(), $this->getVersion(), $repository);
78
        $this->add($selfUpdateCommand);
79
    }
80
81
    protected function configureIO(InputInterface $input, OutputInterface $output)
82
    {
83
        parent::configureIO($input, $output);
84
        $this->resetIO($input, $output);
85
    }
86
}
87