Completed
Push — master ( 01a201...5c6880 )
by Tim
02:19
created

LibraryLoader::load()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 22
c 2
b 0
f 0
nc 10
nop 1
dl 0
loc 39
rs 8.9457
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
     * @throws \Exception Is thrown, if none of the possible vendor directories can be found
74
     */
75
    protected function getVendorDir()
76
    {
77
        return $this->getContainer()->getParameter(DependencyInjectionKeys::CONFIGURATION_VENDOR_DIR);
78
    }
79
80
    /**
81
     * Load's the external libraries registered in the passed configuration.
82
     *
83
     * @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance
84
     *
85
     * @return void
86
     */
87
    public function load(ConfigurationInterface $configuration)
88
    {
89
90
        // load the Magento Version from the configuration
91
        $magentoVersion = $configuration->getMagentoVersion();
92
93
        // initialize the default loader and load the DI configuration for the this library
94
        $defaultLoader = new XmlFileLoader($this->getContainer(), new FileLocator($vendorDir = $this->getVendorDir()));
95
96
        // load the DI configuration for all the extension libraries
97
        foreach ($configuration->getExtensionLibraries() as $library) {
98
            $this->loadConfiguration($defaultLoader, $magentoVersion, sprintf('%s/%s', $vendorDir, $library));
99
        }
100
101
        // register autoloaders for additional vendor directories
102
        $customLoader = new XmlFileLoader($this->getContainer(), new FileLocator());
103
        foreach ($configuration->getAdditionalVendorDirs() as $additionalVendorDir) {
104
            // load the vendor directory's auto loader
105
            if (file_exists($autoLoader = $additionalVendorDir->getVendorDir() . '/autoload.php')) {
106
                require $autoLoader;
107
            } else {
108
                throw new \Exception(
109
                    sprintf(
110
                        'Can\'t find autoloader in configured additional vendor directory "%s"',
111
                        $additionalVendorDir->getVendorDir()
112
                    )
113
                );
114
            }
115
116
            // try to load the DI configuration for the configured extension libraries
117
            foreach ($additionalVendorDir->getLibraries() as $library) {
118
                if ($libraryDir = realpath(sprintf('%s/%s', $additionalVendorDir->getVendorDir(), $library))) {
119
                    $this->loadConfiguration($customLoader, $magentoVersion, $libraryDir);
120
                } else {
121
                    throw new \Exception(
122
                        sprintf(
123
                            'Can\'t find find library directory "%s/%s"',
124
                            $additionalVendorDir->getVendorDir(),
125
                            $library
126
                        )
127
                    );
128
                }
129
            }
130
        }
131
    }
132
133
    /**
134
     * Loads the version specific Symfony DI configuration.
135
     *
136
     * @param \Symfony\Component\Config\Loader\LoaderInterface $loader         The Symfony DI loader instance
137
     * @param string                                           $magentoVersion The Magento Version to load the configuration for
138
     * @param string                                           $libraryDir     The library directory
139
     *
140
     * @return void
141
     */
142
    protected function loadConfiguration(LoaderInterface $loader, $magentoVersion, $libraryDir)
143
    {
144
145
        // load the default Symfony Di configuration
146
        if (file_exists($diConfiguration = sprintf('%s/symfony/Resources/config/services.xml', $libraryDir))) {
147
            // load the DI configuration
148
            $loader->load($diConfiguration);
149
        } else {
150
            throw new \Exception(
151
                sprintf(
152
                    'Can\'t load default DI configuration "%s"',
153
                    $diConfiguration
154
                )
155
            );
156
        }
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
}
176