Completed
Pull Request — master (#13)
by Robbie
01:51
created

Scaffold::scaffoldApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 18
rs 9.4285
c 2
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\Console\Framework;
4
5
use SilverLeague\Console\Framework\Loader\ConfigurationLoader;
6
use SilverLeague\Console\Framework\Loader\SilverStripeLoader;
7
use Symfony\Component\Console\Application;
8
use Symfony\Component\Console\Input\InputOption;
9
10
/**
11
 * The application scaffolder
12
 *
13
 * @package silverstripe-console
14
 * @author  Robbie Averill <[email protected]>
15
 */
16
class Scaffold extends ConsoleBase
17
{
18
    /**
19
     * The application name
20
     * @var string
21
     */
22
    const APPLICATION_NAME = <<<NAME
23
   _____ __             ______      _            _____                   __
24
  / __(_) /  _____ ____/ __/ /_____(_)__  ___   / ___/__  ___  ___ ___  / /__
25
 _\ \/ / / |/ / -_) __/\ \/ __/ __/ / _ \/ -_) / /__/ _ \/ _ \(_-</ _ \/ / -_)
26
/___/_/_/|___/\__/_/ /___/\__/_/ /_/ .__/\__/  \___/\___/_//_/___/\___/_/\__/
27
                                  /_/
28
29
NAME;
30
31
    /**
32
     * The application version (semver)
33
     * @var string
34
     */
35
    const APPLICATION_VERSION = '0.1.0';
36
37
    /**
38
     * The SilverStripe Loader class
39
     * @var SilverStripeLoader
40
     */
41
    protected $silverStripeLoader;
42
43
    /**
44
     * The Configuration Loader class
45
     * @var ConfigurationLoader
46
     */
47
    protected $configurationLoader;
48
49
    /**
50
     * The application configuration
51
     *
52
     * @var array
53
     */
54
    protected $configuration;
55
56
    /**
57
     * Instantiate the console Application
58
     */
59
    public function __construct()
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
__construct uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
60
    {
61
        parent::__construct(new Application);
62
63
        // Handle native SilverStripe flushing
64
        if (in_array('--flush', $_SERVER['argv'])) {
65
            $_GET['flush'] = 1;
66
        }
67
68
        $this->bootstrap();
69
        $this->setSilverStripeLoader(new SilverStripeLoader($this->getApplication()));
70
        $this->setConfigurationLoader(new ConfigurationLoader($this->getApplication()));
71
        $this->scaffoldApplication();
72
    }
73
74
    /**
75
     * Run the console Application
76
     *
77
     * @see Application::run
78
     * @return int Error code, or zero if successful
79
     */
80
    public function run()
81
    {
82
        return $this->getApplication()->run();
83
    }
84
85
    /**
86
     * Set the SilverStripeLoader
87
     *
88
     * @param  SilverStripeLoader $loader
89
     * @return self
90
     */
91
    public function setSilverStripeLoader(SilverStripeLoader $loader)
92
    {
93
        $this->silverStripeLoader = $loader;
94
        return $this;
95
    }
96
97
    /**
98
     * Get the SilverStripeLoader
99
     *
100
     * @return SilverStripeLoader
101
     */
102
    public function getSilverStripeLoader()
103
    {
104
        return $this->silverStripeLoader;
105
    }
106
107
    /**
108
     * Get the console application's configuration
109
     *
110
     * @return array
111
     */
112
    public function getConfiguration()
113
    {
114
        if (is_null($this->configuration)) {
115
            $this->setConfiguration($this->getConfigurationLoader()->load());
116
        }
117
        return $this->configuration;
118
    }
119
120
    /**
121
     * Set the console application's configuration
122
     *
123
     * @param  array $configuration
124
     * @return self
125
     */
126
    public function setConfiguration(array $configuration)
127
    {
128
        $this->configuration = $configuration;
129
        return $this;
130
    }
131
132
    /**
133
     * Get the configuration loader class
134
     *
135
     * @return ConfigurationLoader
136
     */
137
    public function getConfigurationLoader()
138
    {
139
        return $this->configurationLoader;
140
    }
141
142
    /**
143
     * Set the configuration loader class
144
     *
145
     * @param  ConfigurationLoader
146
     * @return self
147
     */
148
    public function setConfigurationLoader(ConfigurationLoader $loader)
149
    {
150
        $this->configurationLoader = $loader;
151
        return $this;
152
    }
153
154
    /**
155
     * Call the SilverStripe bootstrap class
156
     *
157
     * @return self
158
     */
159
    protected function bootstrap()
160
    {
161
        (new Bootstrap)->initialize();
162
        return $this;
163
    }
164
165
    /**
166
     * Scaffold the Application, including adding all requires commands and configuration
167
     *
168
     * @return self
169
     */
170
    protected function scaffoldApplication()
171
    {
172
        $this->getApplication()->setName(self::APPLICATION_NAME);
173
        $this->getApplication()->setVersion('Version ' . self::APPLICATION_VERSION);
174
175
        $this->getApplication()->getDefinition()->addOption(
176
            new InputOption(
177
                'flush',
178
                'f',
179
                null,
180
                'Flush SilverStripe cache and manifest'
181
            )
182
        );
183
184
        $this->addCommands();
185
186
        return $this;
187
    }
188
189
    /**
190
     * Adds all automatically created BuildTask Commands, and all concrete Commands from configuration
191
     *
192
     * @return self
193
     */
194
    protected function addCommands()
195
    {
196
        foreach ($this->getSilverStripeLoader()->getTasks() as $command) {
197
            $this->getApplication()->add($command);
198
        }
199
200
        foreach ($this->getConfiguration()['Commands'] as $commandClass) {
201
            $this->getApplication()->add(new $commandClass);
202
        }
203
204
        return $this;
205
    }
206
}
207