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 <jonwage@gmail.com>
14 : * @author Roman S. Borschel <roman@code-factory.org>
15 : * @author Matthew Weier O'Phinney <matthew@zend.com>
16 : * @author Kris Wallsmith <kris.wallsmith@gmail.com>
17 : * @author Fabien Potencier <fabien.potencier@symfony-project.org>
18 : */
19 :
20 : namespace Mockery;
21 :
22 : class Loader
23 : {
24 : private $_fileExtension = '.php';
25 : private $_namespace;
26 : private $_includePath;
27 : private $_namespaceSeparator = '\\';
28 :
29 : /**
30 : * Creates a new <tt>Loader</tt> that loads classes of the
31 : * specified namespace.
32 : *
33 : * @param string $ns The namespace to use.
34 : */
35 : public function __construct($ns = 'Mockery', $includePath = null)
36 : {
37 0 : $this->_namespace = $ns;
38 0 : $this->_includePath = $includePath;
39 0 : }
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 0 : $this->_namespaceSeparator = $sep;
49 0 : }
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 0 : 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 0 : $this->_includePath = $includePath;
69 0 : }
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 0 : 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 0 : $this->_fileExtension = $fileExtension;
89 0 : }
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 0 : return $this->_fileExtension;
99 : }
100 :
101 : /**
102 : * Installs this class loader on the SPL autoload stack.
103 : */
104 : public function register()
105 : {
106 0 : spl_autoload_register(array($this, 'loadClass'));
107 0 : }
108 :
109 : /**
110 : * Uninstalls this class loader from the SPL autoloader stack.
111 : */
112 : public function unregister()
113 : {
114 0 : spl_autoload_unregister(array($this, 'loadClass'));
115 0 : }
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 1 : if ($className === 'Mockery') {
126 0 : require 'Mockery.php';
127 0 : return;
128 : }
129 1 : if (null === $this->_namespace
130 1 : || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
131 0 : $fileName = '';
132 0 : $namespace = '';
133 0 : if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
134 0 : $namespace = substr($className, 0, $lastNsPos);
135 0 : $className = substr($className, $lastNsPos + 1);
136 0 : $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
137 0 : }
138 0 : $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
139 0 : require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
140 0 : }
141 1 : }
142 : }
|