Completed
Pull Request — master (#12)
by Robbie
01:59
created

Scaffold::bootstrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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