@@ -4,7 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | use Psr\Http\Server\MiddlewareInterface; |
| 6 | 6 | |
| 7 | -interface ViewerInterface extends MiddlewareInterface{ |
|
| 7 | +interface ViewerInterface extends MiddlewareInterface { |
|
| 8 | 8 | |
| 9 | 9 | public static function import(string $file): void; |
| 10 | 10 | |
@@ -88,7 +88,7 @@ |
||
| 88 | 88 | { |
| 89 | 89 | try{ |
| 90 | 90 | echo self::getInstance()->getBody($file.'.inc.php'); |
| 91 | - }catch(\Exception $er){ |
|
| 91 | + } catch(\Exception $er){ |
|
| 92 | 92 | echo "<div class='view error'>Component error: {$er->getMessage()}</div>"; |
| 93 | 93 | } |
| 94 | 94 | } |
@@ -25,14 +25,14 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 27 | 27 | { |
| 28 | - if(null === $request->getAttribute('viewer')){ |
|
| 28 | + if (null===$request->getAttribute('viewer')) { |
|
| 29 | 29 | throw new \RuntimeException('The path and file parameters for viewing were not defined in the request'); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | self::$path = $request->getAttribute('viewer')['path']; |
| 33 | 33 | |
| 34 | 34 | $buffer = $this->getInstance()->render($request->getAttribute('viewer')['file'], |
| 35 | - (isset($request->getAttribute('viewer')['data'])) ? $request->getAttribute('viewer')['data'] : [] ); |
|
| 35 | + (isset($request->getAttribute('viewer')['data'])) ? $request->getAttribute('viewer')['data'] : []); |
|
| 36 | 36 | |
| 37 | 37 | $request = $request->withBody((new Factory())->createStream($buffer)); |
| 38 | 38 | |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | {; |
| 44 | 44 | self::getInstance()->data = $data; |
| 45 | 45 | |
| 46 | - if(!isset(self::getInstance()->middleware )){ |
|
| 46 | + if (!isset(self::getInstance()->middleware)) { |
|
| 47 | 47 | self::getInstance()->handle($file); |
| 48 | 48 | return ''; |
| 49 | 49 | } |
@@ -53,20 +53,20 @@ discard block |
||
| 53 | 53 | |
| 54 | 54 | private function handle(string $file): void |
| 55 | 55 | { |
| 56 | - self::getInstance()->middleware = false; |
|
| 56 | + self::getInstance()->middleware = false; |
|
| 57 | 57 | |
| 58 | 58 | $serverRequest = (new Factory())->createServerRequest( |
| 59 | 59 | $_SERVER['REQUEST_METHOD'], |
| 60 | 60 | new Uri($_SERVER['REQUEST_URI']) |
| 61 | 61 | ); |
| 62 | 62 | |
| 63 | - $serverRequest = $serverRequest->withAttribute('viewer',[ |
|
| 63 | + $serverRequest = $serverRequest->withAttribute('viewer', [ |
|
| 64 | 64 | 'path' => self::$path, |
| 65 | 65 | 'file' => $file, |
| 66 | 66 | 'data' => self::getInstance()->data |
| 67 | 67 | ]); |
| 68 | 68 | |
| 69 | - self::getInstance()->process($serverRequest, new class implements RequestHandlerInterface{ |
|
| 69 | + self::getInstance()->process($serverRequest, new class implements RequestHandlerInterface { |
|
| 70 | 70 | public function handle(ServerRequestInterface $request): ResponseInterface |
| 71 | 71 | { |
| 72 | 72 | echo $request->getBody()->getContents(); |
@@ -85,16 +85,16 @@ discard block |
||
| 85 | 85 | |
| 86 | 86 | public static function import(string $file): void |
| 87 | 87 | { |
| 88 | - try{ |
|
| 88 | + try { |
|
| 89 | 89 | echo self::getInstance()->getBody($file.'.inc.php'); |
| 90 | - }catch(\Exception $er){ |
|
| 90 | + }catch (\Exception $er) { |
|
| 91 | 91 | echo "<div class='view error'>Component error: {$er->getMessage()}</div>"; |
| 92 | 92 | } |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | private function getBody(string $file) |
| 96 | 96 | { |
| 97 | - $buffer = $this->getInstance()->getOB(self::$path . DIRECTORY_SEPARATOR . $file); |
|
| 97 | + $buffer = $this->getInstance()->getOB(self::$path.DIRECTORY_SEPARATOR.$file); |
|
| 98 | 98 | $buffer = $this->getInstance()->getVars($buffer); |
| 99 | 99 | $buffer = $this->getInstance()->getVars($buffer, false); |
| 100 | 100 | $buffer = $this->getInstance()->removeComments($buffer); |
@@ -7,10 +7,10 @@ discard block |
||
| 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,16 +18,16 @@ 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 | return true; |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - return (strpos($value,'*/')) ? false : $inComment; |
|
| 25 | + return (strpos($value, '*/')) ? false : $inComment; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | protected function checkScriptNeed(bool $inComment, string $value): bool |
| 29 | 29 | { |
| 30 | - if(($inComment || strpos($value,'//'))){ |
|
| 30 | + if (($inComment || strpos($value, '//'))) { |
|
| 31 | 31 | return false; |
| 32 | 32 | } |
| 33 | 33 | return true; |
@@ -35,13 +35,13 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | protected function treatScript(string $value): string |
| 37 | 37 | { |
| 38 | - while( strpos($value,'/*') && strpos($value,'*/') ){ |
|
| 39 | - $replace = substr($value,strripos ($value,'/*'),strripos ($value,'*/')+2); |
|
| 40 | - $value = str_replace($replace,'',$value); |
|
| 38 | + while (strpos($value, '/*') && strpos($value, '*/')) { |
|
| 39 | + $replace = substr($value, strripos($value, '/*'), strripos($value, '*/')+2); |
|
| 40 | + $value = str_replace($replace, '', $value); |
|
| 41 | 41 | } |
| 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 | |
| 47 | 47 | return $value; |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -class Address{ |
|
| 3 | +class Address { |
|
| 4 | 4 | public string $nation; |
| 5 | 5 | |
| 6 | 6 | public function __construct() |
@@ -7,11 +7,11 @@ |
||
| 7 | 7 | |
| 8 | 8 | /* NOTE: in case of error an exception is thrown */ |
| 9 | 9 | |
| 10 | -try{ |
|
| 10 | +try { |
|
| 11 | 11 | |
| 12 | 12 | Viewer::path(__DIR__.'/Views/')->render('default', $data); |
| 13 | 13 | |
| 14 | -}catch(Exception $er){ |
|
| 14 | +}catch (Exception $er) { |
|
| 15 | 15 | |
| 16 | 16 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
| 17 | 17 | |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | |
| 12 | 12 | Viewer::path(__DIR__.'/Views/')->render('default', $data); |
| 13 | 13 | |
| 14 | -}catch(Exception $er){ |
|
| 14 | +} catch(Exception $er){ |
|
| 15 | 15 | |
| 16 | 16 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
| 17 | 17 | |
@@ -9,19 +9,19 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | use HnrAzevedo\Viewer\Viewer; |
| 11 | 11 | |
| 12 | -try{ |
|
| 12 | +try { |
|
| 13 | 13 | $serverRequest = (new Factory())->createServerRequest( |
| 14 | 14 | $_SERVER['REQUEST_METHOD'], |
| 15 | 15 | new Uri($_SERVER['REQUEST_URI']) |
| 16 | 16 | ); |
| 17 | 17 | |
| 18 | - $serverRequest = $serverRequest->withAttribute('viewer',[ |
|
| 18 | + $serverRequest = $serverRequest->withAttribute('viewer', [ |
|
| 19 | 19 | 'path' => 'Views', |
| 20 | 20 | 'file' => 'default', |
| 21 | 21 | 'data' => $data |
| 22 | 22 | ]); |
| 23 | 23 | |
| 24 | - class App implements MiddlewareInterface{ |
|
| 24 | + class App implements MiddlewareInterface { |
|
| 25 | 25 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 26 | 26 | { |
| 27 | 27 | echo $request->getBody()->getContents(); |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | } |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - define('GLOBAL_MIDDLEWARES',[ |
|
| 32 | + define('GLOBAL_MIDDLEWARES', [ |
|
| 33 | 33 | Viewer::class, |
| 34 | 34 | App::class |
| 35 | 35 | ]); |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | |
| 66 | 66 | function runMiddlewares($serverRequest) |
| 67 | 67 | { |
| 68 | - nextExample(new class implements RequestHandlerInterface{ |
|
| 68 | + nextExample(new class implements RequestHandlerInterface { |
|
| 69 | 69 | public function handle(ServerRequestInterface $request): ResponseInterface |
| 70 | 70 | { |
| 71 | 71 | return (new Factory())->createResponse(200); |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | |
| 76 | 76 | runMiddlewares($serverRequest); |
| 77 | 77 | |
| 78 | -}catch(Exception $er){ |
|
| 78 | +}catch (Exception $er) { |
|
| 79 | 79 | |
| 80 | 80 | die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}."); |
| 81 | 81 | |
@@ -75,7 +75,7 @@ |
||
| 75 | 75 | |
| 76 | 76 | runMiddlewares($serverRequest); |
| 77 | 77 | |
| 78 | -}catch(Exception $er){ |
|
| 78 | +} catch(Exception $er){ |
|
| 79 | 79 | |
| 80 | 80 | die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}."); |
| 81 | 81 | |
@@ -14,13 +14,13 @@ discard block |
||
| 14 | 14 | { |
| 15 | 15 | $this->data = array_merge($this->data, $data); |
| 16 | 16 | |
| 17 | - foreach($this->data as $variable => $_){ |
|
| 17 | + foreach ($this->data as $variable => $_) { |
|
| 18 | 18 | $$variable = $_; |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | $_ = (isset($data['_'])) ?? $data['_']; |
| 22 | 22 | |
| 23 | - if(!file_exists($require)){ |
|
| 23 | + if (!file_exists($require)) { |
|
| 24 | 24 | $require = basename($require); |
| 25 | 25 | throw new \RuntimeException("Importation file does not exist: {$require}"); |
| 26 | 26 | } |
@@ -41,13 +41,13 @@ discard block |
||
| 41 | 41 | $inScript = false; |
| 42 | 42 | $inComment = false; |
| 43 | 43 | |
| 44 | - foreach($arrayHtml as $index => $value){ |
|
| 44 | + foreach ($arrayHtml as $index => $value) { |
|
| 45 | 45 | $inScript = $this->checkInScript($inScript, $value); |
| 46 | 46 | |
| 47 | - if($inScript){ |
|
| 47 | + if ($inScript) { |
|
| 48 | 48 | $inComment = $this->checkCommentInScript($inComment, $value); |
| 49 | 49 | |
| 50 | - if(!$this->checkScriptNeed($inComment, $value)){ |
|
| 50 | + if (!$this->checkScriptNeed($inComment, $value)) { |
|
| 51 | 51 | continue; |
| 52 | 52 | } |
| 53 | 53 | |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | $html[$index] = ltrim($value); |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - return implode('',$html); |
|
| 60 | + return implode('', $html); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | protected function getVars(string $buffer, ?bool $treat = true): string |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | protected function replaceVars(string $buffer, array $vars, ?string $prefix = ''): string |
| 71 | 71 | { |
| 72 | 72 | foreach ($vars as $key => $value) { |
| 73 | - switch(gettype($value)){ |
|
| 73 | + switch (gettype($value)) { |
|
| 74 | 74 | case 'array': |
| 75 | 75 | $buffer = $this->replaceArray($buffer, $value, $prefix, $key); |
| 76 | 76 | break; |
@@ -88,9 +88,9 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | protected function replaceValue(string $buffer, $value, ?string $prefix, string $key): string |
| 90 | 90 | { |
| 91 | - if(is_scalar($value)){ |
|
| 92 | - while(strstr($buffer, "{{{$this->treat} \${$prefix}{$key} {$this->treat}}}")){ |
|
| 93 | - $buffer = str_replace("{{{$this->treat} \${$prefix}{$key} {$this->treat}}}", (($this->especial) ? htmlspecialchars($value) : $value) ,$buffer); |
|
| 91 | + if (is_scalar($value)) { |
|
| 92 | + while (strstr($buffer, "{{{$this->treat} \${$prefix}{$key} {$this->treat}}}")) { |
|
| 93 | + $buffer = str_replace("{{{$this->treat} \${$prefix}{$key} {$this->treat}}}", (($this->especial) ? htmlspecialchars($value) : $value), $buffer); |
|
| 94 | 94 | } |
| 95 | 95 | } |
| 96 | 96 | |
@@ -99,18 +99,18 @@ discard block |
||
| 99 | 99 | |
| 100 | 100 | protected function replaceObject(string $buffer, object $obj, ?string $prefix, string $key): string |
| 101 | 101 | { |
| 102 | - $vars = method_exists($obj,'getVars') ? $obj->getVars() : []; |
|
| 102 | + $vars = method_exists($obj, 'getVars') ? $obj->getVars() : []; |
|
| 103 | 103 | $vars = array_merge($vars, get_object_vars($obj)); |
| 104 | 104 | |
| 105 | - foreach($vars as $field => $val){ |
|
| 106 | - $buffer = $this->replaceValue($buffer, $val, $key.'.' , $field); |
|
| 105 | + foreach ($vars as $field => $val) { |
|
| 106 | + $buffer = $this->replaceValue($buffer, $val, $key.'.', $field); |
|
| 107 | 107 | |
| 108 | 108 | |
| 109 | 109 | $buffer = $this->checkArray($buffer, $obj->$field, $key.'.'.$field.'.'); |
| 110 | - $buffer = $this->checkObject($buffer, $obj->$field, $key.'.'.$field.'.', $key.'.'.$field); |
|
| 110 | + $buffer = $this->checkObject($buffer, $obj->$field, $key.'.'.$field.'.', $key.'.'.$field); |
|
| 111 | 111 | |
| 112 | - while(strstr($buffer, "{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}")){ |
|
| 113 | - $buffer = str_replace("{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}", (($this->especial) ? htmlspecialchars($obj->$field) : $obj->$field) ,$buffer); |
|
| 112 | + while (strstr($buffer, "{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}")) { |
|
| 113 | + $buffer = str_replace("{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}", (($this->especial) ? htmlspecialchars($obj->$field) : $obj->$field), $buffer); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | } |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | |
| 120 | 120 | private function checkArray(string $buffer, $obj, string $prefix): string |
| 121 | 121 | { |
| 122 | - if(is_array($obj)){ |
|
| 122 | + if (is_array($obj)) { |
|
| 123 | 123 | $buffer = $this->replaceVars($buffer, $obj, $prefix); |
| 124 | 124 | } |
| 125 | 125 | return $buffer; |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | |
| 128 | 128 | private function checkObject(string $buffer, $obj, string $prefix, string $key): string |
| 129 | 129 | { |
| 130 | - if(is_object($obj)){ |
|
| 130 | + if (is_object($obj)) { |
|
| 131 | 131 | $buffer = $this->replaceObject($buffer, $obj, $prefix, $key); |
| 132 | 132 | } |
| 133 | 133 | return $buffer; |
@@ -135,11 +135,11 @@ discard block |
||
| 135 | 135 | |
| 136 | 136 | protected function replaceArray(string $buffer, array $array, ?string $prefix = '', ?string $key = ''): string |
| 137 | 137 | { |
| 138 | - foreach($array as $field => $val){ |
|
| 138 | + foreach ($array as $field => $val) { |
|
| 139 | 139 | $buffer = $this->replaceValue($buffer, $val, $key.'.', $field); |
| 140 | 140 | |
| 141 | - while(strstr($buffer, "{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}")){ |
|
| 142 | - $buffer = str_replace("{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}", (($this->especial) ? htmlspecialchars($val) : $val) ,$buffer); |
|
| 141 | + while (strstr($buffer, "{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}")) { |
|
| 142 | + $buffer = str_replace("{{{$this->treat} \${$prefix}{$key}.{$field} {$this->treat}}}", (($this->especial) ? htmlspecialchars($val) : $val), $buffer); |
|
| 143 | 143 | } |
| 144 | 144 | } |
| 145 | 145 | return $buffer; |
@@ -147,13 +147,13 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | protected function removeComments(string $buffer): string |
| 149 | 149 | { |
| 150 | - while(strstr($buffer,'<!--')){ |
|
| 150 | + while (strstr($buffer, '<!--')) { |
|
| 151 | 151 | $comment = substr( |
| 152 | 152 | $buffer, |
| 153 | - strpos($buffer,'<!--'), |
|
| 154 | - strpos(strstr($buffer,'<!--'),'-->')+3 |
|
| 153 | + strpos($buffer, '<!--'), |
|
| 154 | + strpos(strstr($buffer, '<!--'), '-->')+3 |
|
| 155 | 155 | ); |
| 156 | - $buffer = str_replace($comment,'',$buffer); |
|
| 156 | + $buffer = str_replace($comment, '', $buffer); |
|
| 157 | 157 | } |
| 158 | 158 | return $buffer; |
| 159 | 159 | } |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | /** |
| 6 | 6 | * @property string $email |
| 7 | 7 | */ |
| 8 | -class User{ |
|
| 8 | +class User { |
|
| 9 | 9 | public string $name = 'Henri Azevedo'; |
| 10 | 10 | public array $values = [1 => 'param1']; |
| 11 | 11 | public Address $address; |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | public function getVars(): array |
| 23 | 23 | { |
| 24 | 24 | $vars = []; |
| 25 | - foreach($this->data as $var => $value){ |
|
| 25 | + foreach ($this->data as $var => $value) { |
|
| 26 | 26 | $vars[$var] = $value; |
| 27 | 27 | } |
| 28 | 28 | return $vars; |