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__.'\ApcClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use `composer install --apcu-autoloader` instead.', E_USER_DEPRECATED);
17 * ApcClassLoader implements a wrapping autoloader cached in APC 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 * use Symfony\Component\ClassLoader\ClassLoader;
27 * $loader = new ClassLoader();
28 * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
29 * $loader->addPrefix('Symfony', __DIR__.'/framework');
31 * // or with a Composer autoloader
32 * use Composer\Autoload\ClassLoader;
34 * $loader = new ClassLoader();
35 * $loader->add('Symfony\Component', __DIR__.'/component');
36 * $loader->add('Symfony', __DIR__.'/framework');
38 * $cachedLoader = new ApcClassLoader('my_prefix', $loader);
40 * // activate the cached autoloader
41 * $cachedLoader->register();
43 * // eventually deactivate the non-cached loader if it was registered previously
44 * // to be sure to use the cached one.
45 * $loader->unregister();
47 * @author Fabien Potencier <fabien@symfony.com>
48 * @author Kris Wallsmith <kris@symfony.com>
50 * @deprecated since version 3.3, to be removed in 4.0. Use `composer install --apcu-autoloader` instead.
57 * A class loader object that implements the findFile() method.
64 * @param string $prefix The APC namespace prefix to use
65 * @param object $decorated A class loader object that implements the findFile() method
67 * @throws \RuntimeException
68 * @throws \InvalidArgumentException
70 public function __construct($prefix, $decorated)
72 if (!\function_exists('apcu_fetch')) {
73 throw new \RuntimeException('Unable to use ApcClassLoader as APC is not installed.');
76 if (!method_exists($decorated, 'findFile')) {
77 throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
80 $this->prefix = $prefix;
81 $this->decorated = $decorated;
85 * Registers this instance as an autoloader.
87 * @param bool $prepend Whether to prepend the autoloader or not
89 public function register($prepend = false)
91 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
95 * Unregisters this instance as an autoloader.
97 public function unregister()
99 spl_autoload_unregister(array($this, 'loadClass'));
103 * Loads the given class or interface.
105 * @param string $class The name of the class
107 * @return bool|null True, if loaded
109 public function loadClass($class)
111 if ($file = $this->findFile($class)) {
119 * Finds a file by class name while caching lookups to APC.
121 * @param string $class A class name to resolve to file
123 * @return string|null
125 public function findFile($class)
127 $file = apcu_fetch($this->prefix.$class, $success);
130 apcu_store($this->prefix.$class, $file = $this->decorated->findFile($class) ?: null);
137 * Passes through all unknown calls onto the decorated object.
139 public function __call($method, $args)
141 return \call_user_func_array(array($this->decorated, $method), $args);