PageRenderTime 34ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/clients/extensions/output_minify/output_minify.php

https://gitlab.com/vanthanhhoh/devlovebook
PHP | 87 lines | 84 code | 1 blank | 2 comment | 8 complexity | 63ba906a31678bbf42eebf04217d72cc MD5 | raw file
  1. <?php
  2. class output_minify extends BaseExtension {
  3. public function minify($tpl_output = '') {
  4. $tpl_output = Boot::$tpl_output;
  5. if(Boot::$ControllerGroup == 'frontend' . DIRECTORY_SEPARATOR) {
  6. ob_start(array($this, 'sanitize_output'));
  7. return;
  8. Boot::Library('simple_html_dom');
  9. $frontend_js_path = DATA_PATH . 'theme' . DIRECTORY_SEPARATOR . CURRENT_THEME . '/js/frontend.js';
  10. $frontend_css_path = DATA_PATH . 'theme' . DIRECTORY_SEPARATOR . CURRENT_THEME . '/css/frontend.css';
  11. $minified_script = '<script type="text/javascript" src="' . APPLICATION_DATA_DIR . 'theme/' . CURRENT_THEME . '/js/frontend.js"></script>';
  12. $minified_style = '<link rel="stylesheet" type="text/css" href="' . APPLICATION_DATA_DIR . 'theme/' . CURRENT_THEME . '/css/frontend.css" />';
  13. $init_script = '<script type="text/javascript">var BASE_DIR="' . BASE_DIR . '";var current_route="' . G::$Route['name'] . '";</script>';
  14. if(!file_exists($frontend_js_path) || !file_exists($frontend_css_path)) {
  15. //if(1) {
  16. $merged_script = $merged_style = array();
  17. $html = new simple_html_dom();
  18. $html->load($tpl_output);
  19. $path = rtrim(BASE_PATH, '\\');
  20. /*** Script ***/
  21. $scripts = $html->find('script');
  22. foreach ($scripts as $script) {
  23. if (!empty($script->src)) {
  24. $script_src = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path . $script->src);
  25. if (file_exists($script_src)) $merged_script[] = File::GetContent($script_src) . ';';
  26. $script->outertext = '';
  27. }
  28. if (!empty($script->innertext)) {
  29. if(!preg_match('/var BASE_DIR="[^\"]*";var current_route="[^\"]*";/', $script->innertext, $_m)) {
  30. $merged_script[] = $script->innertext;
  31. }
  32. $script->outertext = '';
  33. }
  34. }
  35. $merged_script = implode(';', $merged_script);
  36. /*** Css ***/
  37. $styles = $html->find('link[type="text/css"]');
  38. foreach($styles as $style) {
  39. if(!empty($style->href)) {
  40. $style_src = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path . $style->href);
  41. if (file_exists($style_src)) $merged_style[] = File::GetContent($style_src);
  42. }
  43. $style->outertext = '';
  44. }
  45. $styles = $html->find('style');
  46. foreach($styles as $style) {
  47. if(!empty($style->href)) {
  48. $merged_style[] = $style->innertext;
  49. }
  50. $style->outertext = '';
  51. }
  52. File::Create($frontend_js_path, $merged_script);
  53. File::Create($frontend_css_path, $merged_style);
  54. $body = $html->find("body", 0);
  55. $body->outertext = $body->makeup() . $body->innertext . $minified_style . $init_script . $minified_script . '</body>';
  56. $tpl_output = $html;
  57. }
  58. else {
  59. $tpl_output = preg_replace('/<script\\b[^>]*>(.*?)<\\/script>/is', "", $tpl_output);
  60. $tpl_output = preg_replace('/<link\\b[^>]+type="text\/css"[^>]+\\/>/is', '', $tpl_output);
  61. $tpl_output = preg_replace("/<style\\b[^>]*>(.*?)<\\/style>/s", "", $tpl_output);
  62. $tpl_output = str_replace('</body>', $minified_style . $init_script . $minified_script, $tpl_output);
  63. }
  64. Boot::$tpl_output = $tpl_output;
  65. unset($tpl_output);
  66. ob_start(array($this, 'sanitize_output'));
  67. }
  68. }
  69. private function sanitize_output($buffer) {
  70. $search = array(
  71. '/\>[^\S ]+/s', // strip whitespaces after tags, except space
  72. '/[^\S ]+\</s', // strip whitespaces before tags, except space
  73. '/(\s)+/s' // shorten multiple whitespace sequences
  74. );
  75. $replace = array(
  76. '>',
  77. '<',
  78. '\\1'
  79. );
  80. $buffer = preg_replace($search, $replace, $buffer);
  81. return $buffer;
  82. }
  83. public function action_flush_static_file($params = array()) {
  84. //n($params);
  85. }
  86. }