Browse Source

dev 2020-10

Max F 5 years ago
parent
commit
8078287be0

+ 6 - 15
src/App/Middlewares/DebugBar.php

@@ -29,11 +29,9 @@ class DebugBar
 			App::setData('debugbar', $debugbar);
 			
 			$debugbar->addCollector(new KarmaFwCollector);
-			$debugbar->addCollector(new SEOCollector);
 			$debugbar->addCollector(new SqlDbCollector);
-
-			//$debugbar->addCollector(new PhpTemplateCollector); // DO NOT WORK
 			$debugbar->addCollector(new KarmaMessagesCollector('templates'));
+			$debugbar->addCollector(new SEOCollector);
 
 			$debugbarRenderer = $debugbar->getJavascriptRenderer('/assets/vendor/debugbar'); // symlink to ${APP_DIR}/vendor/maximebf/debugbar/src/DebugBar/Resources
 		}
@@ -83,31 +81,24 @@ class DebugBar
 	{
 		$content = $response->getBody();
 
-
-		preg_match('~<title(.*?)>(.*?)</title>~', $content, $matches);
+		preg_match('~<title(.*?)>(.*?)</title>~is', $content, $matches);
 		$title = empty($matches) ? '' : $matches[2];
 
-		preg_match('~<meta +name="description" +content="(.*?)" *>~', $content, $matches);
+		preg_match('~<meta +name="description" +content="(.*?)" *>~is', $content, $matches);
 		$meta_desc = empty($matches) ? '' : $matches[1];
 
-		$x = strpos($content, '<h1');
-		$subcontent = substr($content, $x, 1024);
-		//pre($subcontent); exit;
-		//preg_match('~<h1>(.*?)</h1>~', $content, $matches);
-		preg_match('~<h1(.*?)>(.*?)</h1>~', $content, $matches);
-		//pre($matches); exit;
+		preg_match('~<h1(.*?)>(.*?)</h1>~is', $content, $matches);
 		$h1 = empty($matches) ? '' : $matches[2];
 
-		preg_match_all('/<a /', $content, $matches);
+		preg_match_all('/<a /is', $content, $matches);
 		$nb_links = empty($matches) ? 0 : count($matches[0]);
 
-
 		$data = [
 			'title' => $title,
 			'meta description' => $meta_desc,
 			'h1' => $h1,
 			'nb links' => $nb_links,
-			'content length' => strlen($content),
+			'content length' => formatSize(strlen($content)),
 		];
 
 		return $data;

+ 16 - 1
src/App/Middlewares/TrafficLogger.php

@@ -2,6 +2,7 @@
 
 namespace KarmaFW\App\Middlewares;
 
+use \KarmaFW\App;
 use \KarmaFW\Http\Request;
 use \KarmaFW\Http\Response;
 
@@ -11,8 +12,22 @@ class TrafficLogger
 	
 	public function __invoke(Request $request, Response $response, callable $next)
 	{
+        if (! isset($request->SERVER['REQUEST_TIME_FLOAT'])) {
+            $request->SERVER['REQUEST_TIME_FLOAT'] = microtime(true);
+        }
 
-		return $next($request, $response);
+		$response = $next($request, $response);
+
+        $ts_end = microtime(true);
+        $duration = $ts_end - $request->SERVER['REQUEST_TIME_FLOAT'];
+
+		$traffic_logger = App::getData('app')->get('traffic_logger');
+
+		if ($traffic_logger) {
+			$traffic_logger($request, $response, $duration);
+		}
+
+		return $response;
 	}
 
 }

+ 1 - 5
src/Database/Sql/SqlQuery.php

@@ -145,7 +145,7 @@ class SqlQuery
 		$this->db->setLastQuery($this);
 
 
