Browse Source

Merge branch 'master' of ssh://gogs.karmas.fr/KarmaSolutions/KarmaFW

Max F 4 years ago
parent
commit
fd0f25e012

+ 2 - 2
composer.json

@@ -1,5 +1,5 @@
 {
-    "name": "karmasolutions/karmafw",
+    "name": "karma-solutions/karmafw",
     "description": "PHP framework",
     "authors": [
         {
@@ -7,7 +7,7 @@
             "email": "max@karma-solutions.fr"
         }
     ],
-    "minimum-stability": "dev",
+    "license": "MIT",
     "require": {
         "php": ">=5.4.0"
     },

+ 19 - 0
composer.json.bak

@@ -0,0 +1,19 @@
+{
+    "name": "karma-solutions/karmafw",
+    "description": "PHP framework",
+    "authors": [
+        {
+            "name": "Karma Solutions",
+            "email": "max@karma-solutions.fr"
+        }
+    ],
+    "license": "MIT",
+    "require": {
+        "php": ">=5.4.0"
+    },
+    "autoload": {
+        "psr-4": {
+            "KarmaFW\\": "src/"
+        }
+    }
+}

+ 26 - 0
src/App/Middlewares/UrlRouter.php

@@ -97,8 +97,34 @@ class UrlRouter
 				return $response->html($error_message, $error_code);
 			}
 
+			// ERROR 403
+			if (in_array($error_code, [401, 403])) {
+				// if $error_code is a 403 page not found
+				if (empty($error_message)) {
+					$error_message = '<title>Access denied</title><h1>Access denied</h1><p>Page access denied</p>';
+				}
+				return $response->html($error_message, $error_code);
+			}
+
+			// ERROR 400
+			if (in_array($error_code, [400])) {
+				// if $error_code is a 400 page not found
+				if (empty($error_message)) {
+					$error_message = '<title>Bad request</title><h1>Bad request</h1><p>Bad request</p>';
+				}
+				return $response->html($error_message, $error_code);
+			}
+
 
 			// ERROR 500
+			if (in_array($error_code, [500])) {
+				// if $error_code is a 500 page not found
+				if (empty($error_message)) {
+					$error_message = '<title>Server error</title><h1>Server error</h1><p>An error has occured</p>';
+				}
+				return $response->html($error_message, $error_code);
+			}
+
 
 			if (! $this->catch_exceptions) {
 				// on relance l'exception => pour laisser la gestion de l'erreur à un handler parent (ou le error_handler par defaut de PHP)

+ 1 - 1
src/Http/Request.php

@@ -116,7 +116,7 @@ class Request
 
 	public function getFullUrl()
 	{
-		$scheme = $this->isSecure() ? 'https://' : 'http:';
+		$scheme = $this->isSecure() ? 'https://' : 'http://';
 		return $scheme . $this->SERVER['SERVER_NAME'] . $this->url;
 	}
 

+ 4 - 0
src/Http/Response.php

@@ -411,6 +411,8 @@ class Response
 			->setStatus($status)
 			->setBody('');
 
+		// TODO: throw response immediately
+
 		return $this;
 	}
 
@@ -421,6 +423,8 @@ class Response
 			->setContentType($content_type)
 			->setBody($body);
 
+		// TODO: throw response immediately
+
 		return $this;
 	}
 

+ 17 - 14
src/Routing/Controllers/WebAppController.php

@@ -83,7 +83,7 @@ class WebAppController extends AppController
 
 
 
