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