Yaffs site version 1.1
[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 /**
15  * XcacheClassLoader implements a wrapping autoloader cached in XCache for PHP 5.3.
16  *
17  * It expects an object implementing a findFile method to find the file. This
18  * allows 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 autoloaders following this convention (the Composer one for instance).
21  *
22  *     // with a Symfony autoloader
23  *     use Symfony\Component\ClassLoader\ClassLoader;
24  *
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 class XcacheClassLoader
50 {
51     private $prefix;
52
53     /**
54      * A class loader object that implements the findFile() method.
55      *
56      * @var object
57      */
58     private $decorated;
59
60     /**
61      * Constructor.
62      *
63      * @param string $prefix    The XCache 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('xcache')) {
72             throw new \RuntimeException('Unable to use XcacheClassLoader as XCache 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 Xcache.
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         if (xcache_isset($this->prefix.$class)) {
127             $file = xcache_get($this->prefix.$class);
128         } else {
129             $file = $this->decorated->findFile($class) ?: null;
130             xcache_set($this->prefix.$class, $file);
131         }
132
133         return $file;
134     }
135
136     /**
137      * Passes through all unknown calls onto the decorated object.
138      */
139     public function __call($method, $args)
140     {
141         return call_user_func_array(array($this->decorated, $method), $args);
142     }
143 }