Completed
Push — 8.x ( f3eaec...2ddcfa )
by Tim
07:47
created

LibraryLoader::getCustomConfigurationDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Configuration\LibraryLoader
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-cli-simple
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Cli\Configuration;
22
23
use Symfony\Component\Config\FileLocator;
24
use Symfony\Component\Config\Loader\LoaderInterface;
25
use Symfony\Component\DependencyInjection\ContainerInterface;
26
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
27
use TechDivision\Import\ConfigurationInterface;
28
use TechDivision\Import\Cli\Utils\DependencyInjectionKeys;
29
30
/**
31
 * The library loader implementation.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-cli-simple
37
 * @link      http://www.techdivision.com
38
 */
39
class LibraryLoader
40
{
41
42
    /**
43
     * The container instance.
44
     *
45
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
46
     */
47
    protected $container;
48
49
    /**
50
     * Initializes the configuration loader.
51
     *
52
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The container instance
53
     */
54
    public function __construct(ContainerInterface $container)
55
    {
56
        $this->container = $container;
57
    }
58
59
    /**
60
     * Return's the DI container instance.
61
     *
62
     * @return \Symfony\Component\DependencyInjection\ContainerInterface The DI container instance
63
     */
64
    protected function getContainer()
65
    {
66
        return $this->container;
67
    }
68
69
    /**
70
     * Return's the absolute path to the actual vendor directory.
71
     *
72
     * @return string The absolute path to the actual vendor directory
73
     */
74
    protected function getVendorDir()
75
    {
76
        return $this->getContainer()->getParameter(DependencyInjectionKeys::CONFIGURATION_VENDOR_DIR);
77
    }
78
79
    /**
80
     * Return's the relative path to the custom configuration directory.
81
     *
82
     * @return string The relative path to the custom configuration directory
83
     */
84
    protected function getCustomConfigurationDir()
85
    {
86
        return $this->getContainer()->getParameter(DependencyInjectionKeys::APPLICATION_CUSTOM_CONFIGURATION_DIR);
87
    }
88
89
    /**
90
     * Load's the external libraries registered in the passed configuration.
91
     *
92
     * @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance
93
     *
94
     * @return void
95
     */
96
    public function load(ConfigurationInterface $configuration)
97
    {
98
99
        // load the DI container, vendor and custom configuration
100
        // directory as well as the Magento version
101
        $container = $this->getContainer();
102
        $vendorDir = $this->getVendorDir();
103
        $magentoVersion = $configuration->getMagentoVersion();
104
        $customConfigurationDir = $this->getCustomConfigurationDir();
105
106
        // initialize the default loader and load the DI configuration for the this library
107
        $defaultLoader = new XmlFileLoader($container, new FileLocator($vendorDir));
108
109
        // load the DI configuration for all the extension libraries
110
        foreach ($configuration->getExtensionLibraries() as $library) {
111
            $this->loadConfiguration($defaultLoader, $magentoVersion, sprintf('%s/%s', $vendorDir, $library));
112
        }
113
114
        // register autoloaders for additional vendor directories
115
        $customLoader = new XmlFileLoader($container, new FileLocator());
116
        foreach ($configuration->getAdditionalVendorDirs() as $additionalVendorDir) {
117
            // load the vendor directory's auto loader
118
            if (file_exists($autoLoader = $additionalVendorDir->getVendorDir() . '/autoload.php')) {
119
                require $autoLoader;
120
            } else {
121
                throw new \Exception(
122
                    sprintf(
123
                        'Can\'t find autoloader in configured additional vendor directory "%s"',
124
                        $additionalVendorDir->getVendorDir()
125
                    )
126
                );
127
            }
128
129
            // try to load the DI configuration for the configured extension libraries
130
            foreach ($additionalVendorDir->getLibraries() as $library) {
131
                if ($libraryDir = realpath(sprintf('%s/%s', $additionalVendorDir->getVendorDir(), $library))) {
132
                    $this->loadConfiguration($customLoader, $magentoVersion, $libraryDir);
133
                } else {
134
                    throw new \Exception(
135
                        sprintf(
136
                            'Can\'t find find library directory "%s/%s"',
137
                            $additionalVendorDir->getVendorDir(),
138
                            $library
139
                        )
140
                    );
141
                }
142
            }
143
        }
144
145
        // initialize the project specific configuration loader for the DI configuration
146
        $projectLoader = new XmlFileLoader($container, new FileLocator(getcwd()));
147
148
        // finally load the project specific custom library configuration which overwrites the default one
149
        $this->loadConfiguration($projectLoader, $magentoVersion, $customConfigurationDir);
150
    }
151
152
    /**
153
     * Loads the version specific Symfony DI configuration.
154
     *
155
     * @param \Symfony\Component\Config\Loader\LoaderInterface $loader         The Symfony DI loader instance
156
     * @param string                                           $magentoVersion The Magento Version to load the configuration for
157
     * @param string                                           $libraryDir     The library directory
158
     *
159
     * @return boolean TRUE if the configuration file has been loaded, else FALSE
160
     */
161
    protected function loadConfiguration(LoaderInterface $loader, $magentoVersion, $libraryDir)
162
    {
163
164
        // load the default Symfony Di configuration
165
        if (file_exists($diConfiguration = sprintf('%s/symfony/Resources/config/services.xml', $libraryDir))) {
166
            // load the DI configuration
167
            $loader->load($diConfiguration);
168
169
            // load the directories that equals the versions custom configuration files are available for
170
            $versions = glob(sprintf('%s/symfony/Resources/config/*', $libraryDir), GLOB_ONLYDIR);
171
172
            // sort the directories descending by their version
173
            usort($versions, 'version_compare');
0 ignored issues
show
Bug introduced by
It seems like $versions can also be of type false; however, parameter $array of usort() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

173
            usort(/** @scrutinizer ignore-type */ $versions, 'version_compare');
Loading history...
174
            krsort($versions);
175
176
            // override DI configuration with version specifc data
177
            foreach ($versions as $version) {
178
                if (version_compare(basename($version), $magentoVersion, '<=')) {
179
                    if (file_exists($diConfiguration = sprintf('%s/services.xml', $version))) {
180
                        // load the version specific DI configuration
181
                        $loader->load($diConfiguration);
182
                    }
183
                }
184
            }
185
186
            // return TRUE if the configuration has been loaded
187
            return true;
188
        }
189
190
        // return FALSE if the configuration file has NOT been available
191
        return false;
192
    }
193
}
194