bug #11768 [ClassLoader] Add a __call() method to XcacheClassLoader (tstoeckler)

This PR was squashed before being merged into the 2.3 branch (closes #11768).

Discussion
----------

[ClassLoader] Add a __call() method to XcacheClassLoader

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #11733
| License       | MIT
| Doc PR        |

Commits
-------

dd0d6af [ClassLoader] Add a __call() method to XcacheClassLoader
This commit is contained in:
Fabien Potencier 2014-08-28 16:33:47 +02:00
commit f7769b52f1
2 changed files with 27 additions and 13 deletions

View File

@ -15,7 +15,7 @@ namespace Symfony\Component\ClassLoader;
* ApcClassLoader implements a wrapping autoloader cached in APC for PHP 5.3. * ApcClassLoader implements a wrapping autoloader cached in APC for PHP 5.3.
* *
* It expects an object implementing a findFile method to find the file. This * It expects an object implementing a findFile method to find the file. This
* allow using it as a wrapper around the other loaders of the component (the * allows using it as a wrapper around the other loaders of the component (the
* ClassLoader and the UniversalClassLoader for instance) but also around any * ClassLoader and the UniversalClassLoader for instance) but also around any
* other autoloader following this convention (the Composer one for instance) * other autoloader following this convention (the Composer one for instance)
* *
@ -46,7 +46,7 @@ class ApcClassLoader
/** /**
* The class loader object being decorated. * The class loader object being decorated.
* *
* @var \Symfony\Component\ClassLoader\ClassLoader * @var object
* A class loader object that implements the findFile() method. * A class loader object that implements the findFile() method.
*/ */
protected $decorated; protected $decorated;
@ -133,5 +133,4 @@ class ApcClassLoader
{ {
return call_user_func_array(array($this->decorated, $method), $args); return call_user_func_array(array($this->decorated, $method), $args);
} }
} }

View File

@ -12,7 +12,7 @@
namespace Symfony\Component\ClassLoader; namespace Symfony\Component\ClassLoader;
/** /**
* XcacheClassLoader implements a wrapping autoloader cached in Xcache for PHP 5.3. * XcacheClassLoader implements a wrapping autoloader cached in XCache for PHP 5.3.
* *
* It expects an object implementing a findFile method to find the file. This * It expects an object implementing a findFile method to find the file. This
* allows using it as a wrapper around the other loaders of the component (the * allows using it as a wrapper around the other loaders of the component (the
@ -43,31 +43,38 @@ namespace Symfony\Component\ClassLoader;
class XcacheClassLoader class XcacheClassLoader
{ {
private $prefix; private $prefix;
private $classFinder;
/**
* The class loader object being decorated.
*
* @var object
* A class loader object that implements the findFile() method.
*/
private $decorated;
/** /**
* Constructor. * Constructor.
* *
* @param string $prefix A prefix to create a namespace in Xcache * @param string $prefix The XCache namespace prefix to use.
* @param object $classFinder An object that implements findFile() method. * @param object $decorated A class loader object that implements the findFile() method.
* *
* @throws \RuntimeException * @throws \RuntimeException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* *
* @api * @api
*/ */
public function __construct($prefix, $classFinder) public function __construct($prefix, $decorated)
{ {
if (!extension_loaded('Xcache')) { if (!extension_loaded('xcache')) {
throw new \RuntimeException('Unable to use XcacheClassLoader as Xcache is not enabled.'); throw new \RuntimeException('Unable to use XcacheClassLoader as XCache is not enabled.');
} }
if (!method_exists($classFinder, 'findFile')) { if (!method_exists($decorated, 'findFile')) {
throw new \InvalidArgumentException('The class finder must implement a "findFile" method.'); throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
} }
$this->prefix = $prefix; $this->prefix = $prefix;
$this->classFinder = $classFinder; $this->decorated = $decorated;
} }
/** /**
@ -116,10 +123,18 @@ class XcacheClassLoader
if (xcache_isset($this->prefix.$class)) { if (xcache_isset($this->prefix.$class)) {
$file = xcache_get($this->prefix.$class); $file = xcache_get($this->prefix.$class);
} else { } else {
$file = $this->classFinder->findFile($class); $file = $this->decorated->findFile($class);
xcache_set($this->prefix.$class, $file); xcache_set($this->prefix.$class, $file);
} }
return $file; return $file;
} }
/**
* Passes through all unknown calls onto the decorated object.
*/
public function __call($method, $args)
{
return call_user_func_array(array($this->decorated, $method), $args);
}
} }