Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\View;
- use Cake\View\View;
- use Cake\Event\Event;
- class AppView extends View
- {
- public function initialize() {
- $this->eventManager()->on('View.afterLayout', [$this, 'afterLayout']);
- }
- public function afterLayout(Event $event) {
- $pageHtml = $event->subject()->Blocks->get('content');
- $pageHtmlCompressed = $this->sanitize_output($pageHtml);
- $event->subject()->Blocks->set('content', $pageHtmlCompressed);
- }
- public function sanitize_output($buffer) {
- $search = array(
- '/\>[^\S ]+/s', // strip whitespaces after tags, except space
- '/[^\S ]+\</s', // strip whitespaces before tags, except space
- '/(\s)+/s', // shorten multiple whitespace sequences
- '<!--(.*?)-->'
- );
- $replace = array(
- '>',
- '<',
- '\\1'
- );
- $buffer = preg_replace($search, $replace, $buffer);
- $buffer = str_replace('<>', '', $buffer);
- return $buffer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement