annotate vendor/symfony/class-loader/ApcClassLoader.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\ClassLoader;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * ApcClassLoader implements a wrapping autoloader cached in APC for PHP 5.3.
Chris@0 16 *
Chris@0 17 * It expects an object implementing a findFile method to find the file. This
Chris@0 18 * allows using it as a wrapper around the other loaders of the component (the
Chris@0 19 * ClassLoader for instance) but also around any other autoloaders following
Chris@0 20 * this convention (the Composer one for instance).
Chris@0 21 *
Chris@0 22 * // with a Symfony autoloader
Chris@0 23 * use Symfony\Component\ClassLoader\ClassLoader;
Chris@0 24 *
Chris@0 25 * $loader = new ClassLoader();
Chris@0 26 * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
Chris@0 27 * $loader->addPrefix('Symfony', __DIR__.'/framework');
Chris@0 28 *
Chris@0 29 * // or with a Composer autoloader
Chris@0 30 * use Composer\Autoload\ClassLoader;
Chris@0 31 *
Chris@0 32 * $loader = new ClassLoader();
Chris@0 33 * $loader->add('Symfony\Component', __DIR__.'/component');
Chris@0 34 * $loader->add('Symfony', __DIR__.'/framework');
Chris@0 35 *
Chris@0 36 * $cachedLoader = new ApcClassLoader('my_prefix', $loader);
Chris@0 37 *
Chris@0 38 * // activate the cached autoloader
Chris@0 39 * $cachedLoader->register();
Chris@0 40 *
Chris@0 41 * // eventually deactivate the non-cached loader if it was registered previously
Chris@0 42 * // to be sure to use the cached one.
Chris@0 43 * $loader->unregister();
Chris@0 44 *
Chris@0 45 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 46 * @author Kris Wallsmith <kris@symfony.com>
Chris@0 47 */
Chris@0 48 class ApcClassLoader
Chris@0 49 {
Chris@0 50 private $prefix;
Chris@0 51
Chris@0 52 /**
Chris@0 53 * A class loader object that implements the findFile() method.
Chris@0 54 *
Chris@0 55 * @var object
Chris@0 56 */
Chris@0 57 protected $decorated;
Chris@0 58
Chris@0 59 /**
Chris@0 60 * Constructor.
Chris@0 61 *
Chris@0 62 * @param string $prefix The APC namespace prefix to use
Chris@0 63 * @param object $decorated A class loader object that implements the findFile() method
Chris@0 64 *
Chris@0 65 * @throws \RuntimeException
Chris@0 66 * @throws \InvalidArgumentException
Chris@0 67 */
Chris@0 68 public function __construct($prefix, $decorated)
Chris@0 69 {
Chris@0 70 if (!function_exists('apcu_fetch')) {
Chris@0 71 throw new \RuntimeException('Unable to use ApcClassLoader as APC is not installed.');
Chris@0 72 }
Chris@0 73
Chris@0 74 if (!method_exists($decorated, 'findFile')) {
Chris@0 75 throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
Chris@0 76 }
Chris@0 77
Chris@0 78 $this->prefix = $prefix;
Chris@0 79 $this->decorated = $decorated;
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Registers this instance as an autoloader.
Chris@0 84 *
Chris@0 85 * @param bool $prepend Whether to prepend the autoloader or not
Chris@0 86 */
Chris@0 87 public function register($prepend = false)
Chris@0 88 {
Chris@0 89 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * Unregisters this instance as an autoloader.
Chris@0 94 */
Chris@0 95 public function unregister()
Chris@0 96 {
Chris@0 97 spl_autoload_unregister(array($this, 'loadClass'));
Chris@0 98 }
Chris@0 99
Chris@0 100 /**
Chris@0 101 * Loads the given class or interface.
Chris@0 102 *
Chris@0 103 * @param string $class The name of the class
Chris@0 104 *
Chris@0 105 * @return bool|null True, if loaded
Chris@0 106 */
Chris@0 107 public function loadClass($class)
Chris@0 108 {
Chris@0 109 if ($file = $this->findFile($class)) {
Chris@0 110 require $file;
Chris@0 111
Chris@0 112 return true;
Chris@0 113 }
Chris@0 114 }
Chris@0 115
Chris@0 116 /**
Chris@0 117 * Finds a file by class name while caching lookups to APC.
Chris@0 118 *
Chris@0 119 * @param string $class A class name to resolve to file
Chris@0 120 *
Chris@0 121 * @return string|null
Chris@0 122 */
Chris@0 123 public function findFile($class)
Chris@0 124 {
Chris@0 125 $file = apcu_fetch($this->prefix.$class, $success);
Chris@0 126
Chris@0 127 if (!$success) {
Chris@0 128 apcu_store($this->prefix.$class, $file = $this->decorated->findFile($class) ?: null);
Chris@0 129 }
Chris@0 130
Chris@0 131 return $file;
Chris@0 132 }
Chris@0 133
Chris@0 134 /**
Chris@0 135 * Passes through all unknown calls onto the decorated object.
Chris@0 136 */
Chris@0 137 public function __call($method, $args)
Chris@0 138 {
Chris@0 139 return call_user_func_array(array($this->decorated, $method), $args);
Chris@0 140 }
Chris@0 141 }