/app/controllers/components/jlm_packager.php

https://github.com/ButuzGOL/inside_wildflowercms · PHP · 166 lines · 111 code · 24 blank · 31 comment · 9 complexity · a56adfe90517271809735ca665717381 MD5 · raw file

  1. <?php
  2. class JlmPackagerComponent {
  3. public $wfJlmDir;
  4. public $appJlmDir;
  5. /** Files from these directories make it to the generated file */
  6. private $_mvcDirs = array(
  7. 'vendors',
  8. 'controllers/components',
  9. 'controllers'
  10. );
  11. private $_initializedViews = array();
  12. function startup() {
  13. $this->wfJlmDir = APP . 'jlm';
  14. if (!is_dir($this->wfJlmDir)) trigger_error('/wildflower/jlm directory does not exist!');
  15. $this->appJlmDir = '';
  16. }
  17. function l18n($string) {
  18. function translate($pregArray) {
  19. return __($pregArray[1], true);
  20. }
  21. return preg_replace_callback("#<l18n>(.+?)</l18n>#is", 'translate', $string);
  22. }
  23. // @depracated Output is handled by Cake action
  24. function output() {
  25. header('Content-type: application/javascript');
  26. echo $this->concate();
  27. }
  28. /**
  29. * Send headers to browser to cache the current request
  30. *
  31. * @param string $file Path to a file
  32. */
  33. function browserCacheHeaders($modifiedTime, $type = 'application/javascript') {
  34. header("Date: " . date("D, j M Y G:i:s ", $modifiedTime) . 'GMT');
  35. header('Content-type: ' . $type);
  36. header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT");
  37. header("Cache-Control: cache");
  38. header("Pragma: cache");
  39. }
  40. /**
  41. * Concate all files into one string
  42. *
  43. * @return string
  44. */
  45. function concate() {
  46. $output = '';
  47. // Append lib files in this order
  48. $libDir = $this->wfJlmDir . DS . 'lib' . DS;
  49. $output .= $this->readFile($libDir . 'jquery.js');
  50. $output .= $this->readFile($libDir . 'functions.js');
  51. $output .= $this->readFile($libDir . 'trimpath-template.js');
  52. $output .= $this->readFile($libDir . 'jquery.jlm.js');
  53. // First load Wildflower templates
  54. $viewsPath = $this->wfJlmDir . DS . 'views';
  55. $output .= $this->readTemplates($viewsPath);
  56. // Add App templates
  57. $appViewsPath = $this->appJlmDir . DS . 'views';
  58. $output .= $this->readTemplates($appViewsPath);
  59. // Load other MVC dirs
  60. foreach ($this->_mvcDirs as $dir) {
  61. $wfDirPath = $this->wfJlmDir . DS . $dir;
  62. $output .= $this->readMvcFiles($wfDirPath);
  63. $appDirPath = $this->appJlmDir . DS . $dir;
  64. $output .= $this->readMvcFiles($appDirPath);
  65. }
  66. // Translate
  67. $output = $this->l18n($output);
  68. return $output;
  69. }
  70. /**
  71. * Returns string of .js concated files
  72. *
  73. * @param string $dirPath
  74. * @return string
  75. */
  76. function readMvcFiles($dirPath) {
  77. if (!is_dir($dirPath)) {
  78. return '';
  79. }
  80. $output = '';
  81. $files = scandir($dirPath);
  82. foreach ($files as $file) {
  83. $ext = substr($file, -3);
  84. if ($ext !== '.js') {
  85. continue;
  86. }
  87. $path = $dirPath . DS . $file;
  88. $output .= $this->readFile($path);
  89. }
  90. return $output;
  91. }
  92. /**
  93. * Returns string of templates
  94. *
  95. * @param string $viewsPath
  96. * @return string
  97. */
  98. function readTemplates($viewsPath) {
  99. if (!is_dir($viewsPath)) {
  100. return '';
  101. }
  102. $viewDirs = scandir($viewsPath);
  103. $output = '';
  104. foreach ($viewDirs as $dir) {
  105. if ($dir[0] == '.') {
  106. continue;
  107. }
  108. // Init specific template array
  109. if (!in_array($dir, $this->_initializedViews)) {
  110. $output .= "jQuery.jlm.templates['$dir'] = [];\n";
  111. $this->_initializedViews[] = $dir;
  112. }
  113. $viewDirPath = $viewsPath . DS . $dir;
  114. if (!is_dir($viewDirPath)) {
  115. continue;
  116. }
  117. $files = scandir($viewDirPath);
  118. foreach ($files as $file) {
  119. $ext = substr($file, -5);
  120. if ($ext !== '.html') {
  121. continue;
  122. }
  123. $path = $viewDirPath . DS . $file;
  124. $template = $this->readFile($path, false);
  125. // Escape some chars
  126. $template = str_replace(array("\n", "\r", "'"), array('', '', "\'"), $template);
  127. $templateName = str_replace('.html', '', $file);
  128. $varName = "jQuery.jlm.templates['$dir']['$templateName']";
  129. $output .= "$varName = '$template';\n";
  130. }
  131. }
  132. return $output;
  133. }
  134. function readFile($path) {
  135. $output = "\n";
  136. $output .= file_get_contents($path);
  137. return $output;
  138. }
  139. }