Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / class-loader / ApcClassLoader.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__.'\ApcClassLoader 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  * ApcClassLoader implements a wrapping autoloader cached in APC 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  *     use Symfony\Component\ClassLoader\ClassLoader;
26  *
27  *     $loader = new ClassLoader();
28  *     $loader->addPrefix('Symfony\Component', __DIR__.'/component');
29  *     $loader->addPrefix('Symfony',           __DIR__.'/framework');
30  *
31  *     // or with a Composer autoloader
32  *     use Composer\Autoload\ClassLoader;
33  *
34  *     $loader = new ClassLoader();
35  *     $loader->add('Symfony\Component', __DIR__.'/component');
36  *     $loader->add('Symfony',           __DIR__.'/framework');
37  *
38  *     $cachedLoader = new ApcClassLoader('my_prefix', $loader);
39  *
40  *     // activate the cached autoloader
41  *     $cachedLoader->register();
42  *
43  *     // eventually deactivate the non-cached loader if it was registered previously
44  *     // to be sure to use the cached one.
45  *     $loader->unregister();
46  *
47  * @author Fabien Potencier <fabien@symfony.com>
48  * @author Kris Wallsmith <kris@symfony.com>
49  *
50  * @deprecated since version 3.3, to be removed in 4.0. Use `composer install --apcu-autoloader` instead.
51  */
52 class ApcClassLoader
53 {
54     private $prefix;
55
56     /**
57      * A class loader object that implements the findFile() method.
58      *
59      * @var object
60      */
61     protected $decorated;
62
63     /**
64      * @param string $prefix    The APC namespace prefix to use
65      * @param object $decorated A class loader object that implements the findFile() method
66      *
67      * @throws \RuntimeException
68      * @throws \InvalidArgumentException
69      */
70     public function __construct($prefix, $decorated)
71     {
72         if (!\function_exists('apcu_fetch')) {
73             throw new \RuntimeException('Unable to use ApcClassLoader as APC is not installed.');
74         }
75
76         if (!method_exists($decorated, 'findFile')) {
77             throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
78         }
79
80         $this->prefix = $prefix;
81         $this->decorated = $decorated;
82     }
83
84     /**
85      * Registers this instance as an autoloader.
86      *
87      * @param bool $prepend Whether to prepend the autoloader or not
88      */
89     public function register($prepend = false)
90     {
91         spl_autoload_register(array($this, 'loadClass'), true, $prepend);
92     }
93
94     /**
95      * Unregisters this instance as an autoloader.
96      */
97     public function unregister()
98     {
99         spl_autoload_unregister(array($this, 'loadClass'));
100     }
101
102     /**
103      * Loads the given class or interface.
104      *
105      * @param string $class The name of the class
106      *
107      * @return bool|null True, if loaded
108      */
109     public function loadClass($class)
110     {
111         if ($file = $this->findFile($class)) {
112             require $file;
113
114             return true;
115         }
116     }
117
118     /**
119      * Finds a file by class name while caching lookups to APC.
120      *
121      * @param string $class A class name to resolve to file
122      *
123      * @return string|null
124      */
125     public function findFile($class)
126     {
127         $file = apcu_fetch($this->prefix.$class, $success);
128
129         if (!$success) {
130             apcu_store($this->prefix.$class, $file = $this->decorated->findFile($class) ?: null);
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 }