-		// TODO: voir comment bien injecter cette dependance
+		// debugbar
 		$debugbar = App::getData('debugbar');
 		if ($debugbar) {
 			//$debugbar['sql']->addMessage( preg_replace('/\s+/', ' ', $query) );
@@ -166,14 +166,10 @@ class SqlQuery
 					'duration' => $this->duration,
 					'duration_str' => formatDuration($this->duration),
 					'row_count' => $rs->getRowsCount(),
-					//'stmt_id' => null,
-					//'prepared_stmt' => null,
 					'params' => $params,
 					'memory' => $memory_used,
-					//'memory_str' => round($memory_used/1000000, 1) . "Mo",
 					'memory_str' => formatSize($memory_used),
 					'end_memory' => $mem_end,
-					//'end_memory_str' => round($mem_end/1000000, 1) . "Mo",
 					'end_memory_str' => formatSize($mem_end),
 					'is_success' => $is_success,
 					'error_code' => $error_code,

+ 26 - 0
src/Http/Request.php

@@ -11,6 +11,7 @@ class Request
 	protected $method = null;
 	protected $url = null;
 	protected $protocol = null;
+	protected $attributes = [];
 
 	protected $route = null;
 	protected $client_ip = null;
@@ -32,6 +33,8 @@ class Request
 		$this->protocol = $version;
 		//$this->setHeaders($headers);
 
+		$this->setAttribute('env', ENV);
+
 		//print_r($_SERVER); exit;
 	}
 
@@ -175,6 +178,29 @@ class Request
 		return (! empty($request->SERVER['HTTP_X_REQUESTED_WITH']) && $request->SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
 	}
 
+
+
+	public function getAttributes()
+	{
+		return $this->attributes;
+	}
+
+	public function setAttributes($attributes)
+	{
+		$this->attributes = $attributes;
+	}
+
+	public function getAttribute($key, $default_value=null)
+	{
+		return isset($this->attributes[$key]) ? $this->attributes[$key] : $default_value;
+	}
+
+	public function setAttribute($key, $value)
+	{
+		$this->attributes[$key] = $value;
+	}
+
+
 	/*
 
 	public function setUrl($url)

+ 6 - 0
src/Kernel.php

@@ -178,6 +178,8 @@ class Kernel
     
 	public function loadServices()
 	{
+		// TODO: rendre parametrable la liste des services
+
 		$this->set('db', function ($dsn=null) {
 			return new \KarmaFW\Database\Sql\SqlDb($dsn);
 		});
@@ -190,6 +192,10 @@ class Kernel
 			//return new \KarmaFW\Templates\PhpTemplate($tpl, $data);
 			return new \KarmaFW\Templates\LightweightTemplate($tpl, $data);
 		});
+
+		$this->set('traffic_logger', function (Request $request, Response $response) {
+			return null; // TODO
+		});
 	}
 
 

+ 5 - 1
src/Templates/LightweightTemplate.php

@@ -135,6 +135,10 @@ class LightweightTemplate {
 	}
 
 	protected static function includeFiles($file, $level=0, $caller_file=null, $parent_file=null) {
+		if (! is_file(self::$tpl_path . '/' . $file)) {
+			throw new \Exception("Template file not found " . $file, 500);
+		}
+		
 		$code = file_get_contents(self::$tpl_path . '/' . $file);
 		$code_init = $code;
 		$layout = null;
@@ -259,7 +263,7 @@ class LightweightTemplate {
 		preg_match_all('~{if (.*?) ?}(.*?){/if}~is', $code, $matches, PREG_SET_ORDER);
 		foreach ($matches as $value) {
 			
-			$replaced = '<' . '? else if ( $1 ) : ?' . '>';
+			$replaced = '<' . '?php elseif ( $1 ) : ?' . '>';
 			$value[2] = preg_replace('/{elseif (.*?) ?}/', $replaced, $value[2]);
 
 			$replaced = PHP_EOL . '<' . '?php if (' . $value[1] . ') : ?' . '>' . PHP_EOL . $value[2] . PHP_EOL . '<' . '?php endif; ?' . '>';