annotate vendor/symfony/class-loader/XcacheClassLoader.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 * XcacheClassLoader implements a wrapping autoloader cached in XCache 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 * $loader = new ClassLoader();
Chris@0 24 * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
Chris@0 25 * $loader->addPrefix('Symfony', __DIR__.'/framework');
Chris@0 26 *
Chris@0 27 * // or with a Composer autoloader
Chris@0 28 * use Composer\Autoload\ClassLoader;
Chris@0 29 *
Chris@0 30 * $loader = new ClassLoader();
Chris@0 31 * $loader->add('Symfony\Component', __DIR__.'/component');
Chris@0 32 * $loader->add('Symfony', __DIR__.'/framework');
Chris@0 33 *
Chris@0 34 * $cachedLoader = new XcacheClassLoader('my_prefix', $loader);
Chris@0 35 *
Chris@0 36 * // activate the cached autoloader
Chris@0 37 * $cachedLoader->register();
Chris@0 38 *
Chris@0 39 * // eventually deactivate the non-cached loader if it was registered previously
Chris@0 40 * // to be sure to use the cached one.
Chris@0 41 * $loader->unregister();
Chris@0 42 *
Chris@0 43 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 44 * @author Kris Wallsmith <kris@symfony.com>
Chris@0 45 * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
Chris@0 46 */
Chris@0 47 class XcacheClassLoader
Chris@0 48 {
Chris@0 49 private $prefix;
Chris@0 50
Chris@0 51 /**
Chris@0 52 * A class loader object that implements the findFile() method.
Chris@0 53 *
Chris@0 54 * @var object
Chris@0 55 */
Chris@0 56 private $decorated;
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Constructor.
Chris@0 60 *
Chris@0 61 * @param string $prefix The XCache namespace prefix to use
Chris@0 62 * @param object $decorated A class loader object that implements the findFile() method
Chris@0 63 *
Chris@0 64 * @throws \RuntimeException
Chris@0 65 * @throws \InvalidArgumentException
Chris@0 66 */
Chris@0 67 public function __construct($prefix, $decorated)
Chris@0 68 {
Chris@0 69 if (!extension_loaded('xcache')) {
Chris@0 70 throw new \RuntimeException('Unable to use XcacheClassLoader as XCache is not enabled.');
Chris@0 71 }
Chris@0 72
Chris@0 73 if (!method_exists($decorated, 'findFile')) {
Chris@0 74 throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
Chris@0 75 }
Chris@0 76
Chris@0 77 $this->prefix = $prefix;
Chris@0 78 $this->decorated = $decorated;
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * Registers this instance as an autoloader.
Chris@0 83 *
Chris@0 84 * @param bool $prepend Whether to prepend the autoloader or not
Chris@0 85 */
Chris@0 86 public function register($prepend = false)
Chris@0 87 {
Chris@0 88 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * Unregisters this instance as an autoloader.
Chris@0 93 */
Chris@0 94 public function unregister()
Chris@0 95 {
Chris@0 96 spl_autoload_unregister(array($this, 'loadClass'));
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Loads the given class or interface.
Chris@0 101 *
Chris@0 102 * @param string $class The name of the class
Chris@0 103 *
Chris@0 104 * @return bool|null True, if loaded
Chris@0 105 */
Chris@0 106 public function loadClass($class)
Chris@0 107 {
Chris@0 108 if ($file = $this->findFile($class)) {
Chris@0 109 require $file;
Chris@0 110
Chris@0 111 return true;
Chris@0 112 }
Chris@0 113 }
Chris@0 114
Chris@0 115 /**
Chris@0 116 * Finds a file by class name while caching lookups to Xcache.
Chris@0 117 *
Chris@0 118 * @param string $class A class name to resolve to file
Chris@0 119 *
Chris@0 120 * @return string|null
Chris@0 121 */
Chris@0 122 public function findFile($class)
Chris@0 123 {
Chris@0 124 if (xcache_isset($this->prefix.$class)) {
Chris@0 125 $file = xcache_get($this->prefix.$class);
Chris@0 126 } else {
Chris@0 127 $file = $this->decorated->findFile($class) ?: null;
Chris@0 128 xcache_set($this->prefix.$class, $file);
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 }