2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
2016-09-05 18:05:19 +02:00
|
|
|
require_once(__DIR__ . '/BridgeInterface.php');
|
2016-08-24 20:48:12 +02:00
|
|
|
class Bridge {
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
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){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameBridge)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$message = <<<EOD
|
2016-08-24 20:48:12 +02:00
|
|
|
'nameBridge' must start with one uppercase character followed or not by
|
|
|
|
alphanumeric or dash characters!
|
|
|
|
EOD;
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \InvalidArgumentException($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
$nameBridge = $nameBridge . 'Bridge';
|
|
|
|
$pathBridge = self::getDir() . $nameBridge . '.php';
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!file_exists($pathBridge)) {
|
2016-09-10 21:01:02 +02:00
|
|
|
throw new \Exception('The bridge you looking for does not exist. It should be at path '
|
2016-09-10 20:41:11 +02:00
|
|
|
. $pathBridge);
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once $pathBridge;
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(Bridge::isInstantiable($nameBridge)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
return new $nameBridge();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function setDir($dirBridge){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_string($dirBridge)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \InvalidArgumentException('Dir bridge must be a string.');
|
|
|
|
}
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!file_exists($dirBridge)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \Exception('Dir bridge does not exist.');
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$dirBridge = $dirBridge;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function getDir(){
|
|
|
|
$dirBridge = self::$dirBridge;
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(is_null($dirBridge)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
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);
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if($dirFiles !== false) {
|
|
|
|
foreach($dirFiles as $fileName) {
|
|
|
|
if(preg_match('@^([^.]+)Bridge\.php$@U', $fileName, $out)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$listBridge[] = $out[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $listBridge;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function isWhitelisted($whitelist, $name){
|
|
|
|
if(in_array($name, $whitelist)
|
|
|
|
|| in_array($name . '.php', $whitelist)
|
2017-08-05 23:30:24 +02:00
|
|
|
|| in_array($name . 'bridge', $whitelist) // DEPRECATED
|
|
|
|
|| in_array($name . 'bridge.php', $whitelist) // DEPRECATED
|
2017-07-29 19:28:00 +02:00
|
|
|
|| (count($whitelist) === 1 && trim($whitelist[0]) === '*')) {
|
2016-09-10 20:41:11 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-08-24 20:48:12 +02:00
|
|
|
}
|