Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\View;
  4.  
  5. use Cake\View\View;
  6. use Cake\Event\Event;
  7.  
  8. class AppView extends View
  9. {
  10.  
  11. public function initialize() {
  12. $this->eventManager()->on('View.afterLayout', [$this, 'afterLayout']);
  13. }
  14.  
  15. public function afterLayout(Event $event) {
  16. $pageHtml = $event->subject()->Blocks->get('content');
  17.  
  18. $pageHtmlCompressed = $this->sanitize_output($pageHtml);
  19.  
  20. $event->subject()->Blocks->set('content', $pageHtmlCompressed);
  21. }
  22.  
  23. public function sanitize_output($buffer) {
  24.  
  25. $search = array(
  26. '/\>[^\S ]+/s', // strip whitespaces after tags, except space
  27. '/[^\S ]+\</s', // strip whitespaces before tags, except space
  28. '/(\s)+/s', // shorten multiple whitespace sequences
  29. '<!--(.*?)-->'
  30. );
  31.  
  32. $replace = array(
  33. '>',
  34. '<',
  35. '\\1'
  36. );
  37.  
  38. $buffer = preg_replace($search, $replace, $buffer);
  39. $buffer = str_replace('<>', '', $buffer);
  40.  
  41. return $buffer;
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement