Completed
Push — 10.x ( 43e240...db539f )
by Tim
04:19
created

LibraryLoader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 35
c 5
b 0
f 0
dl 0
loc 142
ccs 0
cts 59
cp 0
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainer() 0 3 1
A __construct() 0 3 1
A getCustomConfigurationDir() 0 3 1
A getVendorDir() 0 3 1
A load() 0 43 6
A loadConfiguration() 0 31 5
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\Configuration\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\Configuration\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
            // try to load the vendor directory's auto loader, if available. Otherwise we assume
118
            // that the vendor directory uses an autoloader that has already been loaded, e. g. in
119
            // case of Magento which has app/code registered as vendor directory what we want to use
120
            if (file_exists($autoLoader = $additionalVendorDir->getVendorDir() . '/autoload.php')) {
121
                require $autoLoader;
122
            }
123
124
            // try to load the DI configuration for the configured extension libraries
125
            foreach ($additionalVendorDir->getLibraries() as $library) {
126
                if ($libraryDir = realpath(sprintf('%s/%s/%s', $configuration->getInstallationDir(), $additionalVendorDir->getVendorDir(), $library))) {
127
                    $this->loadConfiguration($customLoader, $magentoVersion, $libraryDir);
128
                } else {
129
                    throw new \Exception(sprintf('Can\'t find find library directory "%s"', $libraryDir));
130
                }
131
            }
132
        }
133
134
        // initialize the project specific configuration loader for the DI configuration
135
        $projectLoader = new XmlFileLoader($container, new FileLocator(getcwd()));
136
137
        // finally load the project specific custom library configuration which overwrites the default one
138
        $this->loadConfiguration($projectLoader, $magentoVersion, $customConfigurationDir);
139
    }
140
141
    /**
142
     * Loads the version specific Symfony DI configuration.
143
     *
144
     * @param \Symfony\Component\Config\Loader\LoaderInterface $loader         The Symfony DI loader instance
145
     * @param string                                           $magentoVersion The Magento Version to load the configuration for
146
     * @param string                                           $libraryDir     The library directory
147
     *
148
     * @return boolean TRUE if the configuration file has been loaded, else FALSE
149
     */
150
    protected function loadConfiguration(LoaderInterface $loader, $magentoVersion, $libraryDir)
151
    {
152
153
        // load the default Symfony Di configuration
154
        if (file_exists($diConfiguration = sprintf('%s/symfony/Resources/config/services.xml', $libraryDir))) {
155
            // load the DI configuration
156
            $loader->load($diConfiguration);
157
158
            // load the directories that equals the versions custom configuration files are available for
159
            $versions = glob(sprintf('%s/symfony/Resources/config/*', $libraryDir), GLOB_ONLYDIR);
160
161
            // sort the directories descending by their version
162
            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

162
            usort(/** @scrutinizer ignore-type */ $versions, 'version_compare');
Loading history...
163
            krsort($versions);
164
165
            // override DI configuration with version specifc data
166
            foreach ($versions as $version) {
167
                if (version_compare(basename($version), $magentoVersion, '<=')) {
168
                    if (file_exists($diConfiguration = sprintf('%s/services.xml', $version))) {
169
                        // load the version specific DI configuration
170
                        $loader->load($diConfiguration);
171
                    }
172
                }
173
            }
174
175
            // return TRUE if the configuration has been loaded
176
            return true;
177
        }
178
179
        // return FALSE if the configuration file has NOT been available
180
        return false;
181
    }
182
}
183