瀏覽代碼

dev 2020-10

Max F 5 年之前
父節點
當前提交
322e80ebc5

+ 0 - 2
src/App/Middlewares/MinimifierCss.php

@@ -13,8 +13,6 @@ class MinimifierCss
     {
         $response = $next($request, $response);
 
-        // TODO: reperer les balises <link href=...> dans le dom, récupérer le css correspondant, le minimifier et le lui créer un id de minification
-
         return $response;
     }
 

+ 66 - 13
src/App/Middlewares/MinimifierHtml.php

@@ -8,28 +8,81 @@ use \KarmaFW\App\Response;
 
 class MinimifierHtml
 {
+    protected $minimify_html;
+    protected $minimify_external_js;
+    protected $minimify_external_css;
+
     
+    public function __construct($minimify_html=true, $minimify_external_js=true, $minimify_external_css=true)
+    {
+        $this->minimify_html = $minimify_html;
+        $this->minimify_external_js = $minimify_external_js;
+        $this->minimify_external_css = $minimify_external_css;
+    }
+
+
     public function __invoke(Request $request, Response $response, callable $next)
     {
         $response = $next($request, $response);
 
-        if (false) {
-            // modify css link files
+        
+        $content_type = $response->getContentType();
+        $content_type_short = explode(';', $content_type)[0];
+
+        if ($content_type_short !== 'text/html') {
+            return $response;
+        }
+
+
+        if ($this->minimify_external_js || $this->minimify_external_css) {
+            // modification à la volée des liens CSS & JS
             $content = $response->getContent();
-            preg_match_all('#<link [^>]+"([^>]).css"#', $content, $regs);
-            pre($regs, 1);
-            $js_files = $regs[1];
+
+            if ($this->minimify_external_css) {
+                // modify CSS link files in HTML content
+                preg_match_all('#<link[^>]+"(/assets/css/[^">]+.css)"[^>]*>#', $content, $regs);
+                $css_files = $regs[1];
+                $suffix = '.phpmin.css';
+                
+                foreach ($css_files as $css_file) {
+                    if (substr($css_file, -8) != '.min.css' && substr($css_file, -11) != '.phpmin.css') {
+                        $replacement = '\1' . $css_file . $suffix . '\2';
+                        $content = preg_replace('#(<link [^>]+")' . preg_quote($css_file) . '("[^>]*>)#', $replacement, $content);
+                    }
+                }
+            }
+
+            if ($this->minimify_external_js) {
+                // modify JS link files in HTML content
+                preg_match_all('#<script[^>]+"(/assets/js/[^">]+.js)"[^>]*>#', $content, $regs);
+                $js_files = $regs[1];
+                $suffix = '.phpmin.js';
+                
+                foreach ($js_files as $js_file) {
+                    if (substr($js_file, -7) != '.min.js' && substr($js_file, -10) != '.phpmin.js') {
+                        $replacement = '\1' . $js_file . $suffix . '\2';
+                        $content = preg_replace('#(<script[^>]+")' . preg_quote($js_file) . '("[^>]*>)#', $replacement, $content);
+                    }
+                }
+            }
+
+            $response->setContent($content);
         }
 
-        // minimify html
-        $initial_content_length = $response->getContentLength();
-        $content_minimified = self::minify_html($response->getContent());
-        $response->setContent($content_minimified);
-        $final_content_length = $response->getContentLength();
 
-        // add information headers
-        $response->addHeader('X-Unminimified-Content-Length', $initial_content_length);
-        $response->addHeader('X-Minimified-Content-Length', $final_content_length);
+        if ($this->minimify_html) {
+            // minimify HTML
+            $content = $response->getContent();
+            $content_length = $response->getContentLength();
+
+            $content_minimified = self::minify_html($content);
+            $response->setContent($content_minimified);
+            $content_minimified_length = $response->getContentLength();
+
+            // add information headers
+            $response->addHeader('X-HTML-Unminimified-Content-Length', $content_length);
+            $response->addHeader('X-HTML-Minimified-Content-Length', $content_minimified_length);
+        }
 
         return $response;
     }

+ 0 - 2
src/App/Middlewares/MinimifierJs.php

@@ -13,8 +13,6 @@ class MinimifierJs
     {
         $response = $next($request, $response);
 
-        // TODO: reperer les balises <script src=...> dans le dom, récupérer le css correspondant, le minimifier et le lui créer un id de minification
-
         return $response;
     }
 

+ 42 - 45
src/Controllers/MinimifierController.php

@@ -6,14 +6,15 @@ use \KarmaFW\Routing\Controllers\WebAppController;
 use \KarmaFW\App\Middlewares\MinimifierJs;
 use \KarmaFW\App\Middlewares\MinimifierCss;
 
+use \KarmaFW\App\ResponseRedirect;
+use \KarmaFW\App\ResponseError404;
+
 
 class MinimifierController extends WebAppController
 {
 	
 	public function minimifier_js($arguments=[])
 	{
-		//pre($arguments, 1);
-
 		$file_url = $arguments['file_url'];
 
 		$document_root = APP_DIR . '/public';
@@ -32,27 +33,16 @@ class MinimifierController extends WebAppController
 
 			if ($file_path != realpath($file_path) || substr($file_path, -3) != '.js') {
 				// file path invalid or not a js file
-				redirect($file_url);
-				/*
-				header('HTTP/1.1 404 Not Found');
-				echo "ERROR 404";
-				exit;
-				*/
+				return new ResponseError404;
+				//return new ResponseRedirect($file_url);
 			}
 
 			if (! is_file($file_path)) {
 				// file not found
-				redirect($file_url);
-				/*
-				header('HTTP/1.1 404 Not Found');
-				echo "ERROR 404";
-				exit;
-				*/
+				return new ResponseError404;
+				//return new ResponseRedirect($file_url);
 
 			} else {
-				header('Content-type: text/javascript');
-
-				//if (ENV == 'dev' && ! (defined('MINIMIFY_JS') && MINIMIFY_JS ) ) {
 				if (false) {
 					// NO minimification
 					readfile($file_path);
@@ -60,20 +50,33 @@ class MinimifierController extends WebAppController
 				} else {
 					// minimification
 					$content = file_get_contents($file_path);
-					$content = MinimifierJs::minify_js($content);
-					echo $content;
+					$this->response->setContent($content);
+		            $content_length = $this->response->getContentLength();
+
+					$content_minimified = MinimifierJs::minify_js($content);
+					$this->response->setContent($content_minimified);
+		            $content_minimified_length = $this->response->getContentLength();
+					
+					$this->response->addHeader('Content-Type', 'text/javascript');
+
+		            // add information headers
+		            $this->response->addHeader('X-CSS-Unminimified-Content-Length', $content_length);
+		            $this->response->addHeader('X-CSS-Minimified-Content-Length', $content_minimified_length);
 
 					// TODO: gerer cache-expire, expires, ...
 				}
 			}
+
+		} else {
+			// Error document root not found
+			return new ResponseError404;
+			//return new ResponseRedirect($file_url);
 		}
 	}
 
 	
 	public function minimifier_css($arguments=[])
 	{
-		//pre($arguments, 1);
-
 		$file_url = $arguments['file_url'];
 
 		$document_root = APP_DIR . '/public';
@@ -92,26 +95,16 @@ class MinimifierController extends WebAppController
 
 			if ($file_path != realpath($file_path) || substr($file_path, -4) != '.css') {
 				// file path invalid or not a css file
-				redirect($file_url);
-				/*
-				header('HTTP/1.1 404 Not Found');
-				echo "ERROR 404";
-				exit;
-				*/
+				return new ResponseError404;
+				//return new ResponseRedirect($file_url);
 			}
 
 			if (! is_file($file_path)) {
 				// file not found
-				redirect($file_url);
-				/*
-				header('HTTP/1.1 404 Not Found');
-				echo "ERROR 404";
-				exit;
-				*/
+				return new ResponseError404;
+				//return new ResponseRedirect($file_url);
 
 			} else {
-				header('Content-type: text/css');
-
 				if (false) {
 					// NO minimification
 					readfile($file_path);
@@ -119,8 +112,18 @@ class MinimifierController extends WebAppController
 				} else {
 					// minimification
 					$content = file_get_contents($file_path);
-					$content = MinimifierCss::minify_css($content);
-					echo $content;
+					$this->response->setContent($content);
+		            $content_length = $this->response->getContentLength();
+
+					$content_minimified = MinimifierCss::minify_css($content);
+					$this->response->setContent($content_minimified);
+		            $content_minimified_length = $this->response->getContentLength();
+					
+					$this->response->addHeader('Content-Type', 'text/css');
+
+		            // add information headers
+		            $this->response->addHeader('X-CSS-Unminimified-Content-Length', $content_length);
+		            $this->response->addHeader('X-CSS-Minimified-Content-Length', $content_minimified_length);
 
 					// TODO: gerer cache-expire, expires, ...
 				}
@@ -128,15 +131,9 @@ class MinimifierController extends WebAppController
 
 		} else {
 			// Error document root not found
-			redirect($file_url);
+			return new ResponseError404;
+			//return new ResponseRedirect($file_url);
 		}
 	}
 
-	protected function redirectToUrl($url)
-	{
-		$continue_url = "";
-		redirect($continue_url);
-		//return new ResponseRedirect($continue_url, 302);
-	}
-
 }

+ 6 - 0
src/Routing/Controllers/WebAppController.php

@@ -8,6 +8,9 @@ use \KarmaFW\Lib\Hooks\HooksManager;
 
 class WebAppController extends AppController
 {
+	protected $request;
+	protected $response;
+	
 	protected $request_uri = null;
 	protected $request_method = null;
 	protected $route = null;
@@ -20,6 +23,9 @@ class WebAppController extends AppController
 	{
 		parent::__construct();
 
+		$this->request = $request;
+		$this->response = $response;
+
 		$this->request_uri = $request->SERVER['REQUEST_URI'];
 		$this->request_method = $request->SERVER['REQUEST_METHOD'];
 		$this->route = $route;

+ 2 - 2
src/WebApp.php

@@ -154,7 +154,7 @@ class WebApp extends App
 			$template->display($error_template);
 
 		} else {
-			//header("HTTP/1.0 " . $error_code . " " . $title);
+			//header("HTTP/1.0 " . $http_status . " " . $meta_title);
 
 			$output_html = '';
 			$output_html .= '<html>' . PHP_EOL;
@@ -176,7 +176,7 @@ class WebApp extends App
 			echo $output_html;
 		}
 
-		exit;
+		//exit;
 	}