Mercurial > hg > isophonics-drupal-site
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 * XcacheClassLoader implements a wrapping autoloader cached in XCache for PHP 5.3. | |
16 * | |
17 * It expects an object implementing a findFile method to find the file. This | |
18 * allows using it as a wrapper around the other loaders of the component (the | |
19 * ClassLoader for instance) but also around any other autoloaders following | |
20 * this convention (the Composer one for instance). | |
21 * | |
22 * // with a Symfony autoloader | |
23 * $loader = new ClassLoader(); | |
24 * $loader->addPrefix('Symfony\Component', __DIR__.'/component'); | |
25 * $loader->addPrefix('Symfony', __DIR__.'/framework'); | |
26 * | |
27 * // or with a Composer autoloader | |
28 * use Composer\Autoload\ClassLoader; | |
29 * | |
30 * $loader = new ClassLoader(); | |
31 * $loader->add('Symfony\Component', __DIR__.'/component'); | |
32 * $loader->add('Symfony', __DIR__.'/framework'); | |
33 * | |
34 * $cachedLoader = new XcacheClassLoader('my_prefix', $loader); | |
35 * | |
36 * // activate the cached autoloader | |
37 * $cachedLoader->register(); | |
38 * | |
39 * // eventually deactivate the non-cached loader if it was registered previously | |
40 * // to be sure to use the cached one. | |
41 * $loader->unregister(); | |
42 * | |
43 * @author Fabien Potencier <fabien@symfony.com> | |
44 * @author Kris Wallsmith <kris@symfony.com> | |
45 * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com> | |
46 */ | |
47 class XcacheClassLoader | |
48 { | |
49 private $prefix; | |
50 | |
51 /** | |
52 * A class loader object that implements the findFile() method. | |
53 * | |
54 * @var object | |
55 */ | |
56 private $decorated; | |
57 | |
58 /** | |
59 * Constructor. | |
60 * | |
61 * @param string $prefix The XCache namespace prefix to use | |
62 * @param object $decorated A class loader object that implements the findFile() method | |
63 * | |
64 * @throws \RuntimeException | |
65 * @throws \InvalidArgumentException | |
66 */ | |
67 public function __construct($prefix, $decorated) | |
68 { | |
69 if (!extension_loaded('xcache')) { | |
70 throw new \RuntimeException('Unable to use XcacheClassLoader as XCache is not enabled.'); | |
71 } | |
72 | |
73 if (!method_exists($decorated, 'findFile')) { | |
74 throw new \InvalidArgumentException('The class finder must implement a "findFile" method.'); | |
75 } | |
76 | |
77 $this->prefix = $prefix; | |
78 $this->decorated = $decorated; | |
79 } | |
80 | |
81 /** | |
82 * Registers this instance as an autoloader. | |
83 * | |
84 * @param bool $prepend Whether to prepend the autoloader or not | |
85 */ | |
86 public function register($prepend = false) | |
87 { | |
88 spl_autoload_register(array($this, 'loadClass'), true, $prepend); | |
89 } | |
90 | |
91 /** | |
92 * Unregisters this instance as an autoloader. | |
93 */ | |
94 public function unregister() | |
95 { | |
96 spl_autoload_unregister(array($this, 'loadClass')); | |
97 } | |
98 | |
99 /** | |
100 * Loads the given class or interface. | |
101 * | |
102 * @param string $class The name of the class | |
103 * | |
104 * @return bool|null True, if loaded | |
105 */ | |
106 public function loadClass($class) | |
107 { | |
108 if ($file = $this->findFile($class)) { | |
109 require $file; | |
110 | |
111 return true; | |
112 } | |
113 } | |
114 | |
115 /** | |
116 * Finds a file by class name while caching lookups to Xcache. | |
117 * | |
118 * @param string $class A class name to resolve to file | |
119 * | |
120 * @return string|null | |
121 */ | |
122 public function findFile($class) | |
123 { | |
124 if (xcache_isset($this->prefix.$class)) { | |
125 $file = xcache_get($this->prefix.$class); | |
126 } else { | |
127 $file = $this->decorated->findFile($class) ?: null; | |
128 xcache_set($this->prefix.$class, $file); | |
129 } | |
130 | |
131 return $file; | |
132 } | |
133 | |
134 /** | |
135 * Passes through all unknown calls onto the decorated object. | |
136 */ | |
137 public function __call($method, $args) | |
138 { | |
139 return call_user_func_array(array($this->decorated, $method), $args); | |
140 } | |
141 } |