1: <?php
2:
3: namespace Xoops\Core;
4:
5: /**
6: * SplClassLoader implementation that implements the technical interoperability
7: * standards for PHP 5.3 namespaces and class names.
8: *
9: * From the PHP Framework Interop Group
10: *
11: * Examples
12: *
13: * @code
14: * $myLibLoader = new SplClassLoader('mylib', '/path/to/mylib/src');
15: * $myLibLoader->register();
16: *
17: * $zendLoader = new SplClassLoader('Zend', '/path/to/zend/lib');
18: * $zendLoader->setNamespaceSeparator('_');
19: * $zendLoader->register();
20: * @endcode
21: *
22: * @category Xoops\Core\Psr0ClassLoader
23: * @package Xoops
24: * @author Jonathan H. Wage <jonwage@gmail.com>
25: * @author Roman S. Borschel <roman@code-factory.org>
26: * @author Matthew Weier O'Phinney <matthew@zend.com>
27: * @author Kris Wallsmith <kris.wallsmith@gmail.com>
28: * @author Fabien Potencier <fabien.potencier@symfony-project.org>
29: * @author Richard Griffith <richard@geekwright.com>
30: * @link https://gist.github.com/jwage/221634
31: * @see http://www.php-fig.org/
32: */
33: class Psr0ClassLoader
34: {
35: private $fileExtension = '.php';
36: private $namespace;
37: private $includePath;
38: private $namespaceSeparator = '\\';
39:
40: /**
41: * Creates a new SplClassLoader that loads classes of the
42: * specified namespace.
43: *
44: * @param string $ns The namespace to use.
45: * @param string $includePath Path to the namespaces top directory
46: *
47: * @return void
48: */
49: public function __construct($ns = null, $includePath = null)
50: {
51: $this->namespace = $ns;
52: $this->includePath = $includePath;
53: }
54:
55: /**
56: * addLoader sets all basic options and registers the autoloader
57: *
58: * @param type $namespace namespace
59: * @param type $path path to the namespace's top directory
60: * @param type $separator namespace separator
61: * @param type $extension file extension
62: *
63: * @return SplClassLoader
64: */
65: public static function addLoader($namespace, $path, $separator = '\\', $extension = '.php')
66: {
67: $loaderClass = get_called_class();
68: $loader = new $loaderClass($namespace, $path);
69: $loader->setNamespaceSeparator($separator);
70: $loader->setFileExtension($extension);
71: $loader->register();
72: return $loader;
73: }
74: /**
75: * Sets the namespace separator used by classes in the namespace of this class loader.
76: *
77: * @param string $sep The separator to use.
78: *
79: * @return void
80: */
81: public function setNamespaceSeparator($sep)
82: {
83: $this->namespaceSeparator = $sep;
84: }
85:
86: /**
87: * Gets the namespace seperator used by classes in the namespace of this class loader.
88: *
89: * @return string
90: */
91: public function getNamespaceSeparator()
92: {
93: return $this->namespaceSeparator;
94: }
95:
96: /**
97: * Sets the base include path for all class files in the namespace of this class loader.
98: *
99: * @param string $includePath include path
100: *
101: * @return void
102: */
103: public function setIncludePath($includePath)
104: {
105: $this->includePath = $includePath;
106: }
107:
108: /**
109: * Gets the base include path for all class files in the namespace of this class loader.
110: *
111: * @return string $includePath include path
112: */
113: public function getIncludePath()
114: {
115: return $this->includePath;
116: }
117:
118: /**
119: * Sets the file extension of class files in the namespace of this class loader.
120: *
121: * @param string $fileExtension file extension
122: *
123: * @return void
124: */
125: public function setFileExtension($fileExtension)
126: {
127: $this->fileExtension = $fileExtension;
128: }
129:
130: /**
131: * Gets the file extension of class files in the namespace of this class loader.
132: *
133: * @return string $fileExtension
134: */
135: public function getFileExtension()
136: {
137: return $this->fileExtension;
138: }
139:
140: /**
141: * Installs this class loader on the SPL autoload stack.
142: *
143: * @return void
144: */
145: public function register()
146: {
147: spl_autoload_register(array($this, 'loadClass'));
148: }
149:
150: /**
151: * Uninstalls this class loader from the SPL autoloader stack.
152: *
153: * @return void
154: */
155: public function unregister()
156: {
157: spl_autoload_unregister(array($this, 'loadClass'));
158: }
159:
160: /**
161: * Loads the given class or interface.
162: *
163: * @param string $className The name of the class to load.
164: *
165: * @return void
166: */
167: public function loadClass($className)
168: {
169: if (null === $this->namespace
170: || $this->namespace.$this->namespaceSeparator
171: === substr($className, 0, strlen($this->namespace.$this->namespaceSeparator))
172: ) {
173: $fileName = '';
174: $namespace = '';
175: if (false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) {
176: $namespace = substr($className, 0, $lastNsPos);
177: $className = substr($className, $lastNsPos + 1);
178: $fileName = str_replace(
179: $this->namespaceSeparator,
180: DIRECTORY_SEPARATOR,
181: $namespace
182: ) . DIRECTORY_SEPARATOR;
183: }
184: $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->fileExtension;
185:
186: $absolute = ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '') . $fileName;
187: if (file_exists($absolute)) {
188: require $absolute;
189: }
190: //require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '') . $fileName;
191: }
192: }
193: }
194: