Format.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * All format logic
  4. * Note : adapter are store in other place
  5. */
  6. interface FormatInterface{
  7. public function stringify();
  8. public function display();
  9. public function setDatas(array $bridge);
  10. }
  11. abstract class FormatAbstract implements FormatInterface{
  12. const DEFAULT_CHARSET = 'UTF-8';
  13. protected
  14. $contentType,
  15. $charset,
  16. $datas,
  17. $extraInfos
  18. ;
  19. public function setCharset($charset){
  20. $this->charset = $charset;
  21. return $this;
  22. }
  23. public function getCharset(){
  24. $charset = $this->charset;
  25. return is_null($charset) ? self::DEFAULT_CHARSET : $charset;
  26. }
  27. protected function setContentType($contentType){
  28. $this->contentType = $contentType;
  29. return $this;
  30. }
  31. protected function callContentType(){
  32. header('Content-Type: ' . $this->contentType);
  33. }
  34. public function display(){
  35. echo $this->stringify();
  36. return $this;
  37. }
  38. public function setDatas(array $datas){
  39. $this->datas = $datas;
  40. return $this;
  41. }
  42. public function getDatas(){
  43. if( !is_array($this->datas) ){
  44. throw new \LogicException('Feed the ' . get_class($this) . ' with "setDatas" method before !');
  45. }
  46. return $this->datas;
  47. }
  48. /**
  49. * Define common informations can be required by formats and set default value for unknow values
  50. * @param array $extraInfos array with know informations (there isn't merge !!!)
  51. * @return this
  52. */
  53. public function setExtraInfos(array $extraInfos = array()){
  54. foreach(array('name', 'uri') as $infoName){
  55. if( !isset($extraInfos[$infoName]) ){
  56. $extraInfos[$infoName] = '';
  57. }
  58. }
  59. $this->extraInfos = $extraInfos;
  60. return $this;
  61. }
  62. /**
  63. * Return extra infos
  64. * @return array See "setExtraInfos" detail method to know what extra are disponibles
  65. */
  66. public function getExtraInfos(){
  67. if( is_null($this->extraInfos) ){ // No extra info ?
  68. $this->setExtraInfos(); // Define with default value
  69. }
  70. return $this->extraInfos;
  71. }
  72. }
  73. class Format{
  74. static protected $dirFormat;
  75. public function __construct(){
  76. throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
  77. }
  78. static public function create($nameFormat){
  79. if( !static::isValidNameFormat($nameFormat) ){
  80. throw new \InvalidArgumentException('Name format must be at least one uppercase follow or not by alphabetic characters.');
  81. }
  82. $pathFormat = self::getDir() . $nameFormat . '.php';
  83. if( !file_exists($pathFormat) ){
  84. throw new \Exception('The format you looking for does not exist.');
  85. }
  86. require_once $pathFormat;
  87. return new $nameFormat();
  88. }
  89. static public function setDir($dirFormat){
  90. if( !is_string($dirFormat) ){
  91. throw new \InvalidArgumentException('Dir format must be a string.');
  92. }
  93. if( !file_exists($dirFormat) ){
  94. throw new \Exception('Dir format does not exist.');
  95. }
  96. self::$dirFormat = $dirFormat;
  97. }
  98. static public function getDir(){
  99. $dirFormat = self::$dirFormat;
  100. if( is_null($dirFormat) ){
  101. throw new \LogicException(__CLASS__ . ' class need to know format path !');
  102. }
  103. return $dirFormat;
  104. }
  105. static public function isValidNameFormat($nameFormat){
  106. return preg_match('@^[A-Z][a-zA-Z]*$@', $nameFormat);
  107. }
  108. /**
  109. * Read format dir and catch informations about each format depending annotation
  110. * @return array Informations about each format
  111. */
  112. static public function searchInformation(){
  113. $pathDirFormat = self::getDir();
  114. $listFormat = array();
  115. $searchCommonPattern = array('name');
  116. $dirFiles = scandir($pathDirFormat);
  117. if( $dirFiles !== false ){
  118. foreach( $dirFiles as $fileName ){
  119. if( preg_match('@([^.]+)\.php@U', $fileName, $out) ){ // Is PHP file ?
  120. $infos = array(); // Information about the bridge
  121. $resParse = token_get_all(file_get_contents($pathDirFormat . $fileName)); // Parse PHP file
  122. foreach($resParse as $v){
  123. if( is_array($v) && $v[0] == T_DOC_COMMENT ){ // Lexer node is COMMENT ?
  124. $commentary = $v[1];
  125. foreach( $searchCommonPattern as $name){ // Catch information with common pattern
  126. preg_match('#@' . preg_quote($name, '#') . '\s+(.+)#', $commentary, $outComment);
  127. if( isset($outComment[1]) ){
  128. $infos[$name] = $outComment[1];
  129. }
  130. }
  131. }
  132. }
  133. if( isset($infos['name']) ){ // If informations containt at least a name
  134. $listFormat[$out[1]] = $infos;
  135. }
  136. }
  137. }
  138. }
  139. return $listFormat;
  140. }
  141. }