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__.'\WinCacheClassLoader 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 * WinCacheClassLoader implements a wrapping autoloader cached in WinCache.
|
Chris@0
|
18 *
|
Chris@0
|
19 * It expects an object implementing a findFile method to find the file. This
|
Chris@0
|
20 * allow 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 WinCacheClassLoader('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 Artem Ryzhkov <artem@smart-core.org>
|
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 WinCacheClassLoader
|
Chris@0
|
52 {
|
Chris@0
|
53 private $prefix;
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * A class loader object that implements the findFile() method.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @var object
|
Chris@0
|
59 */
|
Chris@0
|
60 protected $decorated;
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * @param string $prefix The WinCache namespace prefix to use
|
Chris@0
|
64 * @param object $decorated A class loader object that implements the findFile() method
|
Chris@0
|
65 *
|
Chris@0
|
66 * @throws \RuntimeException
|
Chris@0
|
67 * @throws \InvalidArgumentException
|
Chris@0
|
68 */
|
Chris@0
|
69 public function __construct($prefix, $decorated)
|
Chris@0
|
70 {
|
Chris@17
|
71 if (!\extension_loaded('wincache')) {
|
Chris@0
|
72 throw new \RuntimeException('Unable to use WinCacheClassLoader as WinCache is not enabled.');
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 if (!method_exists($decorated, 'findFile')) {
|
Chris@0
|
76 throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 $this->prefix = $prefix;
|
Chris@0
|
80 $this->decorated = $decorated;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * Registers this instance as an autoloader.
|
Chris@0
|
85 *
|
Chris@0
|
86 * @param bool $prepend Whether to prepend the autoloader or not
|
Chris@0
|
87 */
|
Chris@0
|
88 public function register($prepend = false)
|
Chris@0
|
89 {
|
Chris@17
|
90 spl_autoload_register([$this, 'loadClass'], true, $prepend);
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * Unregisters this instance as an autoloader.
|
Chris@0
|
95 */
|
Chris@0
|
96 public function unregister()
|
Chris@0
|
97 {
|
Chris@17
|
98 spl_autoload_unregister([$this, 'loadClass']);
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * Loads the given class or interface.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @param string $class The name of the class
|
Chris@0
|
105 *
|
Chris@0
|
106 * @return bool|null True, if loaded
|
Chris@0
|
107 */
|
Chris@0
|
108 public function loadClass($class)
|
Chris@0
|
109 {
|
Chris@0
|
110 if ($file = $this->findFile($class)) {
|
Chris@0
|
111 require $file;
|
Chris@0
|
112
|
Chris@0
|
113 return true;
|
Chris@0
|
114 }
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * Finds a file by class name while caching lookups to WinCache.
|
Chris@0
|
119 *
|
Chris@0
|
120 * @param string $class A class name to resolve to file
|
Chris@0
|
121 *
|
Chris@0
|
122 * @return string|null
|
Chris@0
|
123 */
|
Chris@0
|
124 public function findFile($class)
|
Chris@0
|
125 {
|
Chris@0
|
126 $file = wincache_ucache_get($this->prefix.$class, $success);
|
Chris@0
|
127
|
Chris@0
|
128 if (!$success) {
|
Chris@0
|
129 wincache_ucache_set($this->prefix.$class, $file = $this->decorated->findFile($class) ?: null, 0);
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 return $file;
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Passes through all unknown calls onto the decorated object.
|
Chris@0
|
137 */
|
Chris@0
|
138 public function __call($method, $args)
|
Chris@0
|
139 {
|
Chris@17
|
140 return \call_user_func_array([$this->decorated, $method], $args);
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|