Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\ClassLoader; Chris@0: Chris@14: @trigger_error('The '.__NAMESPACE__.'\XcacheClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use `composer install --apcu-autoloader` instead.', E_USER_DEPRECATED); Chris@14: Chris@0: /** Chris@0: * XcacheClassLoader implements a wrapping autoloader cached in XCache for PHP 5.3. Chris@0: * Chris@0: * It expects an object implementing a findFile method to find the file. This Chris@0: * allows using it as a wrapper around the other loaders of the component (the Chris@0: * ClassLoader for instance) but also around any other autoloaders following Chris@0: * this convention (the Composer one for instance). Chris@0: * Chris@0: * // with a Symfony autoloader Chris@0: * $loader = new ClassLoader(); Chris@0: * $loader->addPrefix('Symfony\Component', __DIR__.'/component'); Chris@0: * $loader->addPrefix('Symfony', __DIR__.'/framework'); Chris@0: * Chris@0: * // or with a Composer autoloader Chris@0: * use Composer\Autoload\ClassLoader; Chris@0: * Chris@0: * $loader = new ClassLoader(); Chris@0: * $loader->add('Symfony\Component', __DIR__.'/component'); Chris@0: * $loader->add('Symfony', __DIR__.'/framework'); Chris@0: * Chris@0: * $cachedLoader = new XcacheClassLoader('my_prefix', $loader); Chris@0: * Chris@0: * // activate the cached autoloader Chris@0: * $cachedLoader->register(); Chris@0: * Chris@0: * // eventually deactivate the non-cached loader if it was registered previously Chris@0: * // to be sure to use the cached one. Chris@0: * $loader->unregister(); Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: * @author Kris Wallsmith Chris@0: * @author Kim Hemsø Rasmussen Chris@14: * Chris@14: * @deprecated since version 3.3, to be removed in 4.0. Use `composer install --apcu-autoloader` instead. Chris@0: */ Chris@0: class XcacheClassLoader Chris@0: { Chris@0: private $prefix; Chris@0: private $decorated; Chris@0: Chris@0: /** Chris@0: * @param string $prefix The XCache namespace prefix to use Chris@0: * @param object $decorated A class loader object that implements the findFile() method Chris@0: * Chris@0: * @throws \RuntimeException Chris@0: * @throws \InvalidArgumentException Chris@0: */ Chris@0: public function __construct($prefix, $decorated) Chris@0: { Chris@17: if (!\extension_loaded('xcache')) { Chris@0: throw new \RuntimeException('Unable to use XcacheClassLoader as XCache is not enabled.'); Chris@0: } Chris@0: Chris@0: if (!method_exists($decorated, 'findFile')) { Chris@0: throw new \InvalidArgumentException('The class finder must implement a "findFile" method.'); Chris@0: } Chris@0: Chris@0: $this->prefix = $prefix; Chris@0: $this->decorated = $decorated; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Registers this instance as an autoloader. Chris@0: * Chris@0: * @param bool $prepend Whether to prepend the autoloader or not Chris@0: */ Chris@0: public function register($prepend = false) Chris@0: { Chris@17: spl_autoload_register([$this, 'loadClass'], true, $prepend); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Unregisters this instance as an autoloader. Chris@0: */ Chris@0: public function unregister() Chris@0: { Chris@17: spl_autoload_unregister([$this, 'loadClass']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads the given class or interface. Chris@0: * Chris@0: * @param string $class The name of the class Chris@0: * Chris@0: * @return bool|null True, if loaded Chris@0: */ Chris@0: public function loadClass($class) Chris@0: { Chris@0: if ($file = $this->findFile($class)) { Chris@0: require $file; Chris@0: Chris@0: return true; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finds a file by class name while caching lookups to Xcache. Chris@0: * Chris@0: * @param string $class A class name to resolve to file Chris@0: * Chris@0: * @return string|null Chris@0: */ Chris@0: public function findFile($class) Chris@0: { Chris@0: if (xcache_isset($this->prefix.$class)) { Chris@0: $file = xcache_get($this->prefix.$class); Chris@0: } else { Chris@0: $file = $this->decorated->findFile($class) ?: null; Chris@0: xcache_set($this->prefix.$class, $file); Chris@0: } Chris@0: Chris@0: return $file; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes through all unknown calls onto the decorated object. Chris@0: */ Chris@0: public function __call($method, $args) Chris@0: { Chris@17: return \call_user_func_array([$this->decorated, $method], $args); Chris@0: } Chris@0: }