-
-
Save su-narthur/1930e2e34042317e169016ded74c738a to your computer and use it in GitHub Desktop.
Silex minify HTML middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$app->after(function (\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\HttpFoundation\Response $response) use ($app) { | |
if ($app['debug']) return; | |
$search = array( | |
'#\/\*.+?\*\/#', // /* JS Comments */ | |
'#^\s+\/\/.+#m', // // JS Comments | |
'/<!--.+?-->/', // <!-- HTML Comments --> | |
'/\n/', | |
'/\>[^\S ]+/s', | |
'/[^\S ]+\</s', | |
'/(\s)+/s' | |
); | |
$replace = array( | |
'', | |
'', | |
'', | |
' ', | |
'>', | |
'<', | |
'\\1' | |
); | |
$minified_content = preg_replace($search, $replace, $response->getContent()); | |
$response->setContent($minified_content); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment