1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SplClassLoader implementation that implements the technical interoperability |
4
|
|
|
* standards for PHP 5.3 namespaces and class names. |
5
|
|
|
* |
6
|
|
|
* http://groups.google.com/group/php-standards/web/final-proposal |
7
|
|
|
* |
8
|
|
|
* // Example which loads classes for the Doctrine Common package in the |
9
|
|
|
* // Doctrine\Common namespace. |
10
|
|
|
* $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine'); |
11
|
|
|
* $classLoader->register(); |
12
|
|
|
* |
13
|
|
|
* @author Jonathan H. Wage <[email protected]> |
14
|
|
|
* @author Roman S. Borschel <[email protected]> |
15
|
|
|
* @author Matthew Weier O'Phinney <[email protected]> |
16
|
|
|
* @author Kris Wallsmith <[email protected]> |
17
|
|
|
* @author Fabien Potencier <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace VirtualFileSystem; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Loader to use with PEAR installation |
24
|
|
|
* |
25
|
|
|
* @package VirtualFileSystem |
26
|
|
|
*/ |
27
|
|
|
class Loader |
28
|
|
|
{ |
29
|
|
|
private $fileExtension = '.php'; |
30
|
|
|
private $namespace; |
31
|
|
|
private $includePath; |
32
|
|
|
private $namespaceSeparator = '\\'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Creates a new <tt>Loader</tt> that loads classes of the |
36
|
|
|
* specified namespace. |
37
|
|
|
* |
38
|
|
|
* @param string $namespace The namespace to use. |
39
|
|
|
* @param null $includePath |
40
|
|
|
*/ |
41
|
|
|
public function __construct($namespace = 'VirtualFileSystem', $includePath = null) |
42
|
|
|
{ |
43
|
|
|
$this->namespace = $namespace; |
44
|
|
|
$this->includePath = $includePath; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Sets the namespace separator used by classes in the namespace of this class loader. |
49
|
|
|
* |
50
|
|
|
* @param string $sep The separator to use. |
51
|
|
|
*/ |
52
|
|
|
public function setNamespaceSeparator($sep) |
53
|
|
|
{ |
54
|
|
|
$this->namespaceSeparator = $sep; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Gets the namespace seperator used by classes in the namespace of this class loader. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getNamespaceSeparator() |
63
|
|
|
{ |
64
|
|
|
return $this->namespaceSeparator; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sets the base include path for all class files in the namespace of this class loader. |
69
|
|
|
* |
70
|
|
|
* @param string $includePath |
71
|
|
|
*/ |
72
|
|
|
public function setIncludePath($includePath) |
73
|
|
|
{ |
74
|
|
|
$this->includePath = $includePath; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Gets the base include path for all class files in the namespace of this class loader. |
79
|
|
|
* |
80
|
|
|
* @return null|string $includePath |
81
|
|
|
*/ |
82
|
|
|
public function getIncludePath() |
83
|
|
|
{ |
84
|
|
|
return $this->includePath; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sets the file extension of class files in the namespace of this class loader. |
89
|
|
|
* |
90
|
|
|
* @param string $fileExtension |
91
|
|
|
*/ |
92
|
|
|
public function setFileExtension($fileExtension) |
93
|
|
|
{ |
94
|
|
|
$this->fileExtension = $fileExtension; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Gets the file extension of class files in the namespace of this class loader. |
99
|
|
|
* |
100
|
|
|
* @return string $fileExtension |
101
|
|
|
*/ |
102
|
|
|
public function getFileExtension() |
103
|
|
|
{ |
104
|
|
|
return $this->fileExtension; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Installs this class loader on the SPL autoload stack. |
109
|
|
|
* |
110
|
|
|
* @param bool $prepend If true, prepend autoloader on the autoload stack |
111
|
|
|
*/ |
112
|
|
|
public function register($prepend = false) |
113
|
|
|
{ |
114
|
|
|
spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Uninstalls this class loader from the SPL autoloader stack. |
119
|
|
|
*/ |
120
|
|
|
public function unregister() |
121
|
|
|
{ |
122
|
|
|
spl_autoload_unregister(array($this, 'loadClass')); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Loads the given class or interface. |
127
|
|
|
* |
128
|
|
|
* @param string $className The name of the class to load. |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function loadClass($className) |
132
|
|
|
{ |
133
|
|
|
if (null === $this->namespace |
134
|
|
|
|| $this->namespace.$this->namespaceSeparator === substr( |
135
|
|
|
$className, |
136
|
|
|
0, |
137
|
|
|
strlen($this->namespace.$this->namespaceSeparator) |
138
|
|
|
)) { |
139
|
|
|
$fileName = ''; |
140
|
|
|
if (false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) { |
141
|
|
|
$namespace = substr($className, 0, $lastNsPos); |
142
|
|
|
$className = substr($className, $lastNsPos + 1); |
143
|
|
|
$fileName = |
144
|
|
|
str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . |
145
|
|
|
DIRECTORY_SEPARATOR; |
146
|
|
|
} |
147
|
|
|
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->fileExtension; |
148
|
|
|
require $this->getFullPath($fileName); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|