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