Completed
Push — develop ( f3a4fe...df0b60 )
by Tom
03:55
created

AutoloaderDecorator::mirrorAutoloader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 1999-2017 netz98 GmbH (http://www.netz98.de)
4
 *
5
 * @see PROJECT_LICENSE.txt
6
 */
7
8
namespace N98\Magento\Application;
9
10
use Composer\Autoload\ClassLoader;
11
use Magento\Framework\Autoload\AutoloaderInterface;
12
13
class AutoloaderDecorator implements AutoloaderInterface
14
{
15
    /**
16
     * @var \Composer\Autoload\ClassLoader
17
     */
18
    private $n98MagerunAutoloader;
19
20
    /**
21
     * @var \Magento\Framework\Autoload\AutoloaderInterface
22
     */
23
    private $magentoAutoloader;
24
25
    /**
26
     * TestAutoloader constructor.
27
     * @param \Composer\Autoload\ClassLoader $n98MagerunAutoloader
28
     */
29
    public function __construct(AutoloaderInterface $magentoAutoloader, ClassLoader $n98MagerunAutoloader)
30
    {
31
        $this->n98MagerunAutoloader = $n98MagerunAutoloader;
32
        $this->magentoAutoloader = $magentoAutoloader;
33
34
        $this->mirrorAutoloader($n98MagerunAutoloader);
35
    }
36
37
    /**
38
     * Attempts to load a class and returns true if successful.
39
     *
40
     * @param string $className
41
     * @return bool
42
     */
43
    public function loadClass($className)
44
    {
45
        if ($this->n98MagerunAutoloader->loadClass($className)) {
46
            return true;
47
        }
48
49
        return $this->magentoAutoloader->loadClass($className);
50
    }
51
52
    /**
53
     * Adds a PSR-4 mapping from a namespace prefix to directories to search in for the corresponding class
54
     *
55
     * @param string $nsPrefix The namespace prefix of the PSR-4 mapping
56
     * @param string|array $paths The path or paths to look in for the given prefix
57
     * @param bool $prepend Whether to append the given path or paths to the paths already associated with the prefix
58
     * @return void
59
     */
60
    public function addPsr4($nsPrefix, $paths, $prepend = false)
61
    {
62
        $this->magentoAutoloader->addPsr4($nsPrefix, $paths, $prepend);
63
    }
64
65
    /**
66
     * Adds a PSR-0 mapping from a namespace prefix to directories to search in for the corresponding class
67
     *
68
     * @param string $nsPrefix The namespace prefix of the PSR-0 mapping
69
     * @param string|array $paths The path or paths to look in for the given prefix
70
     * @param bool $prepend Whether to append the given path or paths to the paths already associated with the prefix
71
     * @return void
72
     */
73
    public function addPsr0($nsPrefix, $paths, $prepend = false)
74
    {
75
        $this->magentoAutoloader->addPsr0($nsPrefix, $paths, $prepend);
76
    }
77
78
    /**
79
     * Creates new PSR-0 mappings from the given prefix to the given set of paths, eliminating previous mappings
80
     *
81
     * @param string $nsPrefix The namespace prefix of the PSR-0 mapping
82
     * @param string|array $paths The path or paths to look in for the given prefix
83
     * @return void
84
     */
85
    public function setPsr0($nsPrefix, $paths)
86
    {
87
        $this->magentoAutoloader->setPsr0($nsPrefix, $paths);
88
    }
89
90
    /**
91
     * Creates new PSR-4 mappings from the given prefix to the given set of paths, eliminating previous mappings
92
     *
93
     * @param string $nsPrefix The namespace prefix of the PSR-0 mapping
94
     * @param string|array $paths The path or paths to look in for the given prefix
95
     * @return void
96
     */
97
    public function setPsr4($nsPrefix, $paths)
98
    {
99
        $this->magentoAutoloader->setPsr4($nsPrefix, $paths);
100
    }
101
102
    /**
103
     * Get filepath of class on system or false if it does not exist
104
     *
105
     * @param string $className
106
     * @return string|bool
107
     */
108
    public function findFile($className)
109
    {
110
        $filename = $this->n98MagerunAutoloader->findFile($className);
111
112
        if ($filename !== false) {
113
            return $filename;
114
        }
115
116
        // Fallback to original Magento autoloader
117
118
        return $this->magentoAutoloader->findFile($className);
119
    }
120
121
    /**
122
     * @param \Composer\Autoload\ClassLoader $n98MagerunAutoloader
123
     */
124
    private function mirrorAutoloader(ClassLoader $n98MagerunAutoloader)
125
    {
126
        $this->mirrorPsr0Pathes($n98MagerunAutoloader);
127
        $this->mirrorPsr4Pathes($n98MagerunAutoloader);
128
        $this->mirrorClassMaps($n98MagerunAutoloader);
129
    }
130
131
    /**
132
     * @param \Composer\Autoload\ClassLoader $n98MagerunAutoloader
133
     * @return mixed
134
     */
135
    private function mirrorPsr4Pathes(ClassLoader $n98MagerunAutoloader)
136
    {
137
        foreach ($n98MagerunAutoloader->getPrefixesPsr4() as $prefixPsr4 => $pathes) {
138
            $this->magentoAutoloader->addPsr4($prefixPsr4, $pathes, true);
139
        }
140
    }
141
142
    /**
143
     * @param \Composer\Autoload\ClassLoader $n98MagerunAutoloader
144
     */
145
    private function mirrorPsr0Pathes(ClassLoader $n98MagerunAutoloader)
146
    {
147
        foreach ($n98MagerunAutoloader->getPrefixes() as $prefixPsr0 => $pathes) {
148
            $this->magentoAutoloader->addPsr0($prefixPsr0, $pathes, true);
149
        }
150
    }
151
152
    /**
153
     * Mirror class maps from one autoloader to another
154
     *
155
     * We make use of reflection, because the AutloaderInterface does not contain
156
     * a method for class maps.
157
     *
158
     * @param \Composer\Autoload\ClassLoader $n98MagerunAutoloader
159
     */
160
    private function mirrorClassMaps(ClassLoader $n98MagerunAutoloader)
161
    {
162
        try {
163
            $autoloaderReflection = new \ReflectionObject($this->magentoAutoloader);
164
            $autoloaderProperty = $autoloaderReflection->getProperty('autoloader');
165
            $autoloaderProperty->setAccessible(true);
166
167
            /** @var ClassLoader $magentoComposerAutoloader */
168
            $magentoComposerAutoloader = $autoloaderProperty->getValue($this->magentoAutoloader);
169
            $magentoComposerAutoloader->addClassMap($n98MagerunAutoloader->getClassMap());
170
        } catch (\Exception $e) {
171
            // ignore not existing autoloader
172
        }
173
    }
174
}
175