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__.'\XcacheClassLoader 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 * XcacheClassLoader implements a wrapping autoloader cached in XCache 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 * $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 XcacheClassLoader('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 * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
|
Chris@14
|
48 *
|
Chris@14
|
49 * @deprecated since version 3.3, to be removed in 4.0. Use `composer install --apcu-autoloader` instead.
|
Chris@0
|
50 */
|
Chris@0
|
51 class XcacheClassLoader
|
Chris@0
|
52 {
|
Chris@0
|
53 private $prefix;
|
Chris@0
|
54 private $decorated;
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * @param string $prefix The XCache namespace prefix to use
|
Chris@0
|
58 * @param object $decorated A class loader object that implements the findFile() method
|
Chris@0
|
59 *
|
Chris@0
|
60 * @throws \RuntimeException
|
Chris@0
|
61 * @throws \InvalidArgumentException
|
Chris@0
|
62 */
|
Chris@0
|
63 public function __construct($prefix, $decorated)
|
Chris@0
|
64 {
|
Chris@17
|
65 if (!\extension_loaded('xcache')) {
|
Chris@0
|
66 throw new \RuntimeException('Unable to use XcacheClassLoader as XCache is not enabled.');
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 if (!method_exists($decorated, 'findFile')) {
|
Chris@0
|
70 throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 $this->prefix = $prefix;
|
Chris@0
|
74 $this->decorated = $decorated;
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Registers this instance as an autoloader.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @param bool $prepend Whether to prepend the autoloader or not
|
Chris@0
|
81 */
|
Chris@0
|
82 public function register($prepend = false)
|
Chris@0
|
83 {
|
Chris@17
|
84 spl_autoload_register([$this, 'loadClass'], true, $prepend);
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * Unregisters this instance as an autoloader.
|
Chris@0
|
89 */
|
Chris@0
|
90 public function unregister()
|
Chris@0
|
91 {
|
Chris@17
|
92 spl_autoload_unregister([$this, 'loadClass']);
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 /**
|
Chris@0
|
96 * Loads the given class or interface.
|
Chris@0
|
97 *
|
Chris@0
|
98 * @param string $class The name of the class
|
Chris@0
|
99 *
|
Chris@0
|
100 * @return bool|null True, if loaded
|
Chris@0
|
101 */
|
Chris@0
|
102 public function loadClass($class)
|
Chris@0
|
103 {
|
Chris@0
|
104 if ($file = $this->findFile($class)) {
|
Chris@0
|
105 require $file;
|
Chris@0
|
106
|
Chris@0
|
107 return true;
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * Finds a file by class name while caching lookups to Xcache.
|
Chris@0
|
113 *
|
Chris@0
|
114 * @param string $class A class name to resolve to file
|
Chris@0
|
115 *
|
Chris@0
|
116 * @return string|null
|
Chris@0
|
117 */
|
Chris@0
|
118 public function findFile($class)
|
Chris@0
|
119 {
|
Chris@0
|
120 if (xcache_isset($this->prefix.$class)) {
|
Chris@0
|
121 $file = xcache_get($this->prefix.$class);
|
Chris@0
|
122 } else {
|
Chris@0
|
123 $file = $this->decorated->findFile($class) ?: null;
|
Chris@0
|
124 xcache_set($this->prefix.$class, $file);
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 return $file;
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 /**
|
Chris@0
|
131 * Passes through all unknown calls onto the decorated object.
|
Chris@0
|
132 */
|
Chris@0
|
133 public function __call($method, $args)
|
Chris@0
|
134 {
|
Chris@17
|
135 return \call_user_func_array([$this->decorated, $method], $args);
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|