Bridge.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. require_once(__DIR__ . '/BridgeInterface.php');
  3. class Bridge {
  4. static protected $dirBridge;
  5. public function __construct(){
  6. throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
  7. }
  8. /**
  9. * Checks if a bridge is an instantiable bridge.
  10. * @param string $nameBridge name of the bridge that you want to use
  11. * @return true if it is an instantiable bridge, false otherwise.
  12. */
  13. static public function isInstantiable($nameBridge){
  14. $re = new ReflectionClass($nameBridge);
  15. return $re->IsInstantiable();
  16. }
  17. /**
  18. * Create a new bridge object
  19. * @param string $nameBridge Defined bridge name you want use
  20. * @return Bridge object dedicated
  21. */
  22. static public function create($nameBridge){
  23. if(!preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameBridge)){
  24. $message = <<<EOD
  25. 'nameBridge' must start with one uppercase character followed or not by
  26. alphanumeric or dash characters!
  27. EOD;
  28. throw new \InvalidArgumentException($message);
  29. }
  30. $nameBridge = $nameBridge . 'Bridge';
  31. $pathBridge = self::getDir() . $nameBridge . '.php';
  32. if(!file_exists($pathBridge)){
  33. throw new \Exception('The bridge you looking for does not exist. It should be at path '
  34. . $pathBridge);
  35. }
  36. require_once $pathBridge;
  37. if(Bridge::isInstantiable($nameBridge)){
  38. return new $nameBridge();
  39. } else {
  40. return false;
  41. }
  42. }
  43. static public function setDir($dirBridge){
  44. if(!is_string($dirBridge)){
  45. throw new \InvalidArgumentException('Dir bridge must be a string.');
  46. }
  47. if(!file_exists($dirBridge)){
  48. throw new \Exception('Dir bridge does not exist.');
  49. }
  50. self::$dirBridge = $dirBridge;
  51. }
  52. static public function getDir(){
  53. $dirBridge = self::$dirBridge;
  54. if(is_null($dirBridge)){
  55. throw new \LogicException(__CLASS__ . ' class need to know bridge path !');
  56. }
  57. return $dirBridge;
  58. }
  59. /**
  60. * Lists the available bridges.
  61. * @return array List of the bridges
  62. */
  63. static public function listBridges(){
  64. $pathDirBridge = self::getDir();
  65. $listBridge = array();
  66. $dirFiles = scandir($pathDirBridge);
  67. if($dirFiles !== false){
  68. foreach($dirFiles as $fileName){
  69. if(preg_match('@^([^.]+)Bridge\.php$@U', $fileName, $out)){
  70. $listBridge[] = $out[1];
  71. }
  72. }
  73. }
  74. return $listBridge;
  75. }
  76. static public function isWhitelisted($whitelist, $name){
  77. if(in_array($name, $whitelist)
  78. || in_array($name . '.php', $whitelist)
  79. || in_array($name . 'Bridge', $whitelist) // DEPRECATED
  80. || in_array($name . 'Bridge.php', $whitelist) // DEPRECATED
  81. || (count($whitelist) === 1 && trim($whitelist[0]) === '*')){
  82. return true;
  83. } else {
  84. return false;
  85. }
  86. }
  87. }