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