Completed
Push — master ( d3a073...5737c8 )
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 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
use Psr\Log\LoggerAwareInterface;
13
use Psr\Log\LoggerAwareTrait;
14
15
class Application extends SymfonyApplication implements IOAwareInterface, LoggerAwareInterface
16
{
17
    use IO;
18
    use LoggerAwareTrait;
19
20
    /**
21
     * @param string $name
22
     * @param string $version
23
     */
24
    public function __construct($name, $version)
25
    {
26
        parent::__construct($name, $version);
27
28
        $this->getDefinition()
29
            ->addOption(
30
                new InputOption('--simulate', null, InputOption::VALUE_NONE, 'Run in simulated mode (show what would have happened).')
31
            );
32
        $this->getDefinition()
33
            ->addOption(
34
                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)
35
            );
36
37
        $this->getDefinition()
38
            ->addOption(
39
                new InputOption('--define', '-D', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Define a configuration item value.', [])
40
            );
41
    }
42
43
    /**
44
     * @param string $roboFile
45
     * @param string $roboClass
46
     */
47
    public function addInitRoboFileCommand($roboFile, $roboClass)
48
    {
49
        $createRoboFile = new Command('init');
50
        $createRoboFile->setDescription("Intitalizes basic RoboFile in current dir");
51
        $createRoboFile->setCode(function () use ($roboClass, $roboFile) {
52
            $output = Robo::output();
53
            $output->writeln("<comment>  ~~~ Welcome to Robo! ~~~~ </comment>");
54
            $output->writeln("<comment>  ". basename($roboFile) ." will be created in the current directory </comment>");
55
            file_put_contents(
56
                $roboFile,
57
                '<?php'
58
                . "\n/**"
59
                . "\n * This is project's console commands configuration for Robo task runner."
60
                . "\n *"
61
                . "\n * @see http://robo.li/"
62
                . "\n */"
63
                . "\nclass " . $roboClass . " extends \\Robo\\Tasks\n{\n    // define public methods as commands\n}"
64
            );
65
            $output->writeln("<comment>  Edit this file to add your commands! </comment>");
66
        });
67
        $this->add($createRoboFile);
68
    }
69
70
    /**
71
     * Add self update command, do nothing if null is provided
72
     *
73
     * @param string $repository GitHub Repository for self update
74
     */
75
    public function addSelfUpdateCommand($repository = null)
76
    {
77
        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...
78
            return;
79
        }
80
        $selfUpdateCommand = new SelfUpdateCommand($this->getName(), $this->getVersion(), $repository);
81
        $this->add($selfUpdateCommand);
82
    }
83
84
    protected function configureIO(InputInterface $input, OutputInterface $output)
85
    {
86
        parent::configureIO($input, $output);
87
        $this->resetIO($input, $output);
88
        if ($this->logger instanceof \Consolidation\Log\LoggerManager) {
89
            $this->logger->add('default', $this->createLogger($output));
90
        }
91
    }
92
93
    protected function createLogger($output)
94
    {
95
        return new \Robo\Log\RoboLogger($output);
96
    }
97
}
98