Przeglądaj źródła

[ClassLoader] added ClassLoaderInterface

Fabien Potencier 14 lat temu
rodzic
commit
eded17adf2

+ 2 - 1
src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php

@@ -11,6 +11,7 @@
 
 namespace Symfony\Component\ClassLoader;
 
+require_once __DIR__.'/ClassLoaderInterface.php';
 require_once __DIR__.'/UniversalClassLoader.php';
 
 /**
@@ -36,7 +37,7 @@ class ApcUniversalClassLoader extends UniversalClassLoader
         $this->prefix = $prefix;
     }
 
-    protected function findFile($class)
+    public function findFile($class)
     {
         if (false === $file = apc_fetch($this->prefix.$class)) {
             apc_store($this->prefix.$class, $file = parent::findFile($class));

+ 42 - 0
src/Symfony/Component/ClassLoader/ClassLoaderInterface.php

@@ -0,0 +1,42 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\ClassLoader;
+
+/**
+ * ClassLoaderInterface.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ *
+ * @api
+ */
+interface ClassLoaderInterface
+{
+    /**
+     * Loads the given class or interface.
+     *
+     * @param string $class The name of the class
+     *
+     * @api
+     */
+    function loadClass($class);
+
+    /**
+     * Finds the path to the file where the class is defined.
+     *
+     * @param string $class The name of the class
+     *
+     * @return string|null The path, if found
+     *
+     * @api
+     */
+    function findFile($class);
+}

+ 21 - 1
src/Symfony/Component/ClassLoader/MapFileClassLoader.php

@@ -11,6 +11,8 @@
 
 namespace Symfony\Component\ClassLoader;
 
+require_once __DIR__.'/ClassLoaderInterface.php';
+
 /**
  * A class loader that uses a mapping file to look up paths.
  *
@@ -18,7 +20,7 @@ namespace Symfony\Component\ClassLoader;
  *
  * @api
  */
-class MapFileClassLoader
+class MapFileClassLoader implements ClassLoaderInterface
 {
     private $map = array();
 
@@ -61,4 +63,22 @@ class MapFileClassLoader
             require $this->map[$class];
         }
     }
+
+    /**
+     * Finds the path to the file where the class is defined.
+     *
+     * @param string $class The name of the class
+     *
+     * @return string|null The path, if found
+     */
+    public function findFile($class)
+    {
+        if ('\\' === $class[0]) {
+            $class = substr($class, 1);
+        }
+
+        if (isset($this->map[$class])) {
+            return $this->map[$class];
+        }
+    }
 }

+ 4 - 2
src/Symfony/Component/ClassLoader/UniversalClassLoader.php

@@ -11,6 +11,8 @@
 
 namespace Symfony\Component\ClassLoader;
 
+require_once __DIR__.'/ClassLoaderInterface.php';
+
 /**
  * UniversalClassLoader implements a "universal" autoloader for PHP 5.3.
  *
@@ -54,7 +56,7 @@ namespace Symfony\Component\ClassLoader;
  *
  * @api
  */
-class UniversalClassLoader
+class UniversalClassLoader implements ClassLoaderInterface
 {
     private $namespaces = array();
     private $prefixes = array();
@@ -210,7 +212,7 @@ class UniversalClassLoader
      *
      * @return string|null The path, if found
      */
-    protected function findFile($class)
+    public function findFile($class)
     {
         if ('\\' == $class[0]) {
             $class = substr($class, 1);