Przeglądaj źródła

Merge remote branch 'kriswallsmith/classloader/optimizations'

* kriswallsmith/classloader/optimizations:
  [ClassLoader] added an apc class loader
  [ClassLoader] created protected findFile() method to allow creating a cache layer via inheritance
  [ClassLoader] added a check before trimming the leading \
Fabien Potencier 14 lat temu
rodzic
commit
9ec4f8a8a9

+ 43 - 0
src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php

@@ -0,0 +1,43 @@
+<?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;
+
+require_once __DIR__.'/UniversalClassLoader.php';
+
+/**
+ * Class loader utilizing APC to remember where files are.
+ *
+ * @author Kris Wallsmith <kris.wallsmith@symfony.com>
+ */
+class ApcUniversalClassLoader extends UniversalClassLoader
+{
+    private $prefix;
+
+    /**
+     * Constructor.
+     *
+     * @param string $prefix A prefix to create a namespace in APC
+     */
+    public function __construct($prefix)
+    {
+        $this->prefix = $prefix;
+    }
+
+    protected function findFile($class)
+    {
+        if (false === $file = apc_fetch($this->prefix.$class)) {
+            apc_store($this->prefix.$class, $file = parent::findFile($class));
+        }
+
+        return $file;
+    }
+}

+ 22 - 10
src/Symfony/Component/ClassLoader/UniversalClassLoader.php

@@ -182,9 +182,25 @@ class UniversalClassLoader
      */
     public function loadClass($class)
     {
-        $class = ltrim($class, '\\');
+        if ($file = $this->findFile($class)) {
+            require $file;
+        }
+    }
+
+    /**
+     * 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
+     */
+    protected function findFile($class)
+    {
+        if ('\\' == $class[0]) {
+            $class = substr($class, 1);
+        }
 
-        if (false !== ($pos = strrpos($class, '\\'))) {
+        if (false !== $pos = strrpos($class, '\\')) {
             // namespaced class name
             $namespace = substr($class, 0, $pos);
             foreach ($this->namespaces as $ns => $dirs) {
@@ -193,8 +209,7 @@ class UniversalClassLoader
                         $className = substr($class, $pos + 1);
                         $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
                         if (file_exists($file)) {
-                            require $file;
-                            return;
+                            return $file;
                         }
                     }
                 }
@@ -203,8 +218,7 @@ class UniversalClassLoader
             foreach ($this->namespaceFallback as $dir) {
                 $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
                 if (file_exists($file)) {
-                    require $file;
-                    return;
+                    return $file;
                 }
             }
         } else {
@@ -214,8 +228,7 @@ class UniversalClassLoader
                     if (0 === strpos($class, $prefix)) {
                         $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
                         if (file_exists($file)) {
-                            require $file;
-                            return;
+                            return $file;
                         }
                     }
                 }
@@ -224,8 +237,7 @@ class UniversalClassLoader
             foreach ($this->prefixFallback as $dir) {
                 $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
                 if (file_exists($file)) {
-                    require $file;
-                    return;
+                    return $file;
                 }
             }
         }