Completed
Push — master ( 52e441...fcb932 )
by Robbie
9s
created

Scaffold::setConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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