]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Common/ClassLoader.php
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the LGPL. For more information, see
17 * <http://www.doctrine-project.org>.
20 * @package ClassLoader
21 * @author Doctrine project <contact@doctrine-project.org>
22 * @author Johan Cwiklinski <johan@x-tnd.be>
23 * @license LGPL https://www.gnu.org/licenses/lgpl-3.0.fr.html
24 * @link http://www.doctrine-project.org - http://galette.tuxfamily.org
28 namespace Galette\Common
;
31 * A <tt>ClassLoader</tt> is an autoloader for class files that can be
32 * installed on the SPL autoload stack. It is a class loader that either loads only classes
33 * of a specific namespace or all namespaces and it is suitable for working together
34 * with other autoloaders in the SPL autoload stack.
36 * If no include path is configured through the constructor or {@link setIncludePath}, a ClassLoader
37 * relies on the PHP <code>include_path</code>.
40 * @package ClassLoader
41 * @author Roman Borschel <roman@code-factory.org>
42 * @license LGPL https://www.gnu.org/licenses/lgpl-3.0.fr.html
43 * @link http://www.doctrine-project.org - http://galette.tuxfamily.org
49 * @var string PHP file extension
51 protected $fileExtension = '.php';
54 * @var string Current namespace
59 * @var string Current include path
61 protected $includePath;
64 * @var string PHP namespace separator
66 protected $namespaceSeparator = '\\';
69 * Creates a new <tt>ClassLoader</tt> that loads classes of the
70 * specified namespace from the specified include path.
72 * If no include path is given, the ClassLoader relies on the PHP include_path.
73 * If neither a namespace nor an include path is given, the ClassLoader will
74 * be responsible for loading all classes, thereby relying on the PHP include_path.
76 * @param string $ns The namespace of the classes to load.
77 * @param string $includePath The base include path to use.
79 public function __construct($ns = null, $includePath = null)
81 if (!file_exists($includePath)) {
82 throw new \
RuntimeException('Include path "' . $includePath . '" doesn\'t exists');
85 $this->namespace = $ns;
86 $this->includePath
= $includePath;
90 * Sets the namespace separator used by classes in the namespace of this ClassLoader.
92 * @param string $sep The separator to use.
96 public function setNamespaceSeparator($sep)
98 $this->namespaceSeparator
= $sep;
102 * Gets the namespace separator used by classes in the namespace of this ClassLoader.
106 public function getNamespaceSeparator()
108 return $this->namespaceSeparator
;
112 * Sets the base include path for all class files in the namespace of this ClassLoader.
114 * @param string $includePath Include path
118 public function setIncludePath($includePath)
120 $this->includePath
= $includePath;
124 * Gets the base include path for all class files in the namespace of this ClassLoader.
128 public function getIncludePath()
130 return $this->includePath
;
134 * Sets the file extension of class files in the namespace of this ClassLoader.
136 * @param string $fileExtension File extension
140 public function setFileExtension($fileExtension)
142 $this->fileExtension
= $fileExtension;
146 * Gets the file extension of class files in the namespace of this ClassLoader.
150 public function getFileExtension()
152 return $this->fileExtension
;
156 * Registers this ClassLoader on the SPL autoload stack.
160 public function register()
162 spl_autoload_register(array($this, 'loadClass'));
166 * Removes this ClassLoader from the SPL autoload stack.
170 public function unregister()
172 spl_autoload_unregister(array($this, 'loadClass'));
176 * Loads the given class or interface.
178 * @param string $className The name of the class to load.
180 * @return boolean TRUE if the class has been successfully loaded, FALSE otherwise.
182 public function loadClass($className)
184 if ($this->namespace !== null && strpos($className, $this->namespace . $this->namespaceSeparator
) !== 0) {
188 if ($this->namespace !== null) {
189 $className = str_replace(
190 $this->namespaceSeparator
,
196 $path = ($this->includePath
!== null ?
$this->includePath
. DIRECTORY_SEPARATOR
: '')
198 . $this->fileExtension
;
200 if (file_exists($path)) {
209 * Asks this ClassLoader whether it can potentially load the class (file) with
212 * @param string $className The fully-qualified name of the class.
213 * @return boolean TRUE if this ClassLoader can load the class, FALSE otherwise.
215 public function canLoadClass($className)
217 if ($this->namespace !== null && strpos($className, $this->namespace . $this->namespaceSeparator
) !== 0) {
221 $file = str_replace($this->namespaceSeparator
, DIRECTORY_SEPARATOR
, $className) . $this->fileExtension
;
223 if ($this->includePath
!== null) {
224 return file_exists($this->includePath
. DIRECTORY_SEPARATOR
. $file);
227 return (false !== stream_resolve_include_path($file));
231 * Checks whether a class with a given name exists. A class "exists" if it is either
232 * already defined in the current request or if there is an autoloader on the SPL
233 * autoload stack that is a) responsible for the class in question and b) is able to
234 * load a class file in which the class definition resides.
236 * If the class is not already defined, each autoloader in the SPL autoload stack
237 * is asked whether it is able to tell if the class exists. If the autoloader is
238 * a <tt>ClassLoader</tt>, {@link canLoadClass} is used, otherwise the autoload
239 * function of the autoloader is invoked and expected to return a value that
240 * evaluates to TRUE if the class (file) exists. As soon as one autoloader reports
241 * that the class exists, TRUE is returned.
243 * Note that, depending on what kinds of autoloaders are installed on the SPL
244 * autoload stack, the class (file) might already be loaded as a result of checking
245 * for its existence. This is not the case with a <tt>ClassLoader</tt>, who separates
246 * these responsibilities.
248 * @param string $className The fully-qualified name of the class.
249 * @return boolean TRUE if the class exists as per the definition given above, FALSE otherwise.
251 public static function classExists($className)
253 if (class_exists($className, false) ||
interface_exists($className, false)) {
257 foreach (spl_autoload_functions() as $loader) {
258 if (is_array($loader)) { // array(???, ???)
259 if (is_object($loader[0])) {
260 if ($loader[0] instanceof ClassLoader
) { // array($obj, 'methodName')
261 if ($loader[0]->canLoadClass($className)) {
264 } elseif ($loader[0]->{$loader[1]}($className)) {
267 } elseif ($loader[0]::$loader[1]($className)) { // array('ClassName', 'methodName')
270 } elseif ($loader instanceof \Closure
) { // function($className) {..}
271 if ($loader($className)) {
274 } elseif (is_string($loader) && $loader($className)) { // "MyClass::loadClass"
279 return class_exists($className, false) ||
interface_exists($className, false);
283 * Gets the <tt>ClassLoader</tt> from the SPL autoload stack that is responsible
284 * for (and is able to load) the class with the given name.
286 * @param string $className The name of the class.
287 * @return The <tt>ClassLoader</tt> for the class or NULL if no such <tt>ClassLoader</tt> exists.
289 public static function getClassLoader($className)
291 foreach (spl_autoload_functions() as $loader) {
292 if (is_array($loader)
293 && $loader[0] instanceof ClassLoader
294 && $loader[0]->canLoadClass($className)