Completed
Push — master ( 383d0c...3523cb )
by Tom
04:14
created

Config::hasConfigCommandAliases()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
/*
3
 * @author Tom Klingenberg <[email protected]>
4
 */
5
6
namespace N98\Magento\Application;
7
8
use Composer\Autoload\ClassLoader;
9
use N98\Magento\Application;
10
use N98\Magento\Application\ConfigurationLoader;
11
use N98\Util\ArrayFunctions;
12
use N98\Util\BinaryString;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\ArgvInput;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\NullOutput;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Class Config
21
 *
22
 * Class representing the application configuration. Created to factor out configuration related application
23
 * functionality from @see N98\Magento\Application
24
 *
25
 * @package N98\Magento\Application
26
 */
27
class Config
28
{
29
    /**
30
     * @var array config data
31
     */
32
    private $config = array();
33
34
    /**
35
     * @var array
36
     */
37
    private $partialConfig = array();
38
39
    /**
40
     * @var ConfigurationLoader
41
     */
42
    private $loader;
43
44
    /**
45
     * @var array
46
     */
47
    private $initConfig;
48
49
    /**
50
     * @var boolean
51
     */
52
    private $isPharMode;
53
54
    /**
55
     * @var OutputInterface
56
     */
57
    private $output;
58
59
60
    /**
61
     * Config constructor.
62
     * @param array $initConfig
63
     * @param bool $isPharMode
64
     * @param OutputInterface $output [optional]
0 ignored issues
show
Documentation introduced by
Should the type for parameter $output not be null|OutputInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
65
     */
66
    public function __construct(array $initConfig = array(), $isPharMode = false, OutputInterface $output = null)
67
    {
68
        $this->initConfig = $initConfig;
69
        $this->isPharMode = (bool) $isPharMode;
70
        $this->output = $output ?: new NullOutput();
71
    }
72
73
    /**
74
     * alias magerun command in input from config
75
     *
76
     * @param InputInterface $input
77
     * @return ArgvInput|InputInterface
78
     */
79
    public function checkConfigCommandAlias(InputInterface $input)
0 ignored issues
show
Coding Style introduced by
checkConfigCommandAlias 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...
80
    {
81
        if ($this->hasConfigCommandAliases()) {
82
            foreach ($this->config['commands']['aliases'] as $alias) {
83
                if (is_array($alias)) {
84
                    $aliasCommandName = key($alias);
85
                    if ($input->getFirstArgument() == $aliasCommandName) {
86
                        $aliasCommandParams = array_slice(BinaryString::trimExplodeEmpty(' ',
87
                            $alias[$aliasCommandName]), 1);
88
                        if (count($aliasCommandParams) > 0) {
89
                            // replace with aliased data
90
                            $mergedParams = array_merge(
91
                                array_slice($_SERVER['argv'], 0, 2),
92
                                $aliasCommandParams,
93
                                array_slice($_SERVER['argv'], 2)
94
                            );
95
                            $input = new ArgvInput($mergedParams);
96
                        }
97
                    }
98
                }
99
            }
100
101
            return $input;
102
        }
103
104
        return $input;
105
    }
106
107
    /**
108
     * @param Command $command
109
     */
110
    public function registerConfigCommandAlias(Command $command)
111
    {
112
        if ($this->hasConfigCommandAliases()) {
113
            foreach ($this->config['commands']['aliases'] as $alias) {
114
                if (!is_array($alias)) {
115
                    continue;
116
                }
117
118
                $aliasCommandName = key($alias);
119
                $commandString    = $alias[$aliasCommandName];
120
121
                list($originalCommand) = explode(' ', $commandString);
122
                if ($command->getName() == $originalCommand) {
123
                    $currentCommandAliases   = $command->getAliases();
124
                    $currentCommandAliases[] = $aliasCommandName;
125
                    $command->setAliases($currentCommandAliases);
126
                }
127
            }
128
        }
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    private function hasConfigCommandAliases()
135
    {
136
        return isset($this->config['commands']['aliases']) && is_array($this->config['commands']['aliases']);
137
    }
138
139
    /**
140
     * @param Application $application
141
     */
142
    public function registerCustomCommands(Application $application)
143
    {
144
        if (isset($this->config['commands']['customCommands'])
145
            && is_array($this->config['commands']['customCommands'])
146
        ) {
147
            foreach ($this->config['commands']['customCommands'] as $commandClass) {
148
                if (is_array($commandClass)) { // Support for key => value (name -> class)
149
                    $resolvedCommandClass = current($commandClass);
150
                    /** @var Command $command */
151
                    $command = new $resolvedCommandClass();
152
                    $command->setName(key($commandClass));
153
                } else {
154
                    /** @var Command $command */
155
                    $command = new $commandClass();
156
                }
157
                $application->add($command);
158
159
                $output = $this->output;
160
                if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
161
                    $output->writeln(
162
                        '<debug>Added command </debug><comment>' . get_class($command) . '</comment>'
163
                    );
164
                }
165
            }
166
        }
167
    }
168
169
    /**
170
     * Adds autoloader prefixes from user's config
171
     *
172
     * @param ClassLoader $autoloader
173
     */
174
    public function registerCustomAutoloaders(ClassLoader $autoloader)
175
    {
176
        $output = $this->output;
177
178 View Code Duplication
        if (isset($this->config['autoloaders']) && is_array($this->config['autoloaders'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
            foreach ($this->config['autoloaders'] as $prefix => $path) {
180
                $autoloader->add($prefix, $path);
181
182
                if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
183
                    $output->writeln(
184
                        '<debug>Registrered PSR-2 autoloader </debug> <info>'
185
                        . $prefix . '</info> -> <comment>' . $path . '</comment>'
186
                    );
187
                }
188
            }
189
        }
190
191 View Code Duplication
        if (isset($this->config['autoloaders_psr4']) && is_array($this->config['autoloaders_psr4'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
            foreach ($this->config['autoloaders_psr4'] as $prefix => $path) {
193
                $autoloader->addPsr4($prefix, $path);
194
195
                if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
196
                    $output->writeln(
197
                        '<debug>Registrered PSR-4 autoloader </debug> <info>'
198
                        . $prefix . ' </info> -> <comment>' . $path . '</comment>'
199
                    );
200
                }
201
            }
202
        }
203
    }
204
205
    /**
206
     * @param array $config
207
     */
208
    public function setConfig(array $config)
209
    {
210
        $this->config = $config;
211
    }
212
213
    /**
214
     * @return array
215
     */
216
    public function getConfig()
217
    {
218
        return $this->config;
219
    }
220
221
    /**
222
     * @param ConfigurationLoader $configurationLoader
223
     */
224
    public function setConfigurationLoader(ConfigurationLoader $configurationLoader)
225
    {
226
        $this->loader = $configurationLoader;
227
    }
228
229
    /**
230
     * @return ConfigurationLoader
231
     */
232
    public function getLoader()
233
    {
234
        if (!$this->loader) {
235
            $this->loader = $this->createLoader($this->initConfig, $this->isPharMode, $this->output);
236
            $this->initConfig = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $initConfig.

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...
237
        }
238
239
        return $this->loader;
240
    }
241
242
    public function load()
243
    {
244
        $this->config = $this->getLoader()->toArray();
245
    }
246
247
    /**
248
     * @param bool $loadExternalConfig
249
     */
250
    public function loadPartialConfig($loadExternalConfig)
251
    {
252
        $loader              = $this->getLoader();
253
        $this->partialConfig = $loader->getPartialConfig($loadExternalConfig);
254
    }
255
256
    /**
257
     * Get names of sub-folders to be scanned during Magento detection
258
     * @return array
259
     */
260
    public function getDetectSubFolders()
261
    {
262
        if (isset($this->partialConfig['detect']['subFolders'])) {
263
            return $this->partialConfig['detect']['subFolders'];
264
        }
265
266
        return array();
267
    }
268
269
    /**
270
     * @param array $initConfig
271
     * @param bool $isPharMode
272
     * @param OutputInterface $output
273
     *
274
     * @return ConfigurationLoader
275
     */
276
    public function createLoader(array $initConfig, $isPharMode, OutputInterface $output)
277
    {
278
        $config = ArrayFunctions::mergeArrays($this->config, $initConfig);
279
280
        $loader = new ConfigurationLoader($config, $isPharMode, $output);
281
282
        return $loader;
283
    }
284
}
285