2013-07-08 13:11:07 +04:00
|
|
|
<?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;
|
|
|
|
|
|
|
|
/**
|
2014-11-14 01:37:34 +03:00
|
|
|
* XcacheClassLoader implements a wrapping autoloader cached in XCache for PHP 5.3.
|
2013-07-08 13:11:07 +04:00
|
|
|
*
|
|
|
|
* 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;
|
2014-11-14 01:37:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var object A class loader object that implements the findFile() method
|
|
|
|
*/
|
|
|
|
private $decorated;
|
2013-07-08 13:11:07 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2014-11-14 01:37:34 +03:00
|
|
|
* @param string $prefix The XCache namespace prefix to use.
|
|
|
|
* @param object $decorated A class loader object that implements the findFile() method.
|
2013-07-08 13:11:07 +04:00
|
|
|
*
|
|
|
|
* @throws \RuntimeException
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2014-11-14 01:37:34 +03:00
|
|
|
public function __construct($prefix, $decorated)
|
2013-07-08 13:11:07 +04:00
|
|
|
{
|
2014-11-14 01:37:34 +03:00
|
|
|
if (!extension_loaded('xcache')) {
|
|
|
|
throw new \RuntimeException('Unable to use XcacheClassLoader as XCache is not enabled.');
|
2013-07-08 13:11:07 +04:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:37:34 +03:00
|
|
|
if (!method_exists($decorated, 'findFile')) {
|
2013-07-08 13:11:07 +04:00
|
|
|
throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->prefix = $prefix;
|
2014-11-14 01:37:34 +03:00
|
|
|
$this->decorated = $decorated;
|
2013-07-08 13:11:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers this instance as an autoloader.
|
|
|
|
*
|
2014-08-28 02:10:31 +04:00
|
|
|
* @param bool $prepend Whether to prepend the autoloader or not
|
2013-07-08 13:11:07 +04:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
2014-08-28 02:10:31 +04:00
|
|
|
* @return bool|null True, if loaded
|
2013-07-08 13:11:07 +04:00
|
|
|
*/
|
|
|
|
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 {
|
2014-11-14 01:37:34 +03:00
|
|
|
$file = $this->decorated->findFile($class);
|
2014-08-28 02:10:31 +04:00
|
|
|
xcache_set($this->prefix.$class, $file);
|
2013-07-08 13:11:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $file;
|
|
|
|
}
|
2014-11-14 01:37:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Passes through all unknown calls onto the decorated object.
|
|
|
|
*/
|
|
|
|
public function __call($method, $args)
|
|
|
|
{
|
|
|
|
return call_user_func_array(array($this->decorated, $method), $args);
|
|
|
|
}
|
2013-07-08 13:11:07 +04:00
|
|
|
}
|