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