Bridge.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * All bridge logic
  4. * Note : adapter are store in other place
  5. */
  6. interface BridgeInterface{
  7. public function collectData(array $param);
  8. public function getName();
  9. public function getURI();
  10. public function getCacheDuration();
  11. }
  12. abstract class BridgeAbstract implements BridgeInterface{
  13. protected $cache;
  14. protected $items = array();
  15. /**
  16. * Launch probative exception
  17. */
  18. protected function returnError($message, $code){
  19. throw new \HttpException($message, $code);
  20. }
  21. /**
  22. * Return datas store in the bridge
  23. * @return mixed
  24. */
  25. public function getDatas(){
  26. return $this->items;
  27. }
  28. /**
  29. * Defined datas with parameters depending choose bridge
  30. * Note : you can defined a cache before with "setCache"
  31. * @param array $param $_REQUEST, $_GET, $_POST, or array with bridge expected paramters
  32. */
  33. public function setDatas(array $param){
  34. if( !is_null($this->cache) ){
  35. $this->cache->prepare($param);
  36. $time = $this->cache->getTime();
  37. }
  38. else{
  39. $time = false; // No cache ? No time !
  40. }
  41. if( $time !== false && ( time() - $this->getCacheDuration() < $time ) ){ // Cache file has not expired. Serve it.
  42. $this->items = $this->cache->loadData();
  43. }
  44. else{
  45. $this->collectData($param);
  46. if( !is_null($this->cache) ){ // Cache defined ? We go to refresh is memory :D
  47. $this->cache->saveData($this->getDatas());
  48. }
  49. }
  50. }
  51. /**
  52. * Define default duraction for cache
  53. */
  54. public function getCacheDuration(){
  55. return 3600;
  56. }
  57. /**
  58. * Defined cache object to use
  59. */
  60. public function setCache(\CacheAbstract $cache){
  61. $this->cache = $cache;
  62. return $this;
  63. }
  64. }
  65. class Bridge{
  66. static protected $dirBridge;
  67. public function __construct(){
  68. throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
  69. }
  70. /**
  71. * Create a new bridge object
  72. * @param string $nameBridge Defined bridge name you want use
  73. * @return Bridge object dedicated
  74. */
  75. static public function create($nameBridge){
  76. if( !static::isValidNameBridge($nameBridge) ){
  77. throw new \InvalidArgumentException('Name bridge must be at least one uppercase follow or not by alphanumeric or dash characters.');
  78. }
  79. $pathBridge = self::getDir() . $nameBridge . '.php';
  80. if( !file_exists($pathBridge) ){
  81. throw new \Exception('The bridge you looking for does not exist.');
  82. }
  83. require_once $pathBridge;
  84. return new $nameBridge();
  85. }
  86. static public function setDir($dirBridge){
  87. if( !is_string($dirBridge) ){
  88. throw new \InvalidArgumentException('Dir bridge must be a string.');
  89. }
  90. if( !file_exists($dirBridge) ){
  91. throw new \Exception('Dir bridge does not exist.');
  92. }
  93. self::$dirBridge = $dirBridge;
  94. }
  95. static public function getDir(){
  96. $dirBridge = self::$dirBridge;
  97. if( is_null($dirBridge) ){
  98. throw new \LogicException(__CLASS__ . ' class need to know bridge path !');
  99. }
  100. return $dirBridge;
  101. }
  102. static public function isValidNameBridge($nameBridge){
  103. return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameBridge);
  104. }
  105. /**
  106. * Read bridge dir and catch informations about each bridge depending annotation
  107. * @return array Informations about each bridge
  108. */
  109. static public function searchInformation(){
  110. $pathDirBridge = self::getDir();
  111. $listBridge = array();
  112. $searchCommonPattern = array('description', 'name');
  113. $dirFiles = scandir($pathDirBridge);
  114. if( $dirFiles !== false ){
  115. foreach( $dirFiles as $fileName ){
  116. if( preg_match('@([^.]+)\.php@U', $fileName, $out) ){ // Is PHP file ?
  117. $infos = array(); // Information about the bridge
  118. $resParse = token_get_all(file_get_contents($pathDirBridge . $fileName)); // Parse PHP file
  119. foreach($resParse as $v){
  120. if( is_array($v) && $v[0] == T_DOC_COMMENT ){ // Lexer node is COMMENT ?
  121. $commentary = $v[1];
  122. foreach( $searchCommonPattern as $name){ // Catch information with common pattern
  123. preg_match('#@' . preg_quote($name, '#') . '\s+(.+)#', $commentary, $outComment);
  124. if( isset($outComment[1]) ){
  125. $infos[$name] = $outComment[1];
  126. }
  127. }
  128. preg_match_all('#@use(?<num>[1-9][0-9]*)\s?\((?<args>.+)\)(?:\r|\n)#', $commentary, $outComment); // Catch specific information about "use".
  129. if( isset($outComment['args']) && is_array($outComment['args']) ){
  130. $infos['use'] = array();
  131. foreach($outComment['args'] as $num => $args){ // Each use
  132. preg_match_all('#(?<name>[a-z]+)="(?<value>.*)"(?:,|$)#U', $args, $outArg); // Catch arguments for current use
  133. if( isset($outArg['name']) ){
  134. $usePos = $outComment['num'][$num]; // Current use name
  135. if( !isset($infos['use'][$usePos]) ){ // Not information actually for this "use" ?
  136. $infos['use'][$usePos] = array();
  137. }
  138. foreach($outArg['name'] as $numArg => $name){ // Each arguments
  139. $infos['use'][$usePos][$name] = $outArg['value'][$numArg];
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. if( isset($infos['name']) ){ // If informations containt at least a name
  147. $listBridge[$out[1]] = $infos;
  148. }
  149. }
  150. }
  151. }
  152. return $listBridge;
  153. }
  154. }