Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * process creating html tag parent class
  5. *
  6. * @author Hiroyuki Takai <watashitakai@gmail.com>
  7. * @since 2019/06/
  8. */
  9.  
  10. namespace framework2\lib\html_view;
  11.  
  12. use framework2\lib\html_view\CreateTagStr;
  13. use framework2\lib\html_view\HtmlDts;
  14.  
  15.  
  16. abstract class CreateHtml {
  17.  
  18. const TEMPFILE_DIR = SYSROOT .'/'. APPNAME . '/views/mytemplate/';
  19. const VIEW_CACHE_DIR = self::TEMPFILE_DIR. "cache/";
  20.  
  21. //replacement mark
  22. const TITLE = "#title#";
  23. const PAGE = "#id_name#";
  24.  
  25. protected $templete;
  26. protected $title;
  27. protected $id_name; # for page id name
  28. private $parts_dts = array(array());
  29.  
  30.  
  31. /**
  32. * construct
  33. *
  34. * @param string temp_name -->template file name without extension
  35. * htmlDts dt
  36. */
  37. public function __construct(string $temp_name, htmlDts $dt)
  38. {
  39. try {
  40. if (!is_file(self::TEMPFILE_DIR .$temp_name.'.php')) {
  41. $msg = 'can\'t read file: ' . $temp_name;
  42. throw new BadRequestException($msg, BADREQUEST);
  43. }
  44. } catch(BadRequestException $e) {
  45. ErrorFunc::catchAfter($e);
  46. }
  47.  
  48. $this->template = $temp_name;
  49. $this->title = $dt->getTitle();
  50. $this->id_name = $dt->getName();
  51. if (defined('NOCACHE')) {
  52. $this->cacheFree();
  53. }
  54. $this->parts_dts = $dt->getDts(); # third array demension
  55. }
  56.  
  57.  
  58. /**
  59. * cache free processing use uniq id name
  60. *
  61. * @return string
  62. */
  63. protected function cacheFree() : string
  64. {
  65. //但しAPPNAME/views/template dir/cacheのファイル数が増えてくるので注意。
  66. $this->id_name = date("His").$this->id_name;
  67.  
  68. return $this->id_name;
  69. }
  70.  
  71.  
  72. /**
  73. * return template file path
  74. *
  75. * @return string file path
  76. */
  77. protected function retTempFilePath() : string
  78. {
  79. $path = self::TEMPFILE_DIR. $this->template. '.php';
  80.  
  81. return $path;
  82. }
  83.  
  84.  
  85. /**
  86. * create cache file name
  87. *
  88. * @param string $path
  89. * @return string path
  90. */
  91. protected function createCacheFilename(string $path) : string
  92. {
  93. $file = self::VIEW_CACHE_DIR .$this->id_name .sha1_file($path);
  94.  
  95. return $file;
  96. }
  97.  
  98.  
  99. /**
  100. * get connect html parts data by block and build up for each block html statement
  101. *
  102. * @retrun array contents
  103. */
  104. public function getCreatehtmls() : array
  105. {
  106. $html_strs = $this->factory_createTagStr();
  107. $contents = array_fill(0, count($html_strs), null);
  108.  
  109. for ($i=0; $i<count($html_strs); $i++) {
  110. for ($j=0; $j<count($html_strs[$i]); $j++) {
  111. if (!empty($html_strs[$i][$j])) {
  112. $contents[$i] = $contents[$i].$html_strs[$i][$j];
  113. }
  114. }
  115. }
  116. return $contents;
  117. }
  118.  
  119.  
  120. /**
  121. * createTagStr object factory and get html parts
  122. *
  123. * @param array dts
  124. * @return third demension array of html parts
  125. */
  126. public function factory_createTagStr (array $dts=null) : array
  127. {
  128. if (empty($dts)) {
  129. $obj = new CreateTagStr($this->parts_dts);
  130. } else {
  131. $obj = new CreateTagStr ($dts); # for append
  132. }
  133.  
  134. return $obj->create_htmlstr();
  135. }
  136.  
  137.  
  138. /**
  139. * it is necesary to define child class that is inheritance used this class
  140. */
  141. abstract function view();
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement