[Bridge] Move 'Bridge' class at top of the file

This commit is contained in:
logmanoriginal 2016-08-24 20:48:12 +02:00
parent b9b2428f63
commit 95404b8fc4

View file

@ -1,4 +1,107 @@
<?php <?php
class Bridge {
static protected $dirBridge;
public function __construct(){
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
}
/**
* Checks if a bridge is an instantiable bridge.
* @param string $nameBridge name of the bridge that you want to use
* @return true if it is an instantiable bridge, false otherwise.
*/
static public function isInstantiable($nameBridge){
$re = new ReflectionClass($nameBridge);
return $re->IsInstantiable();
}
/**
* Create a new bridge object
* @param string $nameBridge Defined bridge name you want use
* @return Bridge object dedicated
*/
static public function create($nameBridge){
if(!preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameBridge)){
$message = <<<EOD
'nameBridge' must start with one uppercase character followed or not by
alphanumeric or dash characters!
EOD;
throw new \InvalidArgumentException($message);
}
$nameBridge = $nameBridge . 'Bridge';
$pathBridge = self::getDir() . $nameBridge . '.php';
if(!file_exists($pathBridge)){
throw new \Exception('The bridge you looking for does not exist. It should be at path ' . $pathBridge);
}
require_once $pathBridge;
if(Bridge::isInstantiable($nameBridge)){
return new $nameBridge();
} else {
return false;
}
}
static public function setDir($dirBridge){
if(!is_string($dirBridge)){
throw new \InvalidArgumentException('Dir bridge must be a string.');
}
if(!file_exists($dirBridge)){
throw new \Exception('Dir bridge does not exist.');
}
self::$dirBridge = $dirBridge;
}
static public function getDir(){
$dirBridge = self::$dirBridge;
if(is_null($dirBridge)){
throw new \LogicException(__CLASS__ . ' class need to know bridge path !');
}
return $dirBridge;
}
/**
* Lists the available bridges.
* @return array List of the bridges
*/
static public function listBridges(){
$pathDirBridge = self::getDir();
$listBridge = array();
$dirFiles = scandir($pathDirBridge);
if($dirFiles !== false){
foreach($dirFiles as $fileName){
if(preg_match('@^([^.]+)Bridge\.php$@U', $fileName, $out)){
$listBridge[] = $out[1];
}
}
}
return $listBridge;
}
static function isWhitelisted($whitelist, $name){
if(in_array($name, $whitelist)
or in_array($name . '.php', $whitelist)
or in_array($name . 'Bridge', $whitelist) // DEPRECATED
or in_array($name . 'Bridge.php', $whitelist) // DEPRECATED
or count($whitelist) === 1 and trim($whitelist[0]) === '*'){
return true;
} else {
return false;
}
}
}
interface BridgeInterface { interface BridgeInterface {
public function collectData(array $param); public function collectData(array $param);
public function getCacheDuration(); public function getCacheDuration();
@ -243,109 +346,6 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
} }
} }
class Bridge {
static protected $dirBridge;
public function __construct(){
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
}
/**
* Checks if a bridge is an instantiable bridge.
* @param string $nameBridge name of the bridge that you want to use
* @return true if it is an instantiable bridge, false otherwise.
*/
static public function isInstantiable($nameBridge){
$re = new ReflectionClass($nameBridge);
return $re->IsInstantiable();
}
/**
* Create a new bridge object
* @param string $nameBridge Defined bridge name you want use
* @return Bridge object dedicated
*/
static public function create($nameBridge){
if(!preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameBridge)){
$message = <<<EOD
'nameBridge' must start with one uppercase character followed or not by
alphanumeric or dash characters!
EOD;
throw new \InvalidArgumentException($message);
}
$nameBridge = $nameBridge . 'Bridge';
$pathBridge = self::getDir() . $nameBridge . '.php';
if(!file_exists($pathBridge)){
throw new \Exception('The bridge you looking for does not exist. It should be at path ' . $pathBridge);
}
require_once $pathBridge;
if(Bridge::isInstantiable($nameBridge)){
return new $nameBridge();
} else {
return false;
}
}
static public function setDir($dirBridge){
if(!is_string($dirBridge)){
throw new \InvalidArgumentException('Dir bridge must be a string.');
}
if(!file_exists($dirBridge)){
throw new \Exception('Dir bridge does not exist.');
}
self::$dirBridge = $dirBridge;
}
static public function getDir(){
$dirBridge = self::$dirBridge;
if(is_null($dirBridge)){
throw new \LogicException(__CLASS__ . ' class need to know bridge path !');
}
return $dirBridge;
}
/**
* Lists the available bridges.
* @return array List of the bridges
*/
static public function listBridges(){
$pathDirBridge = self::getDir();
$listBridge = array();
$dirFiles = scandir($pathDirBridge);
if($dirFiles !== false){
foreach($dirFiles as $fileName){
if(preg_match('@^([^.]+)Bridge\.php$@U', $fileName, $out)){
$listBridge[] = $out[1];
}
}
}
return $listBridge;
}
static function isWhitelisted($whitelist, $name){
if(in_array($name, $whitelist)
or in_array($name . '.php', $whitelist)
or in_array($name . 'Bridge', $whitelist) // DEPRECATED
or in_array($name . 'Bridge.php', $whitelist) // DEPRECATED
or count($whitelist) === 1 and trim($whitelist[0]) === '*'){
return true;
} else {
return false;
}
}
}
abstract class RssExpander extends HttpCachingBridgeAbstract { abstract class RssExpander extends HttpCachingBridgeAbstract {
public function collectExpandableDatas(array $param, $name){ public function collectExpandableDatas(array $param, $name){