MinimifierController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace KarmaFW\Controllers;
  3. use \KarmaFW\Routing\Controllers\WebAppController;
  4. use \KarmaFW\App\Middlewares\MinimifierJs;
  5. use \KarmaFW\App\Middlewares\MinimifierCss;
  6. class MinimifierController extends WebAppController
  7. {
  8. public function minimifier_js($arguments=[])
  9. {
  10. //pre($arguments, 1);
  11. $file_url = $arguments['file_url'];
  12. $document_root = APP_DIR . '/public';
  13. if (! is_dir($document_root)) {
  14. if (! empty($_SERVER['DOCUMENT_ROOT'])) {
  15. $document_root = realpath($_SERVER['DOCUMENT_ROOT']);
  16. } else {
  17. $document_root = '';
  18. }
  19. }
  20. if ($document_root) {
  21. $file_path = $document_root . $file_url;
  22. if ($file_path != realpath($file_path) || substr($file_path, -3) != '.js') {
  23. // file path invalid or not a js file
  24. redirect($file_url);
  25. /*
  26. header('HTTP/1.1 404 Not Found');
  27. echo "ERROR 404";
  28. exit;
  29. */
  30. }
  31. if (! is_file($file_path)) {
  32. // file not found
  33. redirect($file_url);
  34. /*
  35. header('HTTP/1.1 404 Not Found');
  36. echo "ERROR 404";
  37. exit;
  38. */
  39. } else {
  40. header('Content-type: text/javascript');
  41. //if (ENV == 'dev' && ! (defined('MINIMIFY_JS') && MINIMIFY_JS ) ) {
  42. if (false) {
  43. // NO minimification
  44. readfile($file_path);
  45. } else {
  46. // minimification
  47. $content = file_get_contents($file_path);
  48. $content = MinimifierJs::minify_js($content);
  49. echo $content;
  50. // TODO: gerer cache-expire, expires, ...
  51. }
  52. }
  53. }
  54. }
  55. public function minimifier_css($arguments=[])
  56. {
  57. //pre($arguments, 1);
  58. $file_url = $arguments['file_url'];
  59. $document_root = APP_DIR . '/public';
  60. if (! is_dir($document_root)) {
  61. if (! empty($_SERVER['DOCUMENT_ROOT'])) {
  62. $document_root = realpath($_SERVER['DOCUMENT_ROOT']);
  63. } else {
  64. $document_root = '';
  65. }
  66. }
  67. if ($document_root) {
  68. $file_path = $document_root . $file_url;
  69. if ($file_path != realpath($file_path) || substr($file_path, -4) != '.css') {
  70. // file path invalid or not a css file
  71. redirect($file_url);
  72. /*
  73. header('HTTP/1.1 404 Not Found');
  74. echo "ERROR 404";
  75. exit;
  76. */
  77. }
  78. if (! is_file($file_path)) {
  79. // file not found
  80. redirect($file_url);
  81. /*
  82. header('HTTP/1.1 404 Not Found');
  83. echo "ERROR 404";
  84. exit;
  85. */
  86. } else {
  87. header('Content-type: text/css');
  88. if (false) {
  89. // NO minimification
  90. readfile($file_path);
  91. } else {
  92. // minimification
  93. $content = file_get_contents($file_path);
  94. $content = MinimifierCss::minify_css($content);
  95. echo $content;
  96. // TODO: gerer cache-expire, expires, ...
  97. }
  98. }
  99. } else {
  100. // Error document root not found
  101. redirect($file_url);
  102. }
  103. }
  104. protected function redirectToUrl($url)
  105. {
  106. $continue_url = "";
  107. redirect($continue_url);
  108. //return new ResponseRedirect($continue_url, 302);
  109. }
  110. }