Ver código fonte

Make Predis\Autoloader faster.

Daniele Alessandri 13 anos atrás
pai
commit
930af24c7f
1 arquivos alterados com 9 adições e 10 exclusões
  1. 9 10
      lib/Predis/Autoloader.php

+ 9 - 10
lib/Predis/Autoloader.php

@@ -15,19 +15,22 @@ namespace Predis;
  * Implements a lightweight PSR-0 compliant autoloader.
  *
  * @author Eric Naeseth <eric@thumbtack.com>
+ * @author Daniele Alessandri <suppakilla@gmail.com>
  */
 class Autoloader
 {
-    private $baseDir;
+    private $directory;
     private $prefix;
+    private $prefixLength;
 
     /**
      * @param string $baseDirectory Base directory where the source files are located.
      */
-    public function __construct($baseDirectory = null)
+    public function __construct($baseDirectory = __DIR__)
     {
-        $this->baseDir = $baseDirectory ?: dirname(__FILE__);
+        $this->directory = $baseDirectory;
         $this->prefix = __NAMESPACE__ . '\\';
+        $this->prefixLength = strlen($this->prefix);
     }
 
     /**
@@ -45,13 +48,9 @@ class Autoloader
      */
     public function autoload($className)
     {
-        if (0 !== strpos($className, $this->prefix)) {
-            return;
+        if (0 === strpos($className, $this->prefix)) {
+            $parts = explode('\\', substr($className, $this->prefixLength));
+            require($this->directory.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts).'.php');
         }
-
-        $relativeClassName = substr($className, strlen($this->prefix));
-        $classNameParts = explode('\\', $relativeClassName);
-
-        require_once $this->baseDir.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $classNameParts).'.php';
     }
 }