|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\ClassLoader; |
| 13 | + |
| 14 | +/** |
| 15 | + * WinCacheClassLoader implements a wrapping autoloader cached in WinCache for PHP 5.3. |
| 16 | + * |
| 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 autoloader following this convention (the Composer one for instance) |
| 21 | + * |
| 22 | + * $loader = new ClassLoader(); |
| 23 | + * |
| 24 | + * // register classes with namespaces |
| 25 | + * $loader->add('Symfony\Component', __DIR__.'/component'); |
| 26 | + * $loader->add('Symfony', __DIR__.'/framework'); |
| 27 | + * |
| 28 | + * $cachedLoader = new WinCacheClassLoader('my_prefix', $loader); |
| 29 | + * |
| 30 | + * // activate the cached autoloader |
| 31 | + * $cachedLoader->register(); |
| 32 | + * |
| 33 | + * // eventually deactivate the non-cached loader if it was registered previously |
| 34 | + * // to be sure to use the cached one. |
| 35 | + * $loader->unregister(); |
| 36 | + * |
| 37 | + * @author Fabien Potencier <[email protected]> |
| 38 | + * @author Kris Wallsmith <[email protected]> |
| 39 | + * @author Artem Ryzhkov <[email protected]> |
| 40 | + * |
| 41 | + * @api |
| 42 | + */ |
| 43 | +class WinCacheClassLoader |
| 44 | +{ |
| 45 | + private $prefix; |
| 46 | + |
| 47 | + /** |
| 48 | + * The class loader object being decorated. |
| 49 | + * |
| 50 | + * @var \Symfony\Component\ClassLoader\ClassLoader |
| 51 | + * A class loader object that implements the findFile() method. |
| 52 | + */ |
| 53 | + protected $decorated; |
| 54 | + |
| 55 | + /** |
| 56 | + * Constructor. |
| 57 | + * |
| 58 | + * @param string $prefix The WinCache namespace prefix to use. |
| 59 | + * @param object $decorated A class loader object that implements the findFile() method. |
| 60 | + * |
| 61 | + * @throws \RuntimeException |
| 62 | + * @throws \InvalidArgumentException |
| 63 | + * |
| 64 | + * @api |
| 65 | + */ |
| 66 | + public function __construct($prefix, $decorated) |
| 67 | + { |
| 68 | + if (!extension_loaded('wincache')) { |
| 69 | + throw new \RuntimeException('Unable to use WinCacheClassLoader as WinCache is not enabled.'); |
| 70 | + } |
| 71 | + |
| 72 | + if (!method_exists($decorated, 'findFile')) { |
| 73 | + throw new \InvalidArgumentException('The class finder must implement a "findFile" method.'); |
| 74 | + } |
| 75 | + |
| 76 | + $this->prefix = $prefix; |
| 77 | + $this->decorated = $decorated; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Registers this instance as an autoloader. |
| 82 | + * |
| 83 | + * @param Boolean $prepend Whether to prepend the autoloader or not |
| 84 | + */ |
| 85 | + public function register($prepend = false) |
| 86 | + { |
| 87 | + spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Unregisters this instance as an autoloader. |
| 92 | + */ |
| 93 | + public function unregister() |
| 94 | + { |
| 95 | + spl_autoload_unregister(array($this, 'loadClass')); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Loads the given class or interface. |
| 100 | + * |
| 101 | + * @param string $class The name of the class |
| 102 | + * |
| 103 | + * @return Boolean|null True, if loaded |
| 104 | + */ |
| 105 | + public function loadClass($class) |
| 106 | + { |
| 107 | + if ($file = $this->findFile($class)) { |
| 108 | + require $file; |
| 109 | + |
| 110 | + return true; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Finds a file by class name while caching lookups to WinCache. |
| 116 | + * |
| 117 | + * @param string $class A class name to resolve to file |
| 118 | + * |
| 119 | + * @return string|null |
| 120 | + */ |
| 121 | + public function findFile($class) |
| 122 | + { |
| 123 | + if (false === $file = wincache_ucache_get($this->prefix.$class)) { |
| 124 | + wincache_ucache_set($this->prefix.$class, $file = $this->decorated->findFile($class), 0); |
| 125 | + } |
| 126 | + |
| 127 | + return $file; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Passes through all unknown calls onto the decorated object. |
| 132 | + */ |
| 133 | + public function __call($method, $args) |
| 134 | + { |
| 135 | + return call_user_func_array(array($this->decorated, $method), $args); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments