PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/latte/latte/php/Document.php

https://bitbucket.org/GIBravo/materialdesign
PHP | 246 lines | 105 code | 36 blank | 105 comment | 8 complexity | db2d72f6c5fcc2364894908aa501148f MD5 | raw file
  1. <?php
  2. /**
  3. * Represents an HTML document
  4. */
  5. class Document {
  6. /**
  7. * The document's &lt;!DOCTYPE&gt; tag
  8. *
  9. * @var Tag
  10. */
  11. public $doctype;
  12. /**
  13. * The document's &lt;html&gt; tag
  14. *
  15. * @var Tag
  16. */
  17. public $html;
  18. /**
  19. * The document's &lt;head&gt; tag
  20. *
  21. * @var Tag
  22. */
  23. public $head;
  24. /**
  25. * The document's &lt;title&gt; tag
  26. *
  27. * @var Tag
  28. */
  29. public $title;
  30. /**
  31. * The document's &lt;body&gt; tag
  32. *
  33. * @var Tag
  34. */
  35. public $body;
  36. /**
  37. * Function to call when rendering document
  38. *
  39. * @var function
  40. */
  41. public $onRender;
  42. /**
  43. * If <c>true</c>, document will be rendered to output before deallocated from memory.
  44. *
  45. * @var boolean
  46. */
  47. public $outputRender;
  48. /**
  49. * If set to true, will add Latte module tags when rendering
  50. *
  51. * @var boolean
  52. */
  53. public $addLatteTags;
  54. /**
  55. * Creates the document. Optionally speifies if document should be rendered to output when deallocated from memory.
  56. *
  57. * @param boolean $output
  58. */
  59. function __construct($output = false, $addLatteTags = false) {
  60. // Create basic tags
  61. $this->doctype = new Tag("!doctype");
  62. $this->doctype->html = NULL;
  63. $this->html = tag("html");
  64. $this->head = tag("head")->addTo($this->html);
  65. $this->title = tag("title")->addTo($this->head)->text("New Document");
  66. $this->body = tag("body")->addTo($this->html);
  67. //Set content type
  68. $contentType = tag("meta")
  69. ->addTo($this->head)
  70. ->attr("http-equiv", "Content-Type")
  71. ->attr("content", "text/html; charset=UTF-8");
  72. // Make globals available
  73. $GLOBALS['body'] = $this->body;
  74. $GLOBALS['head'] = $this->head;
  75. // Initialize events
  76. $this->onRender = array();
  77. // Flag to indicate if document goes to output
  78. $this->outputRender = $output;
  79. // Flag to indicate if document should add latte tags
  80. $this->addLatteTags = $addLatteTags;
  81. }
  82. /**
  83. * If <c>outputRender</c>, renders the document to the output
  84. */
  85. function __destruct() {
  86. if ($this->outputRender) {
  87. $this->render();
  88. }
  89. }
  90. /**
  91. * Renders the document
  92. *
  93. * @return string
  94. */
  95. function __toString() {
  96. return $this->render();
  97. }
  98. /**
  99. * Renders the document to the output or returns it as a <c>string</c>
  100. *
  101. * @param boolean $return
  102. * @return string
  103. */
  104. public function render($return = false) {
  105. if($this->addLatteTags){
  106. /// Add latte tags
  107. foreach(LatteModule::$loadedModules as $module){
  108. $this->head->add($module->getTags());
  109. if($module->isMain){
  110. $main = isset($module->metadata['ua-main']) ? $module->metadata['ua-main'] : "latte.Main";
  111. // Loader module
  112. $this->addScript(" window.addEventListener('load', function(){ new $main() });");
  113. }
  114. }
  115. }
  116. foreach ($this->onRender as $function) {
  117. if (is_callable($function)) {
  118. $function($this);
  119. } else {
  120. $this->body->add($function);
  121. }
  122. }
  123. if ($this->outputRender && !$return) {
  124. echo $this->doctype->render();
  125. echo $this->html->render();
  126. } else {
  127. return $this->doctype->render() . $this->html->render();
  128. }
  129. }
  130. /**
  131. * Adds Google Analytics 'pageview
  132. * @param $uaid
  133. */
  134. public function addGoogleAnalyticsPageView($uaid, $property){
  135. $this->addScript("
  136. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  137. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  138. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  139. })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  140. ga('create', '$uaid', '$property');
  141. ga('send', 'pageview');
  142. ");
  143. }
  144. /**
  145. * Adds JavaScript code to the head of the document
  146. *
  147. * @param string $jsCode
  148. * @param bool $minify Minifies the script (Removes tabs, line feeds and excesive white spaces)
  149. * @return Tag
  150. */
  151. public function addScript($jsCode, $minify = true) {
  152. $jsCode = str_replace("\r", "", $jsCode);
  153. $jsCode = str_replace("\n", "", $jsCode);
  154. $jsCode = str_replace("\t", "", $jsCode);
  155. $jsCode = preg_replace('/\s\s+/', ' ', $jsCode);
  156. return tag("script")
  157. ->attr("type", "text/javascript")
  158. //->text("\r\n<!-- \r\n $jsCode \r\n//-->\r\n")
  159. ->text("\r\n $jsCode \r\n")
  160. ->addTo($this->body);
  161. }
  162. /**
  163. * Adds CSS code to the head of the document
  164. *
  165. * @param string $style
  166. * @return Tag
  167. */
  168. public function addStyle($style) {
  169. $style = str_replace("\r", "", $style);
  170. $style = str_replace("\n", "", $style);
  171. $style = str_replace("\t", "", $style);
  172. $style = preg_replace('/\s\s+/', ' ', $style);
  173. return tag("style")
  174. ->attr("type", "text/css")
  175. ->text("\r\n $style \r\n")
  176. ->addTo($this->head);
  177. }
  178. /**
  179. * Adds a CSS stylesheet to the document
  180. *
  181. * @param string $url
  182. * @return Tag
  183. */
  184. public function addCss($url) {
  185. return tag("link")
  186. ->attr("rel", "stylesheet")
  187. ->attr("href", $url)
  188. ->addTo($this->head);
  189. }
  190. /**
  191. * Adds a JavaScript file to the document
  192. *
  193. * @param string $url
  194. * @return Tag
  195. */
  196. public function addJs($url) {
  197. return tag("script")
  198. ->attr("type", "text/javascript")
  199. ->attr("src", $url)
  200. ->addTo($this->head);
  201. }
  202. /**
  203. * Adds a handler to the onRender event
  204. *
  205. * @param function $function
  206. */
  207. function onRender($function) {
  208. $this->onRender[] = $function;
  209. }
  210. }