MinimifierController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. use \KarmaFW\App\ResponseRedirect;
  7. class MinimifierController extends WebAppController
  8. {
  9. public function minimifier_js($arguments=[])
  10. {
  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. return $this->response->setStatus(404)->setHtml('');
  25. //return new ResponseRedirect($file_url);
  26. }
  27. if (! is_file($file_path)) {
  28. // file not found
  29. return $this->response->setStatus(404)->setHtml('');
  30. //return new ResponseRedirect($file_url);
  31. } else {
  32. if (false) {
  33. // NO minimification
  34. readfile($file_path);
  35. } else {
  36. // minimification
  37. $content = file_get_contents($file_path);
  38. $this->response->setContent($content);
  39. $content_length = $this->response->getContentLength();
  40. $content_minimified = MinimifierJs::minify_js($content);
  41. $this->response->setContent($content_minimified);
  42. $content_minimified_length = $this->response->getContentLength();
  43. $this->response->addHeader('Content-Type', 'text/javascript');
  44. // add information headers
  45. $this->response->addHeader('X-CSS-Unminimified-Content-Length', $content_length);
  46. $this->response->addHeader('X-CSS-Minimified-Content-Length', $content_minimified_length);
  47. // TODO: gerer cache-expire, expires, ...
  48. }
  49. }
  50. } else {
  51. // Error document root not found
  52. return $this->response->setStatus(404)->setHtml('');
  53. //return new ResponseRedirect($file_url);
  54. }
  55. }
  56. public function minimifier_css($arguments=[])
  57. {
  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. return $this->response->setStatus(404)->setHtml('');
  72. //return new ResponseRedirect($file_url);
  73. }
  74. if (! is_file($file_path)) {
  75. // file not found
  76. return $this->response->setStatus(404)->setHtml('');
  77. //return new ResponseRedirect($file_url);
  78. } else {
  79. if (false) {
  80. // NO minimification
  81. readfile($file_path);
  82. } else {
  83. // minimification
  84. $content = file_get_contents($file_path);
  85. $this->response->setContent($content);
  86. $content_length = $this->response->getContentLength();
  87. $content_minimified = MinimifierCss::minify_css($content);
  88. $this->response->setContent($content_minimified);
  89. $content_minimified_length = $this->response->getContentLength();
  90. $this->response->addHeader('Content-Type', 'text/css');
  91. // add information headers
  92. $this->response->addHeader('X-CSS-Unminimified-Content-Length', $content_length);
  93. $this->response->addHeader('X-CSS-Minimified-Content-Length', $content_minimified_length);
  94. // TODO: gerer cache-expire, expires, ...
  95. }
  96. }
  97. } else {
  98. // Error document root not found
  99. return $this->response->setStatus(404)->setHtml('');
  100. //return new ResponseRedirect($file_url);
  101. }
  102. }
  103. }