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