fredyounan
11/15/2014 - 6:09 AM

App::after if(App::Environment() != 'local') // HTML Minification

App::after if(App::Environment() != 'local') // HTML Minification

<?php

### --- Snip --- ###

App::after(function($request, $response)
{
	// HTML Minification
	if(App::Environment() != 'local')
	{
		if($response instanceof Illuminate\Http\Response)
		{
			$output = $response->getOriginalContent();
			
			$filters = array(
				'/<!--([^\[|(<!)].*)/'		=> '', // Remove HTML Comments (breaks with HTML5 Boilerplate)
				'/(?<!\S)\/\/\s*[^\r\n]*/'	=> '', // Remove comments in the form /* */
				'/\s{2,}/'			=> ' ', // Shorten multiple white spaces
				'/(\r?\n)/'			=> '', // Collapse new lines
			);
			
			$output = preg_replace(array_keys($filters), array_values($filters), $output);
			$response->setContent($output);
		}
	}
});

### --- Snip --- ###
<?php

### --- Snip --- ###

App::after(function($request, $response)
{
	// HTML Minification
	if(App::Environment() != 'local')
	{
		if($response instanceof Illuminate\Http\Response)
		{
			$output = $response->getOriginalContent();

			// Clean comments
			$output = preg_replace('/<!--([^\[|(<!)].*)/', '', $output);
			$output = preg_replace('/(?<!\S)\/\/\s*[^\r\n]*/', '', $output);

			// Clean Whitespace
			$output = preg_replace('/\s{2,}/', '', $output);
			$output = preg_replace('/(\r?\n)/', '', $output);

			$response->setContent($output);
		}
	}
});

### --- Snip --- ###