-	public function error($http_status = 500, $meta_title = 'Server Error', $h1 = 'Error 500 - Server Error', $message = 'an error has occured')
+	protected function showError($http_status = 500, $meta_title = 'Server Error', $h1 = 'Error 500 - Server Error', $message = 'an error has occured')
 	{
 		if ($template = $this->getTemplate()) {
 			$template->assign('meta_title', $meta_title);
@@ -97,7 +97,8 @@ class WebAppController extends AppController
 			}
 
 			//$template->display($error_template);
-			return $this->response->html( $template->fetch($error_template) , $http_status);
+			//return $this->response->html( $template->fetch($error_template) , $http_status);
+			$response_content = $template->fetch($error_template);
 
 		} else {
 			//header("HTTP/1.0 " . $http_status . " " . $meta_title);
@@ -121,35 +122,37 @@ class WebAppController extends AppController
 
 			//echo $output_html;
 
-			return $this->response->html($output_html, $http_status);
+			//return $this->response->html($output_html, $http_status);
+			$response_content = $output_html;
 		}
 
+		
+		throw new \Exception($response_content, $http_status);
 	}
 
-	public function error400($title = 'Bad request', $message = '')
+	protected function showError400($title = 'Bad request', $message = '')
 	{
-		return $this->error(400, $title, $title, $message);
+		return $this->showError(400, $title, $title, $message);
 	}
 
-	public function error403($title = 'Forbidden', $message = 'you are not allowed')
+	protected function showError403($title = 'Forbidden', $message = 'you are not allowed')
 	{
-		return $this->error(403, $title, $title, $message);
+		return $this->showError(403, $title, $title, $message);
 	}
 
-	public function error404($title = 'Page not found', $message = "The page you're looking for doesn't exist")
+	protected function showError404($title = 'Page not found', $message = "The page you're looking for doesn't exist")
 	{
-		return $this->error(404, $title, $title, $message);
+		return $this->showError(404, $title, $title, $message);
 	}
 
-	public function error500($title = 'Internal Server Error', $message = 'An error has occured')
+	protected function showError500($title = 'Internal Server Error', $message = 'An error has occured')
 	{
-		return $this->error(500, $title, $title, $message);
+		return $this->showError(500, $title, $title, $message);
 	}
 
-	public function error503($title = 'Service Unavailable', $message = 'The service is unavailable')
+	protected function showError503($title = 'Service Unavailable', $message = 'The service is unavailable')
 	{
-		return $this->error(503, $title, $title, $message);
+		return $this->showError(503, $title, $title, $message);
 	}
 
-
 }

+ 50 - 37
src/Templates/LightweightTemplate.php

@@ -15,10 +15,9 @@ class LightweightTemplate {
 	static $tpl_last_updated = null;
 
 	protected $tpl_cache_enabled = true; // and if $cache_enabled is true
-
-
 	protected $data = [];
 
+
 	public function __construct($tpl_path=null, $variables=[], $layout=null) 
 	{
 		$this->data = $variables;
@@ -30,6 +29,7 @@ class LightweightTemplate {
 		$this->tpl_cache_enabled = false;
 	}
 
+
 	public function enableCache() 
 	{
 		$this->tpl_cache_enabled = true;
@@ -48,17 +48,20 @@ class LightweightTemplate {
 			$this->data[$k] = $v;
 		}
 	}
+
 	
 	public function getVariables() 
 	{
 		return $this->data;
 	}
+
 	
 	public function getVar($var_name, $default_value=null) 
 	{
 		return isset($this->data[$var_name]) ? $this->data[$var_name] : $default_value;
 	}
 
+
 	public function fetch($tpl=null, $extra_vars=[], $layout=null, $options=[]) 
 	{
 		ob_start();
@@ -68,6 +71,7 @@ class LightweightTemplate {
 		return $content;
 	}
 
+
 	public function display($tpl=null, $extra_vars=[], $layout=null, $options=[]) 
 	{
 		$allow_template_debug_traces = empty($options['deny_template_debug']);
@@ -151,12 +155,14 @@ class LightweightTemplate {
 		return $cached_file;
 	}
 
+
 	public static function clearCache() {
 		foreach(glob(self::$cache_path . '/*') as $file) {
 			unlink($file);
 		}
 	}
 
+
 	protected static function compileCode($code) {
 		$code = self::compileBlock($code);
 		$code = self::compileYield($code);
@@ -164,10 +170,11 @@ class LightweightTemplate {
 		$code = self::compileModules($code);
 		$code = self::compileEscapedEchos($code);
 		$code = self::compileEchos($code);
-		//$code = self::compilePHP($code);
+
 		return $code;
 	}
 
+
 	protected static function includeFiles($file, $level=0, $caller_file=null, $parent_file=null, $allow_template_debug_traces=true) {
 		$file_path = strpos($file, '/') === 0 ? $file : (self::$tpl_path . '/' . $file);
 
@@ -213,6 +220,7 @@ class LightweightTemplate {
 
 
 		if (defined('ENV') && ENV == 'dev') {
+			// show dev/debug infos when on development ENV
 			$tpl_infos = '';
 			if ($caller_file) {
 				$tpl_infos .= ' layout for ' . $caller_file . '';
@@ -239,7 +247,8 @@ class LightweightTemplate {
 
 			$layout_code = self::includeFiles($layout, $level-1, $file, null, $allow_template_debug_traces);
 			$code = str_replace($value[0], '', $code);
-			
+
+			// @content
 			$layout_code = str_replace('<' . '?=$child_content?' . '>', '{@content}', $layout_code);
 			$layout_code = str_replace('{$child_content}', '{@content}', $layout_code);
 			$layout_code = str_replace('{@content}', $code, $layout_code);
@@ -275,27 +284,16 @@ class LightweightTemplate {
 		return $code;
 	}
 
-	protected static function compilePHP($code) {
-		/* return preg_replace('~\{%\s*(.+?)\s*\%}~is', '<?php $1 ?>', $code); */
-		return $code;
-	}
 
 	protected static function compileModules($code) {
 
 		// url => {url clients_list}
 		$code = preg_replace('/{routeUrl /', '{url ', $code); // for compatibility with old templates
+		$code = preg_replace('/{route /', '{url ', $code); // for compatibility with old templates
 		preg_match_all('~{url (.*?)}~is', $code, $matches, PREG_SET_ORDER);
 		foreach ($matches as $value) {
-			//pre($matches); exit;
-			if (false) {
-				// {url clients_list} => "/clients/"
-				$code = str_replace($value[0], getRouteUrl($value[1]), $code);
-				// si on utilise des prefix d'urls, le prefix sera hardcodé dans le cache et crééra des erreurs d'urls quand on passera sur une url d'un prefix different
-
-			} else {
-				// {url clients_list} => "< ?=getRouteUrl('clients_list')? >" (dans espaces)
-				$code = str_replace($value[0], '<?=getRouteUrl("' . $value[1] . '")?>', $code);
-			}
+			// {url clients_list} => "< ?=getRouteUrl('clients_list')? >" (dans espaces)
+			$code = str_replace($value[0], '<?=getRouteUrl("' . $value[1] . '")?>', $code);
 		}
 
 		// foreach => {foreach $list as $item}<div>...</div>{/foreach}
@@ -310,7 +308,10 @@ class LightweightTemplate {
 		foreach ($matches as $value) {
 			
 			$replaced = '<' . '?php elseif ( $1 ) : ?' . '>';
-			$value[2] = preg_replace('/{elseif (.*?) ?}/', $replaced, $value[2]);
+			$value[2] = preg_replace('/\{ ?elseif (.*?) ?\}/', $replaced, $value[2]);
+
+			$replaced = '<' . '?php else : ?' . '>';
+			$value[2] = preg_replace('/\{ ?else ?\}/', $replaced, $value[2]);
 
 			$replaced = PHP_EOL . '<' . '?php if (' . $value[1] . ') : ?' . '>' . PHP_EOL . $value[2] . PHP_EOL . '<' . '?php endif; ?' . '>';
 			$code = str_replace($value[0], $replaced, $code);
@@ -319,43 +320,55 @@ class LightweightTemplate {
 		return $code;
 	}
 
-	protected static function compileEchos($code, $strict=true) {
-		// compile PHP variables (method 1) => {$my_var}
-		if ($strict) {
-			$code = preg_replace('~\{\$(.+?)}~is', '<?php echo \$$1 ?>', $code);
-		} else {
-			$code = preg_replace('~\{\$(.+?)}~is', '<?php echo isset(\$$1) ? (\$$1) : ""; ?>', $code);
-		}
-		// compile PHP variables (method 2) => {{ $my_var }}
-		return preg_replace('~\{{\s*(.+?)\s*\}}~is', '<?php echo $1 ?>', $code);
+
+	protected static function compileEchos($code) {
+		// compile PHP optional variables => {{$my_optional_var}} => echo (isset($my_optional_var) ? $my_optional_var : '')
+		$code = preg_replace('~\{{\$(.+?)}}~is', '<?php echo isset(\$$1) ? (\$$1) : ""; ?>', $code);
+
+		// compile PHP strict variables => {$my_var} => echo $my_var
+		$code = preg_replace('~\{\$(.+?)}~is', '<?php echo \$$1 ?>', $code);
+
+		// compile PHP code => {{ my_php_code }} => echo my_php_code
+		$code = preg_replace('~\{{\s*(.+?)\s*\}}~is', '<?php echo $1 ?>', $code);
+
+		return $code;
 	}
 
+
 	protected static function compileEscapedEchos($code) {
 		// compile PHP escaped variables => {{{ $my_var }}}
 		return preg_replace('~\{{{\s*(.+?)\s*\}}}~is', '<?php echo htmlentities($1, ENT_QUOTES, \'UTF-8\') ?>', $code);
 	}
 
+
 	protected static function compileBlock($code) {
 		preg_match_all('~{block ?(.*?) ?}(.*?){/block}~is', $code, $matches, PREG_SET_ORDER);
+
 		foreach ($matches as $value) {
-			if (!array_key_exists($value[1], self::$blocks)) self::$blocks[$value[1]] = '';
-			if (strpos($value[2], '@parent') === false) {
-				self::$blocks[$value[1]] = $value[2];
-			} else {
-				self::$blocks[$value[1]] = str_replace('@parent', self::$blocks[$value[1]], $value[2]);
+			$block_outer = $value[0];
+			$block_name = $value[1];
+			$block_inner = $value[2];
+
+			if (!array_key_exists($block_name, self::$blocks)) {
+				self::$blocks[$block_name] = [];
 			}
-			$code = str_replace($value[0], '', $code);
+			
+			self::$blocks[$block_name][] = $block_inner;
+
+			$code = str_replace($block_outer, '', $code);
 		}
 		return $code;
 	}
 
+
 	protected static function compileYield($code) {
 		// compile yields => {yield my_block}
-		foreach(self::$blocks as $block => $value) {
-			$code = preg_replace('/{yield ' . $block . ' ?}/', $value, $code);
+		foreach(self::$blocks as $block_name => $block_chunks) {
+			$block_replace = implode('', $block_chunks);
+			$code = preg_replace('/{yield ' . $block_name . ' ?}/', $block_replace, $code);
 		}
 		$code = preg_replace('/{yield ?(.*?) ?}/i', '', $code);
 		return $code;
 	}
 
-}
+}

+ 2 - 2
src/helpers/helpers_array.php

@@ -233,8 +233,8 @@ if (! function_exists('get_csv')) {
 				$line = array();
 				foreach ($fields as $field) {
 					$val = $row[$field];
-					if (is_numeric($val) && substr($val."", 0, 1) === "0" && strlen($val."") > 1) {
-						// pour exporter correctement dans Excel les numeros de telephone commencant par 0
+					if (is_numeric($val) && strlen($val."") > 1) {
+						// pour exporter correctement dans Excel les valeurs numériques
 						$val = '="' . $val . '"';
 					}
 					$line[] = $val;