@@ -16,7 +16,7 @@ |
||
| 16 | 16 | ]; |
| 17 | 17 | |
| 18 | 18 | Viewer::create(__DIR__.'/Views/') |
| 19 | - ->render('default', $data); |
|
| 19 | + ->render('default', $data); |
|
| 20 | 20 | |
| 21 | 21 | }catch(Exception $er){ |
| 22 | 22 | |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | |
| 9 | 9 | /* NOTE: in case of error an exception is thrown */ |
| 10 | 10 | |
| 11 | -try{ |
|
| 11 | +try { |
|
| 12 | 12 | |
| 13 | 13 | $data = ['parameter'=> |
| 14 | 14 | [ |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | Viewer::create(__DIR__.'/Views/') |
| 21 | 21 | ->render('default', $data); |
| 22 | 22 | |
| 23 | -}catch(Exception $er){ |
|
| 23 | +}catch (Exception $er) { |
|
| 24 | 24 | |
| 25 | 25 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
| 26 | 26 | |
@@ -4,11 +4,11 @@ |
||
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | 6 | |
| 7 | -trait CheckTrait{ |
|
| 7 | +trait CheckTrait { |
|
| 8 | 8 | |
| 9 | - protected function check_viewExist(string $viewfile){ |
|
| 10 | - if(!file_exists($this->path . $viewfile . '.view.php')){ |
|
| 11 | - $v = $this->path . $viewfile; |
|
| 9 | + protected function check_viewExist(string $viewfile) { |
|
| 10 | + if (!file_exists($this->path.$viewfile.'.view.php')) { |
|
| 11 | + $v = $this->path.$viewfile; |
|
| 12 | 12 | throw new Exception("Preview file {$v} not found"); |
| 13 | 13 | } |
| 14 | 14 | } |
@@ -4,10 +4,10 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | 6 | |
| 7 | -trait EspecialHelperTrait{ |
|
| 7 | +trait EspecialHelperTrait { |
|
| 8 | 8 | use HelperTrait; |
| 9 | 9 | |
| 10 | - protected function getEspecialVars(string $buffer,string $prefix = null, ?array $values = null): string |
|
| 10 | + protected function getEspecialVars(string $buffer, string $prefix = null, ?array $values = null): string |
|
| 11 | 11 | { |
| 12 | 12 | $this->initData(); |
| 13 | 13 | |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | protected function replaceEspecialVars($buffer, $vars, $prefix): string |
| 20 | 20 | { |
| 21 | 21 | foreach ($vars as $key => $value) { |
| 22 | - switch(gettype($value)){ |
|
| 22 | + switch (gettype($value)) { |
|
| 23 | 23 | case 'array': |
| 24 | 24 | $buffer = $this->replaceEspecialArray($buffer, $value, $prefix, $key); |
| 25 | 25 | break; |
@@ -37,9 +37,9 @@ discard block |
||
| 37 | 37 | |
| 38 | 38 | protected function replaceEspecialValue(string $buffer, $value, ?string $prefix, string $key): string |
| 39 | 39 | { |
| 40 | - if(gettype($value)!=='array' && gettype($value)!=='object'){ |
|
| 41 | - while(strstr($buffer,'{{!! $'.$prefix.$key.' !!}}')){ |
|
| 42 | - $buffer = str_replace('{{!! $'.$prefix.$key.' !!}}', $value ,$buffer); |
|
| 40 | + if (gettype($value)!=='array' && gettype($value)!=='object') { |
|
| 41 | + while (strstr($buffer, '{{!! $'.$prefix.$key.' !!}}')) { |
|
| 42 | + $buffer = str_replace('{{!! $'.$prefix.$key.' !!}}', $value, $buffer); |
|
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | 45 | return $buffer; |
@@ -47,12 +47,12 @@ discard block |
||
| 47 | 47 | |
| 48 | 48 | protected function replaceEspecialObject(string $buffer, object $obj, string $prefix, string $key): string |
| 49 | 49 | { |
| 50 | - foreach($obj->get_object_vars() as $field => $val){ |
|
| 50 | + foreach ($obj->get_object_vars() as $field => $val) { |
|
| 51 | 51 | |
| 52 | - $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.' , $field); |
|
| 52 | + $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.', $field); |
|
| 53 | 53 | |
| 54 | - while(strstr($buffer,'{{!! $'.$prefix.$key.'.'.$field.' !!}}')){ |
|
| 55 | - $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $val ,$buffer); |
|
| 54 | + while (strstr($buffer, '{{!! $'.$prefix.$key.'.'.$field.' !!}}')) { |
|
| 55 | + $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $val, $buffer); |
|
| 56 | 56 | } |
| 57 | 57 | } |
| 58 | 58 | return $buffer; |
@@ -60,11 +60,11 @@ discard block |
||
| 60 | 60 | |
| 61 | 61 | protected function replaceEspecialArray(string $buffer, array $array, ?string $prefix = '', ?string $key = ''): string |
| 62 | 62 | { |
| 63 | - foreach($array as $field => $val){ |
|
| 64 | - $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.' , $field); |
|
| 63 | + foreach ($array as $field => $val) { |
|
| 64 | + $buffer = $this->replaceEspecialValue($buffer, $val, $key.'.'.$field.'.', $field); |
|
| 65 | 65 | |
| 66 | - while(strstr($buffer,'{{!! $'.$prefix.$key.'.'.$field.' !!}}')){ |
|
| 67 | - $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $val ,$buffer); |
|
| 66 | + while (strstr($buffer, '{{!! $'.$prefix.$key.'.'.$field.' !!}}')) { |
|
| 67 | + $buffer = str_replace('{{!! $'.$prefix.$key.'.'.$field.' !!}}', $val, $buffer); |
|
| 68 | 68 | } |
| 69 | 69 | } |
| 70 | 70 | return $buffer; |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | namespace HnrAzevedo\Viewer; |
| 4 | 4 | |
| 5 | -class Viewer{ |
|
| 5 | +class Viewer { |
|
| 6 | 6 | use EspecialHelperTrait, CheckTrait; |
| 7 | 7 | |
| 8 | 8 | private static $instance = null; |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | public static function getInstance(string $path): Viewer |
| 17 | 17 | { |
| 18 | - if(is_null(self::$instance)){ |
|
| 18 | + if (is_null(self::$instance)) { |
|
| 19 | 19 | self::$instance = new self(); |
| 20 | 20 | } |
| 21 | 21 | self::$instance->path = $path; |
@@ -29,13 +29,13 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | public function render(string $file, array $data = [], bool $return = false): string |
| 31 | 31 | { |
| 32 | - if(!headers_sent()){ |
|
| 32 | + if (!headers_sent()) { |
|
| 33 | 33 | header('Content-Type: text/html; charset=utf-8'); |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | $this->check_viewExist($file); |
| 37 | 37 | |
| 38 | - $buffer = $this->getOB($this->path . DIRECTORY_SEPARATOR . $file . '.view.php', $data); |
|
| 38 | + $buffer = $this->getOB($this->path.DIRECTORY_SEPARATOR.$file.'.view.php', $data); |
|
| 39 | 39 | |
| 40 | 40 | $buffer = $this->getVars($buffer); |
| 41 | 41 | $buffer = $this->getEspecialVars($buffer); |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | $this->saveData(); |
| 46 | 46 | |
| 47 | - if(!$return){ |
|
| 47 | + if (!$return) { |
|
| 48 | 48 | echo $buffer; |
| 49 | 49 | return ''; |
| 50 | 50 | } |
@@ -55,11 +55,11 @@ discard block |
||
| 55 | 55 | public function include(string $file): void |
| 56 | 56 | { |
| 57 | 57 | $buffer = ''; |
| 58 | - try{ |
|
| 58 | + try { |
|
| 59 | 59 | $buffer = $this->getOB($this->path.$file.'.inc.php'); |
| 60 | 60 | $buffer = $this->getVars($buffer); |
| 61 | 61 | $buffer = $this->getEspecialVars($buffer); |
| 62 | - }catch(\Exception $er){ |
|
| 62 | + }catch (\Exception $er) { |
|
| 63 | 63 | $buffer = "<div class='view error'>Component error: {$er->getMessage()}</div>"; |
| 64 | 64 | } |
| 65 | 65 | echo $buffer; |
@@ -3,14 +3,14 @@ discard block |
||
| 3 | 3 | namespace HnrAzevedo\Viewer; |
| 4 | 4 | |
| 5 | 5 | |
| 6 | -trait JScriptTrait{ |
|
| 6 | +trait JScriptTrait { |
|
| 7 | 7 | |
| 8 | 8 | protected function checkInScript(bool $inScript, string $value): bool |
| 9 | 9 | { |
| 10 | - if((substr(ltrim($value),0,8) === '<script>' && !strpos($value,'src'))){ |
|
| 10 | + if ((substr(ltrim($value), 0, 8)==='<script>' && !strpos($value, 'src'))) { |
|
| 11 | 11 | $inScript = true; |
| 12 | 12 | } |
| 13 | - if((substr(ltrim($value),0,9) === '</script>')){ |
|
| 13 | + if ((substr(ltrim($value), 0, 9)==='</script>')) { |
|
| 14 | 14 | $inScript = false; |
| 15 | 15 | } |
| 16 | 16 | return $inScript; |
@@ -18,17 +18,17 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | protected function checkCommentInScript(bool $inComment, string $value): bool |
| 20 | 20 | { |
| 21 | - if(strpos($value,'/*') && !strpos($value,'*/')){ |
|
| 21 | + if (strpos($value, '/*') && !strpos($value, '*/')) { |
|
| 22 | 22 | $inComment = true; |
| 23 | - }else{ |
|
| 24 | - $inComment = (strpos($value,'*/')) ? false : $inComment; |
|
| 23 | + }else { |
|
| 24 | + $inComment = (strpos($value, '*/')) ? false : $inComment; |
|
| 25 | 25 | } |
| 26 | 26 | return $inComment; |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | protected function checkScriptNeed(bool $inComment, string $value): bool |
| 30 | 30 | { |
| 31 | - if(($inComment || strpos($value,'//'))){ |
|
| 31 | + if (($inComment || strpos($value, '//'))) { |
|
| 32 | 32 | return false; |
| 33 | 33 | } |
| 34 | 34 | return true; |
@@ -36,12 +36,12 @@ discard block |
||
| 36 | 36 | |
| 37 | 37 | protected function treatScript(string $value): string |
| 38 | 38 | { |
| 39 | - while( strpos($value,'/*') && strpos($value,'*/') ){ |
|
| 40 | - $replace = substr($value,strripos ($value,'/*'),strripos ($value,'*/')+2); |
|
| 41 | - $value = str_replace($replace,'',$value); |
|
| 39 | + while (strpos($value, '/*') && strpos($value, '*/')) { |
|
| 40 | + $replace = substr($value, strripos($value, '/*'), strripos($value, '*/')+2); |
|
| 41 | + $value = str_replace($replace, '', $value); |
|
| 42 | 42 | } |
| 43 | - if(strpos($value,'*/')){ |
|
| 44 | - $value = substr($value,strpos($value,'*/')+2); |
|
| 43 | + if (strpos($value, '*/')) { |
|
| 44 | + $value = substr($value, strpos($value, '*/')+2); |
|
| 45 | 45 | } |
| 46 | 46 | return $value; |
| 47 | 47 | } |
@@ -4,25 +4,25 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | 6 | |
| 7 | -trait HelperTrait{ |
|
| 7 | +trait HelperTrait { |
|
| 8 | 8 | use CheckTrait, JScriptTrait; |
| 9 | 9 | |
| 10 | 10 | protected function getOB(string $require, array $data = []): string |
| 11 | 11 | { |
| 12 | - foreach($data as $variable => $_){ |
|
| 12 | + foreach ($data as $variable => $_) { |
|
| 13 | 13 | $$variable = $_; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | - $_ = (array_key_exists('_',$data)) ? $data['_'] : null; |
|
| 16 | + $_ = (array_key_exists('_', $data)) ? $data['_'] : null; |
|
| 17 | 17 | |
| 18 | - if(!file_exists($require)){ |
|
| 18 | + if (!file_exists($require)) { |
|
| 19 | 19 | $require = basename($require); |
| 20 | 20 | throw new Exception("Importation file does not exist: {$require} ."); |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | $this->initData(); |
| 24 | 24 | |
| 25 | - $_SESSION['data'] = (!empty($data)) ? array_merge($data,$_SESSION['data']) : $_SESSION['data']; |
|
| 25 | + $_SESSION['data'] = (!empty($data)) ? array_merge($data, $_SESSION['data']) : $_SESSION['data']; |
|
| 26 | 26 | |
| 27 | 27 | ob_start(); |
| 28 | 28 | require($require); |
@@ -36,22 +36,22 @@ discard block |
||
| 36 | 36 | |
| 37 | 37 | private function treatHTML(string $html): string |
| 38 | 38 | { |
| 39 | - $arrayHtml = explode(PHP_EOL,$html); |
|
| 39 | + $arrayHtml = explode(PHP_EOL, $html); |
|
| 40 | 40 | $html = []; |
| 41 | 41 | |
| 42 | 42 | $inScript = false; |
| 43 | 43 | $inComment = false; |
| 44 | 44 | |
| 45 | - foreach($arrayHtml as $index => $value){ |
|
| 45 | + foreach ($arrayHtml as $index => $value) { |
|
| 46 | 46 | $inScript = $this->checkInScript($inScript, $value); |
| 47 | 47 | |
| 48 | 48 | |
| 49 | - if($inScript){ |
|
| 49 | + if ($inScript) { |
|
| 50 | 50 | $inComment = $this->checkCommentInScript($inComment, $value); |
| 51 | 51 | |
| 52 | - if(!$this->checkScriptNeed($inComment, $value)){ |
|
| 52 | + if (!$this->checkScriptNeed($inComment, $value)) { |
|
| 53 | 53 | continue; |
| 54 | - }else{ |
|
| 54 | + }else { |
|
| 55 | 55 | $value = $this->treatScript($value); |
| 56 | 56 | } |
| 57 | 57 | } |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | $html[$index] = ltrim($value); |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - return implode('',$html); |
|
| 62 | + return implode('', $html); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | protected function initData() |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | $_SESSION['data'] = (empty($_SESSION['data'])) ? null : $_SESSION['data']; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - protected function getVars(string $buffer,string $prefix = null, ?array $values = null): string |
|
| 70 | + protected function getVars(string $buffer, string $prefix = null, ?array $values = null): string |
|
| 71 | 71 | { |
| 72 | 72 | $this->initData(); |
| 73 | 73 | |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | protected function replace_vars($buffer, $vars, $prefix): string |
| 80 | 80 | { |
| 81 | 81 | foreach ($vars as $key => $value) { |
| 82 | - switch(gettype($value)){ |
|
| 82 | + switch (gettype($value)) { |
|
| 83 | 83 | case 'array': |
| 84 | 84 | $buffer = $this->replace_Array($buffer, $value, $prefix, $key); |
| 85 | 85 | break; |
@@ -97,9 +97,9 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | protected function replace_value(string $buffer, $value, ?string $prefix, string $key): string |
| 99 | 99 | { |
| 100 | - if(gettype($value)!=='array' && gettype($value)!=='object'){ |
|
| 101 | - while(strstr($buffer,'{{ $'.$prefix.$key.' }}')){ |
|
| 102 | - $buffer = str_replace('{{ $'.$prefix.$key.' }}', htmlspecialchars($value) ,$buffer); |
|
| 100 | + if (gettype($value)!=='array' && gettype($value)!=='object') { |
|
| 101 | + while (strstr($buffer, '{{ $'.$prefix.$key.' }}')) { |
|
| 102 | + $buffer = str_replace('{{ $'.$prefix.$key.' }}', htmlspecialchars($value), $buffer); |
|
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | 105 | return $buffer; |
@@ -107,12 +107,12 @@ discard block |
||
| 107 | 107 | |
| 108 | 108 | protected function replace_Object(string $buffer, object $obj, string $prefix, string $key): string |
| 109 | 109 | { |
| 110 | - foreach($obj->get_object_vars() as $field => $val){ |
|
| 110 | + foreach ($obj->get_object_vars() as $field => $val) { |
|
| 111 | 111 | |
| 112 | - $buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.' , $field); |
|
| 112 | + $buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.', $field); |
|
| 113 | 113 | |
| 114 | - while(strstr($buffer,'{{ $'.$prefix.$key.'.'.$field.' }}')){ |
|
| 115 | - $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val) ,$buffer); |
|
| 114 | + while (strstr($buffer, '{{ $'.$prefix.$key.'.'.$field.' }}')) { |
|
| 115 | + $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val), $buffer); |
|
| 116 | 116 | } |
| 117 | 117 | } |
| 118 | 118 | return $buffer; |
@@ -120,11 +120,11 @@ discard block |
||
| 120 | 120 | |
| 121 | 121 | protected function replace_Array(string $buffer, array $array, ?string $prefix = '', ?string $key = ''): string |
| 122 | 122 | { |
| 123 | - foreach($array as $field => $val){ |
|
| 124 | - $buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.' , $field); |
|
| 123 | + foreach ($array as $field => $val) { |
|
| 124 | + $buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.', $field); |
|
| 125 | 125 | |
| 126 | - while(strstr($buffer,'{{ $'.$prefix.$key.'.'.$field.' }}')){ |
|
| 127 | - $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val) ,$buffer); |
|
| 126 | + while (strstr($buffer, '{{ $'.$prefix.$key.'.'.$field.' }}')) { |
|
| 127 | + $buffer = str_replace('{{ $'.$prefix.$key.'.'.$field.' }}', htmlspecialchars($val), $buffer); |
|
| 128 | 128 | } |
| 129 | 129 | } |
| 130 | 130 | return $buffer; |
@@ -132,25 +132,25 @@ discard block |
||
| 132 | 132 | |
| 133 | 133 | protected function removeComments(string $buffer): string |
| 134 | 134 | { |
| 135 | - while(strstr($buffer,'<!--')){ |
|
| 135 | + while (strstr($buffer, '<!--')) { |
|
| 136 | 136 | $comment = substr( |
| 137 | 137 | $buffer, |
| 138 | - strpos($buffer,'<!--'), |
|
| 139 | - strpos(strstr($buffer,'<!--'),'-->')+3 |
|
| 138 | + strpos($buffer, '<!--'), |
|
| 139 | + strpos(strstr($buffer, '<!--'), '-->')+3 |
|
| 140 | 140 | ); |
| 141 | - $buffer = str_replace($comment,'',$buffer); |
|
| 141 | + $buffer = str_replace($comment, '', $buffer); |
|
| 142 | 142 | } |
| 143 | 143 | return $buffer; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | protected function saveData(): bool |
| 147 | 147 | { |
| 148 | - if(session_status() !== PHP_SESSION_ACTIVE){ |
|
| 148 | + if (session_status()!==PHP_SESSION_ACTIVE) { |
|
| 149 | 149 | return false; |
| 150 | 150 | } |
| 151 | 151 | unset($_SESSION['data']); |
| 152 | 152 | |
| 153 | - if(!empty($_SESSION['save'])){ |
|
| 153 | + if (!empty($_SESSION['save'])) { |
|
| 154 | 154 | foreach ($_SESSION['save'] as $key => $value) { |
| 155 | 155 | $_SESSION['data'][$key] = $value; |
| 156 | 156 | } |