ConfigurationLoader::fromEnv()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 11
nc 3
nop 2
crap 12
1
<?php
2
/**
3
 * For licensing information, please see the LICENSE file accompanied with this file.
4
 *
5
 * @author Gerard van Helden <[email protected]>
6
 * @copyright 2012 Gerard van Helden <http://melp.nl>
7
 */
8
namespace Zicht\Tool\Configuration;
9
10
use Symfony\Component\Config\FileLocatorInterface;
11
use Symfony\Component\Config\Definition\Processor;
12
use Zicht\Tool\Debug;
13
use Zicht\Version\Version;
14
15
16
/**
17
 * Z-file configuration loader.
18
 */
19
class ConfigurationLoader
20
{
21
    /**
22
     * Create the configuration loader based the current shell environment variables.
23
     *
24
     * @param string $configFilename
25
     * @param Version $version
26
     * @return ConfigurationLoader
27
     * @codeCoverageIgnore
28
     */
29
    public static function fromEnv($configFilename, Version $version)
30
    {
31
        if (null === $configFilename) {
32
            $configFilename = getenv('ZFILE') ? getenv('ZFILE') : 'z.yml';
33
        }
34
35
        return new self(
36
            $configFilename,
37
            new PathDefaultFileLocator('ZPATH', array(getcwd(), getenv('HOME') . '/.config/z')),
38
            new FileLoader(
39
                new PathDefaultFileLocator(
40
                    'ZPLUGINPATH',
41
                    array(ZPREFIX . '/vendor/zicht/z-plugins/', getcwd())
42
                ),
43
                $version
44
            )
45
        );
46
    }
47
48
    protected $sourceFiles = array();
49
    protected $plugins = array();
50
    protected $configFilename;
51
    protected $configLocator;
52
    protected $loader;
53
54
55
56
    /**
57
     * Construct the loader.
58
     *
59
     * @param string $configFilename
60
     * @param \Symfony\Component\Config\FileLocatorInterface $configLocator
61
     * @param FileLoader $loader
62
     */
63 5
    public function __construct($configFilename, FileLocatorInterface $configLocator, FileLoader $loader)
64
    {
65 5
        $this->configFilename = $configFilename;
66 5
        $this->configLocator = $configLocator;
67 5
        $this->loader = $loader;
68 5
    }
69
70
    /**
71
     * Add a plugin on-the-fly
72
     *
73
     * @param string $name
74
     * @return void
75
     */
76
    public function addPlugin($name)
77
    {
78
        $this->loader->addPlugin($name, getcwd());
79
    }
80
81
82
    /**
83
     * Processes the configuration contents
84
     *
85
     * @return array
86
     *
87
     * @throws \UnexpectedValueException
88
     */
89 4
    public function processConfiguration()
90
    {
91 4
        Debug::enterScope('config');
92 4
        Debug::enterScope('load');
93
        try {
94 4
            $zfiles = (array)$this->configLocator->locate($this->configFilename, null, false);
95 4
        } catch (\InvalidArgumentException $e) {
96 1
            $zfiles = array();
97
        }
98 4 View Code Duplication
        foreach ($zfiles as $file) {
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...
99 3
            Debug::enterScope($file);
100 3
            $this->sourceFiles[] = $file;
101 3
            $this->loader->load($file);
102 3
            Debug::exitScope($file);
103 4
        }
104 4 View Code Duplication
        foreach ($this->loader->getPlugins() as $name => $file) {
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...
105 2
            Debug::enterScope($file);
106 2
            $this->sourceFiles[] = $file;
107 2
            $this->loadPlugin($name, $file);
108 1
            Debug::exitScope($file);
109 3
        }
110 3
        Debug::exitScope('load');
111
112 3
        Debug::enterScope('process');
113 3
        $processor = new Processor();
114 3
        $ret = $processor->processConfiguration(
115 3
            new Configuration($this->plugins),
116 3
            $this->loader->getConfigs()
117 3
        );
118 3
        Debug::exitScope('process');
119 3
        Debug::exitScope('config');
120 3
        return $ret;
121
    }
122
123
124
    /**
125
     * Load the specified plugin instance.
126
     *
127
     * @param string $name
128
     * @param string $file
129
     * @return void
130
     *
131
     * @throws \UnexpectedValueException
132
     */
133 2
    protected function loadPlugin($name, $file)
134
    {
135 2
        require_once $file;
136 2
        $className = sprintf('Zicht\Tool\Plugin\%s\Plugin', ucfirst(basename($name)));
137 2
        $class     = new \ReflectionClass($className);
138 2
        if (!$class->implementsInterface('Zicht\Tool\PluginInterface')) {
139 1
            throw new \UnexpectedValueException("The class $className is not a 'Zicht\\Tool\\PluginInterface'");
140
        }
141 1
        $this->plugins[$name] = $class->newInstance();
142 1
    }
143
144
145
    /**
146
     * Returns all plugins registered while loading.
147
     *
148
     * @return array
149
     */
150 1
    public function getPlugins()
151
    {
152 1
        return $this->plugins;
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function getSourceFiles()
159
    {
160
        return $this->loader->getSourceFiles();
161
    }
162
}
163