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