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