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.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-08-05 23:58:18 +02:00
|
|
|
if((new ReflectionClass($nameBridge))->isInstantiable()) {
|
2016-09-10 20:41:11 +02:00
|
|
|
return new $nameBridge();
|
|
|
|
}
|
2017-08-05 23:58:18 +02:00
|
|
|
|
|
|
|
return false;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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(){
|
2017-08-05 23:58:18 +02:00
|
|
|
if(is_null(self::$dirBridge)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \LogicException(__CLASS__ . ' class need to know bridge path !');
|
|
|
|
}
|
|
|
|
|
2017-08-05 23:58:18 +02:00
|
|
|
return self::$dirBridge;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists the available bridges.
|
|
|
|
* @return array List of the bridges
|
|
|
|
*/
|
|
|
|
static public function listBridges(){
|
|
|
|
$listBridge = array();
|
2017-08-05 23:58:18 +02:00
|
|
|
$dirFiles = scandir(self::getDir());
|
2016-09-10 20:41:11 +02:00
|
|
|
|
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){
|
2017-08-05 23:58:18 +02:00
|
|
|
return in_array($name, $whitelist)
|
2016-09-10 20:41:11 +02:00
|
|
|
|| 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-08-05 23:58:18 +02:00
|
|
|
|| (count($whitelist) === 1 && trim($whitelist[0]) === '*');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2016-08-24 20:48:12 +02:00
|
|
|
}
|