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: * @param string $includePath The include path to use.
35: */
36: public function __construct($ns = null, $includePath = null)
37: {
38: $this->namespace = $ns;
39: $this->includePath = $includePath;
40: }
41:
42: /**
43: * Sets the namespace separator used by classes in the namespace of this class loader.
44: *
45: * @param string $sep The separator to use.
46: */
47: public function setNamespaceSeparator($sep)
48: {
49: $this->namespaceSeparator = $sep;
50: }
51:
52: /**
53: * Gets the namespace separator used by classes in the namespace of this class loader.
54: *
55: * @return string $namespaceSeparator
56: */
57: public function getNamespaceSeparator()
58: {
59: return $this->namespaceSeparator;
60: }
61:
62: /**
63: * Sets the base include path for all class files in the namespace of this class loader.
64: *
65: * @param string $includePath
66: */
67: public function setIncludePath($includePath)
68: {
69: $this->includePath = $includePath;
70: }
71:
72: /**
73: * Gets the base include path for all class files in the namespace of this class loader.
74: *
75: * @return string $includePath
76: */
77: public function getIncludePath()
78: {
79: return $this->includePath;
80: }
81:
82: /**
83: * Sets the file extension of class files in the namespace of this class loader.
84: *
85: * @param string $fileExtension
86: */
87: public function setFileExtension($fileExtension)
88: {
89: $this->fileExtension = $fileExtension;
90: }
91:
92: /**
93: * Gets the file extension of class files in the namespace of this class loader.
94: *
95: * @return string $fileExtension
96: */
97: public function getFileExtension()
98: {
99: return $this->fileExtension;
100: }
101:
102: /**
103: * Installs this class loader on the SPL autoload stack.
104: */
105: public function register()
106: {
107: spl_autoload_register(array($this, 'loadClass'));
108: }
109:
110: /**
111: * Uninstalls this class loader from the SPL autoloader stack.
112: */
113: public function unregister()
114: {
115: spl_autoload_unregister(array($this, 'loadClass'));
116: }
117:
118: /**
119: * Loads the given class or interface.
120: *
121: * @param string $className The name of the class to load.
122: * @return void
123: */
124: public function loadClass($className)
125: {
126: if (null === $this->namespace || $this->namespace . $this->namespaceSeparator ===
127: substr($className, 0, strlen($this->namespace . $this->namespaceSeparator))
128: ) {
129: $fileName = '';
130: if (false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) {
131: $namespace = substr($className, 0, $lastNsPos);
132: $className = substr($className, $lastNsPos + 1);
133: $fileName = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $namespace)
134: . DIRECTORY_SEPARATOR;
135: }
136: $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->fileExtension;
137:
138: require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '') . $fileName;
139: }
140: }
141: }
142: