Scaffold   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 97.92%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 7
dl 0
loc 191
rs 10
c 0
b 0
f 0
ccs 47
cts 48
cp 0.9792

11 Methods

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