git-svn-id: http://78.47.251.156/svn/dev/sterntours-3@3283 f459cee4-fb09-11de-96c3-f9c5f16c3c76

This commit is contained in:
uli 2016-12-17 10:11:28 +00:00
parent 75a065758f
commit 7422f06e90
261 changed files with 83347 additions and 0 deletions

View file

@ -0,0 +1,118 @@
<?php
/**
* @author Ulrich Hecht <ulrich.hecht@hecht-software.de>
* @date 12/01/2016
*/
namespace AppBundle;
class RedisClassLoader
{
private $prefix;
/**
* A class loader object that implements the findFile() method.
*
* @var object
*/
private $decorated;
/**
* @var null|\Redis
*/
private $redis = null;
/**
* Constructor.
*
* @param string $prefix The XCache namespace prefix to use
* @param object $decorated A class loader object that implements the findFile() method
*
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function __construct($prefix, $decorated)
{
if (extension_loaded('redis')) {
$this->redis = new \Redis();
$this->redis->connect('127.0.0.1');
}
if (!method_exists($decorated, 'findFile')) {
throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
}
$this->prefix = $prefix;
$this->decorated = $decorated;
}
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend Whether to prepend the autoloader or not
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
*
* @return bool|null True, if loaded
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
require $file;
return true;
}
}
/**
* Finds a file by class name while caching lookups to Xcache.
*
* @param string $class A class name to resolve to file
*
* @return string|null
*/
public function findFile($class)
{
if ($this->redis === null)
{
return $this->decorated->findFile($class) ?: null;
}
if ($this->redis->exists($this->prefix.$class))
{
$file = $this->redis->get($this->prefix.$class);
}
else
{
$file = $this->decorated->findFile($class) ?: null;
$this->redis->set($this->prefix.$class, $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);
}
}