Autoloader   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 26
lcom 2
cbo 2
dl 0
loc 187
ccs 69
cts 69
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A register() 0 8 2
A unregister() 0 8 2
A registerNamespace() 0 12 2
A isNamespaceRegistered() 0 4 1
A getRegisteredNamespaces() 0 3 1
A load() 0 9 3
A validateNamespace() 0 5 2
A validateNamespacePath() 0 10 3
A getNamespaceClassPath() 0 13 4
A createClassPath() 0 6 3
A getTranslatedClassPath() 0 4 1
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Common;
26
27
use Brickoo\Component\Common\Exception\DirectoryDoesNotExistException;
28
use Brickoo\Component\Common\Exception\DuplicateNamespaceRegistrationException;
29
30
/**
31
 * Autoloader
32
 *
33
 * Implementation of a namespace based autoloader.
34
 * @author Celestino Diaz <[email protected]>
35
 */
36
class Autoloader {
37
38
    /** @var boolean */
39
    private $isRegistered;
40
41
    /** @var boolean */
42
    private $prependAutoloader;
43
44
    /** @var array */
45
    private $namespaces;
46
47
    /**
48
     * Class constructor.
49
     * @param array $namespaces the namespaces to register as namespace => path structure.
50
     * @param boolean $prepend flag to prepend or append to the PHP autoloader list
51
     * @throws \InvalidArgumentException if an argument is not valid
52
     * @throws \Brickoo\Component\Common\Exception\DirectoryDoesNotExistException
53
     * @throws \Brickoo\Component\Common\Exception\DuplicateNamespaceRegistrationException
54
     */
55 1
    public function __construct(array $namespaces = [], $prepend = true) {
56 1
        $this->isRegistered = false;
57 1
        $this->prependAutoloader = (boolean)$prepend;
58 1
        $this->namespaces = [];
59
60 1
        include_once "Exception.php";
61 1
        foreach ($namespaces as $namespace => $includePath) {
62 1
            $this->registerNamespace($namespace, $includePath);
63 1
        }
64 1
    }
65
66
    /**
67
     * Register the autoloader.
68
     * @return \Brickoo\Component\Common\Autoloader
69
     */
70 1
    public function register() {
71 1
        if (!$this->isRegistered) {
72 1
            spl_autoload_register([$this, "load"], true, $this->prependAutoloader);
73 1
            $this->isRegistered = true;
74 1
        }
75
76 1
        return $this;
77
    }
78
79
    /**
80
     * Unregister the autoloader.
81
     * @return \Brickoo\Component\Common\Autoloader
82
     */
83 1
    public function unregister() {
84 1
        if ($this->isRegistered) {
85 1
            spl_autoload_unregister([$this, "load"]);
86 1
            $this->isRegistered = false;
87 1
        }
88
89 1
        return $this;
90
    }
91
92
    /**
93
     * Register the namespace to the available namespaces.
94
     * @param string $namespace the namespace to register
95
     * @param string $namespacePath the absolute path to the namespace
96
     * @throws Exception\DirectoryDoesNotExistException
97
     * @throws Exception\DuplicateNamespaceRegistrationException
98
     * @throws \InvalidArgumentException if an argument is not valid
99
     * @return \Brickoo\Component\Common\Autoloader
100
     */
101 5
    public function registerNamespace($namespace, $namespacePath) {
102 5
        $this->validateNamespace($namespace);
103 4
        $this->validateNamespacePath($namespacePath);
104
105 2
        if ($this->isNamespaceRegistered($namespace)) {
106 1
            require_once "Exception".DIRECTORY_SEPARATOR."DuplicateNamespaceRegistrationException.php";
107 1
            throw new DuplicateNamespaceRegistrationException($namespace);
108
        }
109
110 2
        $this->namespaces[$namespace] = rtrim($namespacePath, "/\\");
111 2
        return $this;
112
    }
113
114
    /**
115
     * Check if the given namespace has been registered.
116
     * @param string $namespace the namespace to check
117
     * @throws \InvalidArgumentException if an argument is not valid
118
     * @return boolean check result
119
     */
120 2
    public function isNamespaceRegistered($namespace) {
121 2
        $this->validateNamespace($namespace);
122 1
        return array_key_exists($namespace, $this->namespaces);
123
    }
124
125
    /**
126
     * Return the registered namespaces.
127
     * @return array the registered namespaces
128
     */
129 1
    public function getRegisteredNamespaces() {
130 1
        return $this->namespaces;
131
    }
132
133
    /**
134
     * Load the requested class.
135
     * Commonly this is the auto loader callback function registered.
136
     * @param string $className the class to load
137
     * @return boolean true on success false on failure
138
     */
139 5
    public function load($className) {
140 5
        if (($namespaceClassPath = $this->getNamespaceClassPath($className)) === null
141 5
            || (!file_exists($namespaceClassPath))) {
142 2
                return false;
143
        }
144
145 3
        include $namespaceClassPath;
146 3
        return true;
147
    }
148
149
    /**
150
     * Validate the namespace.
151
     * @param string $namespace
152
     * @throws \InvalidArgumentException
153
     * @return void
154
     */
155 2
    private function validateNamespace($namespace) {
156 2
        if (!is_string($namespace)) {
157 1
            throw new \InvalidArgumentException("Invalid namespace argument used.");
158
        }
159 1
    }
160
161
    /**
162
     * Validate the namespace path.
163
     * @param string $namespacePath
164
     * @throws \Brickoo\Component\Common\Exception\DirectoryDoesNotExistException
165
     * @throws \InvalidArgumentException
166
     * @return void
167
     */
168 3
    private function validateNamespacePath($namespacePath) {
169 3
        if (!is_string($namespacePath)) {
170 1
            throw new \InvalidArgumentException("Invalid namespace path argument used.");
171
        }
172
173 2
        if (!is_dir($namespacePath)) {
174 1
            require_once "Exception".DIRECTORY_SEPARATOR."DirectoryDoesNotExistException.php";
175 1
            throw new DirectoryDoesNotExistException($namespacePath);
176
        }
177 1
    }
178
179
    /**
180
     * Returns the path for a namespace class.
181
     * @param string $className
182
     * @return string|null the namespace based path otherwise null
183
     */
184 3
    private function getNamespaceClassPath($className) {
185 3
        $chosenPath = "";
186 3
        $chosenNamespace = "";
187
188 3
        foreach ($this->namespaces as $namespace => $path) {
189 2
            if ((strpos($className, $namespace) === 0)
190 2
                && strlen($chosenNamespace) < strlen($namespace)) {
191 2
                    $chosenNamespace = $namespace;
192 2
                    $chosenPath = $path;
193 2
            }
194 3
        }
195 3
        return $this->createClassPath($className, $chosenNamespace, $chosenPath);
196
    }
197
198
    /**
199
     * Create the class filesystem loader path.
200
     * @param string $className
201
     * @param string $chosenNamespace
202
     * @param string $chosenPath
203
     * @return null|string the class path otherwise null
204
     */
205 3
    private function createClassPath($className, $chosenNamespace, $chosenPath) {
206 3
        if (empty($chosenNamespace) || empty($chosenPath)) {
207 1
            return null;
208
        }
209 2
        return $chosenPath.$this->getTranslatedClassPath(substr($className, strlen($chosenNamespace)));
210
    }
211
212
    /**
213
     * Returns a translated namespace class to filesystem path.
214
     * @param string $className class including namespace
215
     * @return string the translated class path
216
     */
217 3
    private function getTranslatedClassPath($className) {
218 3
        $translatedClassName = str_replace("_", DIRECTORY_SEPARATOR, $className);
219 3
        return DIRECTORY_SEPARATOR.str_replace("\\", DIRECTORY_SEPARATOR, $translatedClassName).".php";
220
    }
221
222
}
223