Completed
Push — master ( 53e74a...4c1ea9 )
by Robbie
01:57
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 Configuration
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
        if (is_null($this->configurationLoader)) {
133
            $this->setConfigurationLoader(new ConfigurationLoader);
0 ignored issues
show
Bug introduced by
The call to ConfigurationLoader::__construct() misses a required argument $application.

This check looks for function calls that miss required arguments.

Loading history...
134
        }
135
        return $this->configurationLoader;
136
    }
137
138
    /**
139
     * Set the configuration loader class
140
     *
141
     * @param  ConfigurationLoader
142
     * @return self
143
     */
144
    public function setConfigurationLoader(ConfigurationLoader $loader)
145
    {
146
        $this->configurationLoader = $loader;
0 ignored issues
show
Documentation Bug introduced by
It seems like $loader of type object<SilverLeague\Cons...er\ConfigurationLoader> is incompatible with the declared type object<SilverLeague\Cons...ramework\Configuration> of property $configurationLoader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
147
        return $this;
148
    }
149
150
    /**
151
     * Scaffold the Application, including adding all requires commands and configuration
152
     *
153
     * @return self
154
     */
155
    protected function scaffoldApplication()
156
    {
157
        $this->getApplication()->setName(self::APPLICATION_NAME);
158
        $this->getApplication()->setVersion('Version ' . self::APPLICATION_VERSION);
159
160
        $this->getApplication()->getDefinition()->addOption(
161
            new InputOption(
162
                'flush',
163
                'f',
164
                null,
165
                'Flush SilverStripe cache and manifest'
166
            )
167
        );
168
169
        foreach ($this->getSilverStripeLoader()->getTasks() as $command) {
170
            $this->getApplication()->add($command);
171
        }
172
173
        foreach ($this->getConfiguration()['Commands'] as $commandClass) {
174
            $this->getApplication()->add(new $commandClass);
175
        }
176
177
        return $this;
178
    }
179
}
180