|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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 | + * ApcClassLoader implements a wrapping autoloader cached in APC 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 ApcClassLoader('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 <fabien@symfony.com> |
| 38 | + * @author Kris Wallsmith <kris@symfony.com> |
| 39 | + * |
| 40 | + * @api |
| 41 | + */ |
| 42 | +class ApcClassLoader |
| 43 | +{ |
| 44 | + private $prefix; |
| 45 | + private $classFinder; |
| 46 | + |
| 47 | + /** |
| 48 | + * Constructor. |
| 49 | + * |
| 50 | + * @param string $prefix A prefix to create a namespace in APC |
| 51 | + * @param object $classFinder |
| 52 | + * |
| 53 | + * @api |
| 54 | + */ |
| 55 | + public function __construct($prefix, $classFinder) |
| 56 | + { |
| 57 | + if (!extension_loaded('apc')) { |
| 58 | + throw new \RuntimeException('Unable to use ApcUniversalClassLoader as APC is not enabled.'); |
| 59 | + } |
| 60 | + |
| 61 | + $this->prefix = $prefix; |
| 62 | + $this->classFinder = $classFinder; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Registers this instance as an autoloader. |
| 67 | + * |
| 68 | + * @param Boolean $prepend Whether to prepend the autoloader or not |
| 69 | + */ |
| 70 | + public function register($prepend = false) |
| 71 | + { |
| 72 | + spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Unregisters this instance as an autoloader. |
| 77 | + */ |
| 78 | + public function unregister() |
| 79 | + { |
| 80 | + spl_autoload_unregister(array($this, 'loadClass')); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Loads the given class or interface. |
| 85 | + * |
| 86 | + * @param string $class The name of the class |
| 87 | + * @return Boolean|null True, if loaded |
| 88 | + */ |
| 89 | + public function loadClass($class) |
| 90 | + { |
| 91 | + if ($file = $this->findFile($class)) { |
| 92 | + require $file; |
| 93 | + return true; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Finds a file by class name while caching lookups to APC. |
| 99 | + * |
| 100 | + * @param string $class A class name to resolve to file |
| 101 | + * |
| 102 | + * @return string|null |
| 103 | + */ |
| 104 | + public function findFile($class) |
| 105 | + { |
| 106 | + if (false === $file = apc_fetch($this->prefix.$class)) { |
| 107 | + apc_store($this->prefix.$class, $file = $this->classFinder->findFile($class)); |
| 108 | + } |
| 109 | + |
| 110 | + return $file; |
| 111 | + } |
| 112 | +} |
0 commit comments