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 \
This commit is contained in:
Fabien Potencier 2011-03-12 12:39:42 +01:00
commit 9ec4f8a8a9
2 changed files with 65 additions and 10 deletions

View File

@ -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;
}
}

View File

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