4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\ClassLoader;
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);
17 * XcacheClassLoader implements a wrapping autoloader cached in XCache for PHP 5.3.
19 * It expects an object implementing a findFile method to find the file. This
20 * allows using it as a wrapper around the other loaders of the component (the
21 * ClassLoader for instance) but also around any other autoloaders following
22 * this convention (the Composer one for instance).
24 * // with a Symfony autoloader
25 * $loader = new ClassLoader();
26 * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
27 * $loader->addPrefix('Symfony', __DIR__.'/framework');
29 * // or with a Composer autoloader
30 * use Composer\Autoload\ClassLoader;
32 * $loader = new ClassLoader();
33 * $loader->add('Symfony\Component', __DIR__.'/component');
34 * $loader->add('Symfony', __DIR__.'/framework');
36 * $cachedLoader = new XcacheClassLoader('my_prefix', $loader);
38 * // activate the cached autoloader
39 * $cachedLoader->register();
41 * // eventually deactivate the non-cached loader if it was registered previously
42 * // to be sure to use the cached one.
43 * $loader->unregister();
45 * @author Fabien Potencier <fabien@symfony.com>
46 * @author Kris Wallsmith <kris@symfony.com>
47 * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
49 * @deprecated since version 3.3, to be removed in 4.0. Use `composer install --apcu-autoloader` instead.
51 class XcacheClassLoader
57 * @param string $prefix The XCache namespace prefix to use
58 * @param object $decorated A class loader object that implements the findFile() method
60 * @throws \RuntimeException
61 * @throws \InvalidArgumentException
63 public function __construct($prefix, $decorated)
65 if (!\extension_loaded('xcache')) {
66 throw new \RuntimeException('Unable to use XcacheClassLoader as XCache is not enabled.');
69 if (!method_exists($decorated, 'findFile')) {
70 throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
73 $this->prefix = $prefix;
74 $this->decorated = $decorated;
78 * Registers this instance as an autoloader.
80 * @param bool $prepend Whether to prepend the autoloader or not
82 public function register($prepend = false)
84 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
88 * Unregisters this instance as an autoloader.
90 public function unregister()
92 spl_autoload_unregister(array($this, 'loadClass'));
96 * Loads the given class or interface.
98 * @param string $class The name of the class
100 * @return bool|null True, if loaded
102 public function loadClass($class)
104 if ($file = $this->findFile($class)) {
112 * Finds a file by class name while caching lookups to Xcache.
114 * @param string $class A class name to resolve to file
116 * @return string|null
118 public function findFile($class)
120 if (xcache_isset($this->prefix.$class)) {
121 $file = xcache_get($this->prefix.$class);
123 $file = $this->decorated->findFile($class) ?: null;
124 xcache_set($this->prefix.$class, $file);
131 * Passes through all unknown calls onto the decorated object.
133 public function __call($method, $args)
135 return \call_user_func_array(array($this->decorated, $method), $args);