@@ -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 |
@@ -26,14 +26,14 @@ discard block |
||
26 | 26 | |
27 | 27 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
28 | 28 | { |
29 | - if(null === $request->getAttribute('viewer')){ |
|
29 | + if (null===$request->getAttribute('viewer')) { |
|
30 | 30 | throw new \RuntimeException('The path and file parameters for viewing were not defined in the request'); |
31 | 31 | } |
32 | 32 | |
33 | 33 | self::$path = $request->getAttribute('viewer')['path']; |
34 | 34 | |
35 | 35 | $buffer = $this->getInstance()->render($request->getAttribute('viewer')['file'], |
36 | - (isset($request->getAttribute('viewer')['data'])) ? $request->getAttribute('viewer')['data'] : [] ); |
|
36 | + (isset($request->getAttribute('viewer')['data'])) ? $request->getAttribute('viewer')['data'] : []); |
|
37 | 37 | |
38 | 38 | $request = $request->withBody((new Factory())->createStream($buffer)); |
39 | 39 | |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | { |
45 | 45 | self::getInstance()->data = $data; |
46 | 46 | |
47 | - if(!isset(self::$middleware)){ |
|
47 | + if (!isset(self::$middleware)) { |
|
48 | 48 | self::getInstance()->handle($file); |
49 | 49 | return ''; |
50 | 50 | } |
@@ -61,14 +61,14 @@ discard block |
||
61 | 61 | new Uri($_SERVER['REQUEST_URI']) |
62 | 62 | ); |
63 | 63 | |
64 | - $serverRequest = $serverRequest->withAttribute('viewer',[ |
|
64 | + $serverRequest = $serverRequest->withAttribute('viewer', [ |
|
65 | 65 | 'path' => self::$path, |
66 | 66 | 'file' => $file, |
67 | 67 | 'data' => self::getInstance()->data |
68 | 68 | ]); |
69 | 69 | |
70 | 70 | |
71 | - self::getInstance()->process($serverRequest, new class implements RequestHandlerInterface{ |
|
71 | + self::getInstance()->process($serverRequest, new class implements RequestHandlerInterface { |
|
72 | 72 | public function handle(ServerRequestInterface $request): ResponseInterface |
73 | 73 | { |
74 | 74 | echo $request->getBody()->getContents(); |
@@ -86,16 +86,16 @@ discard block |
||
86 | 86 | |
87 | 87 | public static function import(string $file): void |
88 | 88 | { |
89 | - try{ |
|
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 | } |
95 | 95 | |
96 | 96 | private function getBody(string $file) |
97 | 97 | { |
98 | - $buffer = $this->getInstance()->getOB(self::$path . DIRECTORY_SEPARATOR . $file); |
|
98 | + $buffer = $this->getInstance()->getOB(self::$path.DIRECTORY_SEPARATOR.$file); |
|
99 | 99 | $buffer = $this->getInstance()->getVars($buffer); |
100 | 100 | $buffer = $this->getInstance()->getVars($buffer, false); |
101 | 101 | $buffer = $this->getInstance()->removeComments($buffer); |
@@ -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 | } |
@@ -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; |