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;
15 * WinCacheClassLoader implements a wrapping autoloader cached in WinCache.
17 * It expects an object implementing a findFile method to find the file. This
18 * allow using it as a wrapper around the other loaders of the component (the
19 * ClassLoader and the UniversalClassLoader for instance) but also around any
20 * other autoloaders following this convention (the Composer one for instance).
22 * // with a Symfony autoloader
23 * use Symfony\Component\ClassLoader\ClassLoader;
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 WinCacheClassLoader('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 Artem Ryzhkov <artem@smart-core.org>
49 class WinCacheClassLoader
54 * A class loader object that implements the findFile() method.
63 * @param string $prefix The WinCache namespace prefix to use
64 * @param object $decorated A class loader object that implements the findFile() method
66 * @throws \RuntimeException
67 * @throws \InvalidArgumentException
69 public function __construct($prefix, $decorated)
71 if (!extension_loaded('wincache')) {
72 throw new \RuntimeException('Unable to use WinCacheClassLoader as WinCache is not enabled.');
75 if (!method_exists($decorated, 'findFile')) {
76 throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
79 $this->prefix = $prefix;
80 $this->decorated = $decorated;
84 * Registers this instance as an autoloader.
86 * @param bool $prepend Whether to prepend the autoloader or not
88 public function register($prepend = false)
90 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
94 * Unregisters this instance as an autoloader.
96 public function unregister()
98 spl_autoload_unregister(array($this, 'loadClass'));
102 * Loads the given class or interface.
104 * @param string $class The name of the class
106 * @return bool|null True, if loaded
108 public function loadClass($class)
110 if ($file = $this->findFile($class)) {
118 * Finds a file by class name while caching lookups to WinCache.
120 * @param string $class A class name to resolve to file
122 * @return string|null
124 public function findFile($class)
126 $file = wincache_ucache_get($this->prefix.$class, $success);
129 wincache_ucache_set($this->prefix.$class, $file = $this->decorated->findFile($class) ?: null, 0);
136 * Passes through all unknown calls onto the decorated object.
138 public function __call($method, $args)
140 return call_user_func_array(array($this->decorated, $method), $args);