Version 1
[yaffs-website] / vendor / symfony / class-loader / ApcUniversalClassLoader.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__.'\ApcUniversalClassLoader class is deprecated since version 2.7 and will be removed in 3.0. Use the Symfony\Component\ClassLoader\ApcClassLoader class instead.', E_USER_DEPRECATED);
15
16 /**
17  * ApcUniversalClassLoader implements a "universal" autoloader cached in APC for PHP 5.3.
18  *
19  * It is able to load classes that use either:
20  *
21  *  * The technical interoperability standards for PHP 5.3 namespaces and
22  *    class names (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md);
23  *
24  *  * The PEAR naming convention for classes (http://pear.php.net/).
25  *
26  * Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be
27  * looked for in a list of locations to ease the vendoring of a sub-set of
28  * classes for large projects.
29  *
30  * Example usage:
31  *
32  *     require 'vendor/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
33  *     require 'vendor/symfony/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php';
34  *
35  *     use Symfony\Component\ClassLoader\ApcUniversalClassLoader;
36  *
37  *     $loader = new ApcUniversalClassLoader('apc.prefix.');
38  *
39  *     // register classes with namespaces
40  *     $loader->registerNamespaces(array(
41  *         'Symfony\Component' => __DIR__.'/component',
42  *         'Symfony' => __DIR__.'/framework',
43  *         'Sensio' => array(__DIR__.'/src', __DIR__.'/vendor'),
44  *     ));
45  *
46  *     // register a library using the PEAR naming convention
47  *     $loader->registerPrefixes(array(
48  *         'Swift_' => __DIR__.'/Swift',
49  *     ));
50  *
51  *     // activate the autoloader
52  *     $loader->register();
53  *
54  * In this example, if you try to use a class in the Symfony\Component
55  * namespace or one of its children (Symfony\Component\Console for instance),
56  * the autoloader will first look for the class under the component/
57  * directory, and it will then fallback to the framework/ directory if not
58  * found before giving up.
59  *
60  * @author Fabien Potencier <fabien@symfony.com>
61  * @author Kris Wallsmith <kris@symfony.com>
62  *
63  * @deprecated since version 2.4, to be removed in 3.0.
64  *             Use the {@link ClassLoader} class instead.
65  */
66 class ApcUniversalClassLoader extends UniversalClassLoader
67 {
68     private $prefix;
69
70     /**
71      * Constructor.
72      *
73      * @param string $prefix A prefix to create a namespace in APC
74      *
75      * @throws \RuntimeException
76      */
77     public function __construct($prefix)
78     {
79         if (!function_exists('apcu_fetch')) {
80             throw new \RuntimeException('Unable to use ApcUniversalClassLoader as APC is not enabled.');
81         }
82
83         $this->prefix = $prefix;
84     }
85
86     /**
87      * Finds a file by class name while caching lookups to APC.
88      *
89      * @param string $class A class name to resolve to file
90      *
91      * @return string|null The path, if found
92      */
93     public function findFile($class)
94     {
95         $file = apcu_fetch($this->prefix.$class, $success);
96
97         if (!$success) {
98             apcu_store($this->prefix.$class, $file = parent::findFile($class) ?: null);
99         }
100
101         return $file;
102     }
103 }