Completed
Push — master ( 441321...88de30 )
by Martijn van
02:09
created

SilverstripeApplication::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
use Symfony\Component\Console\Input\ArrayInput;
4
use Symfony\Component\Console\Input\InputOption;
5
use Symfony\Component\Console\Output\BufferedOutput;
6
use Symfony\Component\Console\Application as SymfonyApplication;
7
use Symfony\Component\Console\Command\Command as SymfonyCommand;
8
9
/**
10
 * Class Application
11
 *
12
 * Shameless copy/paste from Taylor Otwell's Laravel
13
 */
14
class SilverstripeApplication extends SymfonyApplication
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
15
{
16
17
    /**
18
     * The output from the previous command.
19
     *
20
     * @var \Symfony\Component\Console\Output\BufferedOutput
21
     */
22
    protected $lastOutput;
23
24
    public function __construct()
25
    {
26
        parent::__construct();
27
28
        $this->loadCommands();
29
        
30
        $this->add($default = new DefaultCommand());
31
        $this->setDefaultCommand($default->getName());
32
33
        $this->setAutoExit(false);
34
        $this->setCatchExceptions(false);
35
    }
36
37
    public function run($input = null, $output = null)
38
    {
39
        return parent::run($input, $output);
40
    }
41
42
    /**
43
     * Run an Artisan console command by name.
44
     *
45
     * @param  string  $command
46
     * @param  array  $parameters
47
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be null|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
48
     */
49
    public function call($command, array $parameters = [])
50
    {
51
        $parameters = array_merge((array)$command, $parameters);
52
53
        $this->lastOutput = new BufferedOutput;
54
55
        $this->setCatchExceptions(false);
56
57
        $result = $this->run(new ArrayInput($parameters), $this->lastOutput);
58
59
        $this->setCatchExceptions(true);
60
61
        return $result;
62
    }
63
64
    /**
65
     * Get the output for the last run command.
66
     *
67
     * @return string
68
     */
69
    public function output()
70
    {
71
        return $this->lastOutput ? $this->lastOutput->fetch() : '';
72
    }
73
74
    /**
75
     * Add a command to the console.
76
     *
77
     * @param  \Symfony\Component\Console\Command\Command  $command
78
     * @return \Symfony\Component\Console\Command\Command
0 ignored issues
show
Documentation introduced by
Should the return type not be SymfonyCommand|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
79
     */
80
    public function add(SymfonyCommand $command)
81
    {
82
        return $this->addToParent($command);
83
    }
84
85
    /**
86
     * Add the command to the parent instance.
87
     *
88
     * @param  \Symfony\Component\Console\Command\Command  $command
89
     * @return \Symfony\Component\Console\Command\Command
0 ignored issues
show
Documentation introduced by
Should the return type not be SymfonyCommand|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
90
     */
91
    protected function addToParent(SymfonyCommand $command)
92
    {
93
        return parent::add($command);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (add() instead of addToParent()). Are you sure this is correct? If so, you might want to change this to $this->add().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
94
    }
95
96
    /**
97
     * Get the default input definitions for the applications.
98
     *
99
     * This is used to add the --env option to every available command.
100
     *
101
     * @return \Symfony\Component\Console\Input\InputDefinition
102
     */
103
    protected function getDefaultInputDefinition()
104
    {
105
        $definition = parent::getDefaultInputDefinition();
106
107
        $definition->addOption($this->getEnvironmentOption());
108
109
        return $definition;
110
    }
111
112
    /**
113
     * Get the global environment option for the definition.
114
     *
115
     * @return \Symfony\Component\Console\Input\InputOption
116
     */
117
    protected function getEnvironmentOption()
118
    {
119
        $message = 'The environment the command should run under.';
120
121
        return new InputOption('--env', null, InputOption::VALUE_OPTIONAL, $message);
122
    }
123
124
    /**
125
     * Load all available commands into the console application
126
     */
127
    protected function loadCommands()
128
    {
129
        /**
130
         * Why does this not work
131
         */
132
        ///$commands = ClassInfo::subclassesFor('SilverstripeCommand'); //var_dump($commands);exit();
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
133
134
        // This works, but does not load custom Commands
135
        $commands = ClassInfo::classes_for_folder(BASE_PATH . '/console/code/');
136
137
        /** @var SilverstripeCommand $command */
138
        foreach ($commands as $command) {
139
            $reflection = new ReflectionClass($command);
140
            if (!$reflection->isAbstract() && $reflection->isSubclassOf('SilverstripeCommand')) {
141
                $this->add(new $command());
142
            }
143
        }
144
    }
145
146
}
147