merged branch kimhemsoe/xcache_classloader (PR #3809)
Commits -------c36651b
Fixed spelling errorf123684
Removed leftover from c/pb74a5d4
Updated to new cache loader pattern.7e66908
Added XCache class loader Discussion ---------- [ClassLoader] Added XCache class loader Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - There is no tests, as it seems there is no way to use xcache storage functions from CLI. --------------------------------------------------------------------------- by stof at 2012-04-06T20:12:09Z Please implement a XcacheClassLoader following the same pattern than the new ApcClassLoader instead --------------------------------------------------------------------------- by cordoval at 2012-04-07T14:20:47Z - should include tests - should include documentation (will you also update the component documentation for this new class?) --------------------------------------------------------------------------- by stof at 2012-04-07T14:25:00Z @cordoval the PR explains why there is no tests: xcache canot be used in the CLI. --------------------------------------------------------------------------- by cordoval at 2012-04-07T14:26:43Z ok @stof sorry it said it seemed not to be possible, i thought it was possible but I am wrong. --------------------------------------------------------------------------- by kimhemsoe at 2012-04-07T15:01:24Z @cordoval My english is really horrible. I would not mind if someone else could do that task for me. We also need to add doc for the new ApcClassLoader. --------------------------------------------------------------------------- by cordoval at 2012-04-07T15:03:57Z I wish you can explain me more then about this class and how to use it in code so then I can write easily the documentation :D deal? --------------------------------------------------------------------------- by kimhemsoe at 2012-04-07T15:21:25Z Deal :P The XcacheClassLoader and ApcClassLoader replaces the old ApcUniversalClassLoader. They giving us support for using another loader then UniversalClassLoader, without duplicating the cache layer. Aslong it have a public function findFile($class) method. $loader = new ClassLoader(); // register classes with namespaces $loader->add('Symfony\Component', __DIR__.'/component'); $loader->add('Symfony', __DIR__.'/framework'); $cachedLoader = new XcacheClassLoader('my_prefix', $loader); // activate the cached autoloader $cachedLoader->register(); Think that is more or less the essence of this. --------------------------------------------------------------------------- by cordoval at 2012-04-09T08:28:53Z it is not add but registerNamespace right? so the main idea is to get rid of the restriction to use Apc with Universal loader what is the comparative advantage between APC and Xcache? --------------------------------------------------------------------------- by kimhemsoe at 2012-04-09T08:55:23Z Yes if the $loader (class finder) were to be a instance UniversalClassLoader. Yes the main idea is to be able to reuse the cache layer with any class loader there obey to the one restriction. Difference between apc and xcache and why to use what is coming down to taste and your setup. We use xcache as APC have some issues in fastcgi setups. when we upgrade to php54 at somepoint we get to chance to move to php-fpm wich solves these issues. Short story: Slightly out of scope for any documentation in here :-P
This commit is contained in:
commit
22d8a436fa
119
src/Symfony/Component/ClassLoader/XcacheClassLoader.php
Normal file
119
src/Symfony/Component/ClassLoader/XcacheClassLoader.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* XcacheClassLoader implements a wrapping autoloader cached in Xcache for PHP 5.3.
|
||||
*
|
||||
* It expects an object implementing a findFile method to find the file. This
|
||||
* allows using it as a wrapper around the other loaders of the component (the
|
||||
* ClassLoader and the UniversalClassLoader for instance) but also around any
|
||||
* other autoloader following this convention (the Composer one for instance)
|
||||
*
|
||||
* $loader = new ClassLoader();
|
||||
*
|
||||
* // register classes with namespaces
|
||||
* $loader->add('Symfony\Component', __DIR__.'/component');
|
||||
* $loader->add('Symfony', __DIR__.'/framework');
|
||||
*
|
||||
* $cachedLoader = new XcacheClassLoader('my_prefix', $loader);
|
||||
*
|
||||
* // activate the cached autoloader
|
||||
* $cachedLoader->register();
|
||||
*
|
||||
* // eventually deactivate the non-cached loader if it was registered previously
|
||||
* // to be sure to use the cached one.
|
||||
* $loader->unregister();
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Kris Wallsmith <kris@symfony.com>
|
||||
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class XcacheClassLoader
|
||||
{
|
||||
private $prefix;
|
||||
private $classFinder;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $prefix A prefix to create a namespace in Xcache
|
||||
* @param object $classFinder
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function __construct($prefix, $classFinder)
|
||||
{
|
||||
if (!extension_loaded('Xcache')) {
|
||||
throw new \RuntimeException('Unable to use XcacheClassLoader as Xcache is not enabled.');
|
||||
}
|
||||
|
||||
if (!method_exists($classFinder, 'findFile')) {
|
||||
throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
|
||||
}
|
||||
|
||||
$this->prefix = $prefix;
|
||||
$this->classFinder = $classFinder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers this instance as an autoloader.
|
||||
*
|
||||
* @param Boolean $prepend Whether to prepend the autoloader or not
|
||||
*/
|
||||
public function register($prepend = false)
|
||||
{
|
||||
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters this instance as an autoloader.
|
||||
*/
|
||||
public function unregister()
|
||||
{
|
||||
spl_autoload_unregister(array($this, 'loadClass'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the given class or interface.
|
||||
*
|
||||
* @param string $class The name of the class
|
||||
* @return Boolean|null True, if loaded
|
||||
*/
|
||||
public function loadClass($class)
|
||||
{
|
||||
if ($file = $this->findFile($class)) {
|
||||
require $file;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a file by class name while caching lookups to Xcache.
|
||||
*
|
||||
* @param string $class A class name to resolve to file
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function findFile($class)
|
||||
{
|
||||
if (xcache_isset($this->prefix.$class)) {
|
||||
$file = xcache_get($this->prefix.$class);
|
||||
} else {
|
||||
xcache_set($this->prefix.$class, $file = $this->classFinder->findFile($class));
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user