@@ -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 | |
@@ -18,7 +18,7 @@ |
||
| 18 | 18 | Viewer::create(__DIR__.'/Views/') |
| 19 | 19 | ->render('default', $data); |
| 20 | 20 | |
| 21 | -}catch(Exception $er){ |
|
| 21 | +} catch(Exception $er){ |
|
| 22 | 22 | |
| 23 | 23 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
| 24 | 24 | |
@@ -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,27 +4,27 @@ |
||
| 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 | } |
| 15 | 15 | |
| 16 | 16 | protected function check_importExist($import): string |
| 17 | 17 | { |
| 18 | - $tpl = str_replace('.',DIRECTORY_SEPARATOR, |
|
| 18 | + $tpl = str_replace('.', DIRECTORY_SEPARATOR, |
|
| 19 | 19 | substr( |
| 20 | 20 | $import, |
| 21 | - strpos($import,'\'')+1, |
|
| 21 | + strpos($import, '\'')+1, |
|
| 22 | 22 | strlen($import)-11 |
| 23 | 23 | ) |
| 24 | 24 | ); |
| 25 | 25 | |
| 26 | - if(!file_exists($this->path . DIRECTORY_SEPARATOR . $tpl . '.tpl.php')){ |
|
| 27 | - throw new Exception('Import \''.str_replace(['@import(\'','\')'],'',$import).'\' não encontrado.'); |
|
| 26 | + if (!file_exists($this->path.DIRECTORY_SEPARATOR.$tpl.'.tpl.php')) { |
|
| 27 | + throw new Exception('Import \''.str_replace(['@import(\'', '\')'], '', $import).'\' não encontrado.'); |
|
| 28 | 28 | } |
| 29 | 29 | return $tpl; |
| 30 | 30 | } |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | namespace HnrAzevedo\Viewer; |
| 4 | 4 | |
| 5 | -class Viewer{ |
|
| 5 | +class Viewer { |
|
| 6 | 6 | use HelperTrait, CheckTrait; |
| 7 | 7 | |
| 8 | 8 | private static $instance = null; |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | public static function getInstance(string $path): Viewer |
| 18 | 18 | { |
| 19 | - if(is_null(self::$instance)){ |
|
| 19 | + if (is_null(self::$instance)) { |
|
| 20 | 20 | self::$instance = new self($path); |
| 21 | 21 | } |
| 22 | 22 | return self::$instance; |
@@ -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 | |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | $this->saveData(); |
| 45 | 45 | |
| 46 | - if(!$return){ |
|
| 46 | + if (!$return) { |
|
| 47 | 47 | echo $buffer; |
| 48 | 48 | return ''; |
| 49 | 49 | } |
@@ -4,34 +4,34 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | 6 | |
| 7 | -trait HelperTrait{ |
|
| 7 | +trait HelperTrait { |
|
| 8 | 8 | use CheckTrait; |
| 9 | 9 | |
| 10 | 10 | protected function getOB(string $require, array $data = []): string |
| 11 | 11 | { |
| 12 | - foreach($data as $variable => $value){ |
|
| 12 | + foreach ($data as $variable => $value) { |
|
| 13 | 13 | $$variable = $value; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | - if(!file_exists($require)){ |
|
| 16 | + if (!file_exists($require)) { |
|
| 17 | 17 | throw new Exception("Impotation file does not exist:{$require} ."); |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | $this->initData(); |
| 21 | 21 | |
| 22 | - $_SESSION['data'] = (!empty($data)) ? array_merge($data,$_SESSION['data']) : $_SESSION['data']; |
|
| 22 | + $_SESSION['data'] = (!empty($data)) ? array_merge($data, $_SESSION['data']) : $_SESSION['data']; |
|
| 23 | 23 | |
| 24 | 24 | ob_start(); |
| 25 | 25 | require($require); |
| 26 | 26 | $response = ob_get_contents(); |
| 27 | 27 | ob_end_clean(); |
| 28 | 28 | |
| 29 | - $response = explode(PHP_EOL,$response); |
|
| 30 | - foreach($response as $index => $value){ |
|
| 29 | + $response = explode(PHP_EOL, $response); |
|
| 30 | + foreach ($response as $index => $value) { |
|
| 31 | 31 | $response[$index] = ltrim($value); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - return implode('',$response); |
|
| 34 | + return implode('', $response); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | protected function initData() |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | $_SESSION['data'] = (empty($_SESSION['data'])) ? null : $_SESSION['data']; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - protected function getVars(string $buffer,string $prefix = null, ?array $values = null): string |
|
| 42 | + protected function getVars(string $buffer, string $prefix = null, ?array $values = null): string |
|
| 43 | 43 | { |
| 44 | 44 | $this->initData(); |
| 45 | 45 | |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | protected function replace_vars($buffer, $vars, $prefix): string |
| 52 | 52 | { |
| 53 | 53 | foreach ($vars as $key => $value) { |
| 54 | - switch(gettype($value)){ |
|
| 54 | + switch (gettype($value)) { |
|
| 55 | 55 | case 'array': |
| 56 | 56 | $buffer = $this->replace_Array($buffer, $value, $prefix, $key); |
| 57 | 57 | break; |
@@ -69,9 +69,9 @@ discard block |
||
| 69 | 69 | |
| 70 | 70 | protected function replace_value(string $buffer, $value, ?string $prefix, string $key): string |
| 71 | 71 | { |
| 72 | - if(gettype($value)!=='array' && gettype($value)!=='object'){ |
|
| 73 | - while(strstr($buffer,'{{'.$prefix.$key.'}}')){ |
|
| 74 | - $buffer = str_replace('{{'.$prefix.$key.'}}', $value ,$buffer); |
|
| 72 | + if (gettype($value)!=='array' && gettype($value)!=='object') { |
|
| 73 | + while (strstr($buffer, '{{'.$prefix.$key.'}}')) { |
|
| 74 | + $buffer = str_replace('{{'.$prefix.$key.'}}', $value, $buffer); |
|
| 75 | 75 | } |
| 76 | 76 | } |
| 77 | 77 | return $buffer; |
@@ -79,12 +79,12 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | protected function replace_Object(string $buffer, object $obj, string $prefix, string $key): string |
| 81 | 81 | { |
| 82 | - foreach($obj->get_object_vars() as $field => $val){ |
|
| 82 | + foreach ($obj->get_object_vars() as $field => $val) { |
|
| 83 | 83 | |
| 84 | - $buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.' , $field); |
|
| 84 | + $buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.', $field); |
|
| 85 | 85 | |
| 86 | - while(strstr($buffer,'{{'.$prefix.$key.'.'.$field.'}}')){ |
|
| 87 | - $buffer = str_replace('{{'.$prefix.$key.'.'.$field.'}}',$val ,$buffer); |
|
| 86 | + while (strstr($buffer, '{{'.$prefix.$key.'.'.$field.'}}')) { |
|
| 87 | + $buffer = str_replace('{{'.$prefix.$key.'.'.$field.'}}', $val, $buffer); |
|
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | return $buffer; |
@@ -92,11 +92,11 @@ discard block |
||
| 92 | 92 | |
| 93 | 93 | protected function replace_Array(string $buffer, array $array, ?string $prefix = '', ?string $key = ''): string |
| 94 | 94 | { |
| 95 | - foreach($array as $field => $val){ |
|
| 96 | - $buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.' , $field); |
|
| 95 | + foreach ($array as $field => $val) { |
|
| 96 | + $buffer = $this->replace_value($buffer, $val, $key.'.'.$field.'.', $field); |
|
| 97 | 97 | |
| 98 | - while(strstr($buffer,'{{'.$prefix.$key.'.'.$field.'}}')){ |
|
| 99 | - $buffer = str_replace('{{'.$prefix.$key.'.'.$field.'}}',$val,$buffer); |
|
| 98 | + while (strstr($buffer, '{{'.$prefix.$key.'.'.$field.'}}')) { |
|
| 99 | + $buffer = str_replace('{{'.$prefix.$key.'.'.$field.'}}', $val, $buffer); |
|
| 100 | 100 | } |
| 101 | 101 | } |
| 102 | 102 | return $buffer; |
@@ -104,25 +104,25 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | protected function removeComments(string $buffer): string |
| 106 | 106 | { |
| 107 | - while(strstr($buffer,'<!--')){ |
|
| 107 | + while (strstr($buffer, '<!--')) { |
|
| 108 | 108 | $comment = substr( |
| 109 | 109 | $buffer, |
| 110 | - strpos($buffer,'<!--'), |
|
| 111 | - strpos(strstr($buffer,'<!--'),'-->')+3 |
|
| 110 | + strpos($buffer, '<!--'), |
|
| 111 | + strpos(strstr($buffer, '<!--'), '-->')+3 |
|
| 112 | 112 | ); |
| 113 | - $buffer = str_replace($comment,'',$buffer); |
|
| 113 | + $buffer = str_replace($comment, '', $buffer); |
|
| 114 | 114 | } |
| 115 | 115 | return $buffer; |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | protected function saveData(): bool |
| 119 | 119 | { |
| 120 | - if(session_status() !== PHP_SESSION_ACTIVE){ |
|
| 120 | + if (session_status()!==PHP_SESSION_ACTIVE) { |
|
| 121 | 121 | return false; |
| 122 | 122 | } |
| 123 | 123 | unset($_SESSION['data']); |
| 124 | 124 | |
| 125 | - if(!empty($_SESSION['save'])){ |
|
| 125 | + if (!empty($_SESSION['save'])) { |
|
| 126 | 126 | foreach ($_SESSION['save'] as $key => $value) { |
| 127 | 127 | $_SESSION['data'][$key] = $value; |
| 128 | 128 | } |