MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
ClassLoader.php
Ir para a documentação deste ficheiro.
1<?php
2/*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the LGPL. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\Common;
21
35{
36 private $fileExtension = '.php';
37 private $namespace;
38 private $includePath;
39 private $namespaceSeparator = '\\';
40
52 public function __construct($ns = null, $includePath = null)
53 {
54 $this->namespace = $ns;
55 $this->includePath = $includePath;
56 }
57
63 public function setNamespaceSeparator($sep)
64 {
65 $this->namespaceSeparator = $sep;
66 }
67
73 public function getNamespaceSeparator()
74 {
75 return $this->namespaceSeparator;
76 }
77
83 public function setIncludePath($includePath)
84 {
85 $this->includePath = $includePath;
86 }
87
93 public function getIncludePath()
94 {
95 return $this->includePath;
96 }
97
103 public function setFileExtension($fileExtension)
104 {
105 $this->fileExtension = $fileExtension;
106 }
107
113 public function getFileExtension()
114 {
115 return $this->fileExtension;
116 }
117
121 public function register()
122 {
123 spl_autoload_register(array($this, 'loadClass'));
124 }
125
129 public function unregister()
130 {
131 spl_autoload_unregister(array($this, 'loadClass'));
132 }
133
140 public function loadClass($className)
141 {
142 if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) {
143 return false;
144 }
145
146 require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '')
147 . str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className)
148 . $this->fileExtension;
149
150 return true;
151 }
152
160 public function canLoadClass($className)
161 {
162 if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) {
163 return false;
164 }
165 return file_exists(($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '')
166 . str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className)
167 . $this->fileExtension);
168 }
169
191 public static function classExists($className)
192 {
193 if (class_exists($className, false)) {
194 return true;
195 }
196
197 foreach (spl_autoload_functions() as $loader) {
198 if (is_array($loader)) { // array(???, ???)
199 if (is_object($loader[0])) {
200 if ($loader[0] instanceof ClassLoader) { // array($obj, 'methodName')
201 if ($loader[0]->canLoadClass($className)) {
202 return true;
203 }
204 } else if ($loader[0]->{$loader[1]}($className)) {
205 return true;
206 }
207 } else if ($loader[0]::$loader[1]($className)) { // array('ClassName', 'methodName')
208 return true;
209 }
210 } else if ($loader instanceof \Closure) { // function($className) {..}
211 if ($loader($className)) {
212 return true;
213 }
214 } else if (is_string($loader) && $loader($className)) { // "MyClass::loadClass"
215 return true;
216 }
217 }
218
219 return false;
220 }
221
229 public static function getClassLoader($className)
230 {
231 foreach (spl_autoload_functions() as $loader) {
232 if (is_array($loader) && $loader[0] instanceof ClassLoader &&
233 $loader[0]->canLoadClass($className)) {
234 return $loader[0];
235 }
236 }
237
238 return null;
239 }
240}
setIncludePath($includePath)
Definição ClassLoader.php:83
static getClassLoader($className)
__construct($ns=null, $includePath=null)
Definição ClassLoader.php:52
setFileExtension($fileExtension)
static classExists($className)