Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 146 lines (131 sloc) 4.041 kb
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
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 /**
3acea26 @tstoeckler [ClassLoader] Add a __call() method to XcacheClassLoader
tstoeckler authored
15 * XcacheClassLoader implements a wrapping autoloader cached in XCache for PHP 5.3.
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
16 *
17 * It expects an object implementing a findFile method to find the file. This
011ec1a @kimhemsoe Fixed spelling error
kimhemsoe authored
18 * allows using it as a wrapper around the other loaders of the component (the
2ae376a @hhamon [ClassLoader] removed deprecated UniversalClassLoader and DebugClassLoad...
hhamon authored
19 * ClassLoader for instance) but also around any other autoloaders following
20 * this convention (the Composer one for instance).
731d5a4 @hhamon [ClassLoader] removes deprecated classes from documentation.
hhamon authored
21 *
22 * // with a Symfony autoloader
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
23 * $loader = new ClassLoader();
731d5a4 @hhamon [ClassLoader] removes deprecated classes from documentation.
hhamon authored
24 * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
25 * $loader->addPrefix('Symfony', __DIR__.'/framework');
26 *
27 * // or with a Composer autoloader
28 * use Composer\Autoload\ClassLoader;
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
29 *
731d5a4 @hhamon [ClassLoader] removes deprecated classes from documentation.
hhamon authored
30 * $loader = new ClassLoader();
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
31 * $loader->add('Symfony\Component', __DIR__.'/component');
32 * $loader->add('Symfony', __DIR__.'/framework');
33 *
34 * $cachedLoader = new XcacheClassLoader('my_prefix', $loader);
35 *
36 * // activate the cached autoloader
37 * $cachedLoader->register();
38 *
39 * // eventually deactivate the non-cached loader if it was registered previously
40 * // to be sure to use the cached one.
41 * $loader->unregister();
42 *
43 * @author Fabien Potencier <fabien@symfony.com>
44 * @author Kris Wallsmith <kris@symfony.com>
45 * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
46 *
47 * @api
48 */
49 class XcacheClassLoader
50 {
51 private $prefix;
3acea26 @tstoeckler [ClassLoader] Add a __call() method to XcacheClassLoader
tstoeckler authored
52
53 /**
2f8a6f9 @GrahamCampbell [2.3] CS Fixes
GrahamCampbell authored
54 * A class loader object that implements the findFile() method.
55 *
56 * @var object
3acea26 @tstoeckler [ClassLoader] Add a __call() method to XcacheClassLoader
tstoeckler authored
57 */
58 private $decorated;
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
59
60 /**
61 * Constructor.
62 *
307598c @GrahamCampbell Docblock fixes
GrahamCampbell authored
63 * @param string $prefix The XCache namespace prefix to use.
64 * @param object $decorated A class loader object that implements the findFile() method.
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
65 *
18fab53 @dlsniper Fixed most of the docblocks/unused namespaces
dlsniper authored
66 * @throws \RuntimeException
67 * @throws \InvalidArgumentException
68 *
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
69 * @api
70 */
3acea26 @tstoeckler [ClassLoader] Add a __call() method to XcacheClassLoader
tstoeckler authored
71 public function __construct($prefix, $decorated)
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
72 {
3acea26 @tstoeckler [ClassLoader] Add a __call() method to XcacheClassLoader
tstoeckler authored
73 if (!extension_loaded('xcache')) {
74 throw new \RuntimeException('Unable to use XcacheClassLoader as XCache is not enabled.');
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
75 }
76
3acea26 @tstoeckler [ClassLoader] Add a __call() method to XcacheClassLoader
tstoeckler authored
77 if (!method_exists($decorated, 'findFile')) {
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
78 throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
79 }
80
81 $this->prefix = $prefix;
3acea26 @tstoeckler [ClassLoader] Add a __call() method to XcacheClassLoader
tstoeckler authored
82 $this->decorated = $decorated;
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
83 }
84
85 /**
86 * Registers this instance as an autoloader.
87 *
307598c @GrahamCampbell Docblock fixes
GrahamCampbell authored
88 * @param bool $prepend Whether to prepend the autoloader or not
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
89 */
90 public function register($prepend = false)
91 {
92 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
93 }
94
95 /**
96 * Unregisters this instance as an autoloader.
97 */
98 public function unregister()
99 {
100 spl_autoload_unregister(array($this, 'loadClass'));
101 }
102
103 /**
104 * Loads the given class or interface.
105 *
106 * @param string $class The name of the class
4e509e3 @gajdaw [2.1][Component][ClassLoader] cs
gajdaw authored
107 *
307598c @GrahamCampbell Docblock fixes
GrahamCampbell authored
108 * @return bool|null True, if loaded
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
109 */
110 public function loadClass($class)
111 {
112 if ($file = $this->findFile($class)) {
113 require $file;
4e509e3 @gajdaw [2.1][Component][ClassLoader] cs
gajdaw authored
114
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
115 return true;
116 }
117 }
118
119 /**
9b210d7 @kimhemsoe Removed leftover from c/p
kimhemsoe authored
120 * Finds a file by class name while caching lookups to Xcache.
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
121 *
122 * @param string $class A class name to resolve to file
123 *
124 * @return string|null
125 */
126 public function findFile($class)
127 {
128 if (xcache_isset($this->prefix.$class)) {
129 $file = xcache_get($this->prefix.$class);
130 } else {
3acea26 @tstoeckler [ClassLoader] Add a __call() method to XcacheClassLoader
tstoeckler authored
131 $file = $this->decorated->findFile($class);
8a70d19 @fabpot fixed various inconsistencies
fabpot authored
132 xcache_set($this->prefix.$class, $file);
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
133 }
134
135 return $file;
136 }
3acea26 @tstoeckler [ClassLoader] Add a __call() method to XcacheClassLoader
tstoeckler authored
137
138 /**
139 * Passes through all unknown calls onto the decorated object.
140 */
141 public function __call($method, $args)
142 {
143 return call_user_func_array(array($this->decorated, $method), $args);
144 }
ad72bdb @kimhemsoe Updated to new cache loader pattern.
kimhemsoe authored
145 }
Something went wrong with that request. Please try again.