@@ -4,17 +4,17 @@ |
||
| 4 | 4 | |
| 5 | 5 | class PageContent extends PageComponent |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @inheritDoc |
|
| 9 | - */ |
|
| 7 | + /** |
|
| 8 | + * @inheritDoc |
|
| 9 | + */ |
|
| 10 | 10 | protected function limit(): int |
| 11 | 11 | { |
| 12 | 12 | return 10; |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @inheritDoc |
|
| 17 | - */ |
|
| 15 | + /** |
|
| 16 | + * @inheritDoc |
|
| 17 | + */ |
|
| 18 | 18 | protected function count(): int |
| 19 | 19 | { |
| 20 | 20 | return 150; |
@@ -4,23 +4,23 @@ |
||
| 4 | 4 | |
| 5 | 5 | class PageContent extends PageComponent |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @inheritDoc |
|
| 9 | - */ |
|
| 10 | - protected function limit(): int |
|
| 11 | - { |
|
| 12 | - return 10; |
|
| 13 | - } |
|
| 7 | + /** |
|
| 8 | + * @inheritDoc |
|
| 9 | + */ |
|
| 10 | + protected function limit(): int |
|
| 11 | + { |
|
| 12 | + return 10; |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @inheritDoc |
|
| 17 | - */ |
|
| 18 | - protected function count(): int |
|
| 19 | - { |
|
| 20 | - return 150; |
|
| 21 | - } |
|
| 15 | + /** |
|
| 16 | + * @inheritDoc |
|
| 17 | + */ |
|
| 18 | + protected function count(): int |
|
| 19 | + { |
|
| 20 | + return 150; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - public function html(): string |
|
| 23 | + public function html(): string |
|
| 24 | 24 | { |
| 25 | 25 | return 'Showing page number ' . $this->currentPage(); |
| 26 | 26 | } |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | public function sayHello(bool $isCaps, bool $bNotify = true) |
| 11 | 11 | { |
| 12 | 12 | $text = $isCaps ? 'HELLO WORLD!' : 'Hello World!'; |
| 13 | - if(($bNotify)) |
|
| 13 | + if (($bNotify)) |
|
| 14 | 14 | { |
| 15 | 15 | // $this->response->confirmCommands(2, 'Skip text assignement?'); |
| 16 | 16 | $this->response->assign('div2', 'innerHTML', $text); |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | public function setColor(string $sColor, bool $bNotify = true) |
| 28 | 28 | { |
| 29 | - if(($bNotify)) |
|
| 29 | + if (($bNotify)) |
|
| 30 | 30 | { |
| 31 | 31 | // $this->response->confirmCommands(1, 'Skip color assignement?'); |
| 32 | 32 | $this->response->assign('div2', 'style.color', $sColor); |
@@ -16,8 +16,7 @@ discard block |
||
| 16 | 16 | $this->response->assign('div2', 'innerHTML', $text); |
| 17 | 17 | // $this->response->confirmCommands(1, 'Skip text notification?'); |
| 18 | 18 | $this->response->dialog->success("div2 text is now $text"); |
| 19 | - } |
|
| 20 | - else |
|
| 19 | + } else |
|
| 21 | 20 | { |
| 22 | 21 | // $this->response->confirmCommands(1, 'Skip text assignement?'); |
| 23 | 22 | $this->response->assign('div2', 'innerHTML', $text); |
@@ -32,8 +31,7 @@ discard block |
||
| 32 | 31 | $this->response->assign('div2', 'style.color', $sColor); |
| 33 | 32 | // $this->response->confirmCommands(1, 'Skip color assignement?'); |
| 34 | 33 | $this->response->dialog->success("div2 color is now $sColor"); |
| 35 | - } |
|
| 36 | - else |
|
| 34 | + } else |
|
| 37 | 35 | { |
| 38 | 36 | // $this->response->confirmCommands(1, 'Skip color assignement?'); |
| 39 | 37 | $this->response->assign('div2', 'style.color', $sColor); |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | $text = $isCaps ? 'HELLO WORLD!' : 'Hello World!'; |
| 8 | 8 | $xResponse = jaxon()->newResponse(); |
| 9 | 9 | $xResponse->assign('div2', 'innerHTML', $text); |
| 10 | - if(($bNotify)) |
|
| 10 | + if (($bNotify)) |
|
| 11 | 11 | $xResponse->dialog->success("div2 text is now $text"); |
| 12 | 12 | } |
| 13 | 13 | |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | { |
| 16 | 16 | $xResponse = jaxon()->newResponse(); |
| 17 | 17 | $xResponse->assign('div2', 'style.color', $sColor); |
| 18 | - if(($bNotify)) |
|
| 18 | + if (($bNotify)) |
|
| 19 | 19 | $xResponse->dialog->success("div2 color is now $sColor"); |
| 20 | 20 | } |
| 21 | 21 | |
@@ -7,16 +7,18 @@ |
||
| 7 | 7 | $text = $isCaps ? 'HELLO WORLD!' : 'Hello World!'; |
| 8 | 8 | $xResponse = jaxon()->newResponse(); |
| 9 | 9 | $xResponse->assign('div2', 'innerHTML', $text); |
| 10 | - if(($bNotify)) |
|
| 11 | - $xResponse->dialog->success("div2 text is now $text"); |
|
| 10 | + if(($bNotify)) { |
|
| 11 | + $xResponse->dialog->success("div2 text is now $text"); |
|
| 12 | + } |
|
| 12 | 13 | } |
| 13 | 14 | |
| 14 | 15 | public function setColor(string $sColor, bool $bNotify = true) |
| 15 | 16 | { |
| 16 | 17 | $xResponse = jaxon()->newResponse(); |
| 17 | 18 | $xResponse->assign('div2', 'style.color', $sColor); |
| 18 | - if(($bNotify)) |
|
| 19 | - $xResponse->dialog->success("div2 color is now $sColor"); |
|
| 19 | + if(($bNotify)) { |
|
| 20 | + $xResponse->dialog->success("div2 color is now $sColor"); |
|
| 21 | + } |
|
| 20 | 22 | } |
| 21 | 23 | |
| 22 | 24 | public function showDialog() |
@@ -4,7 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | $menus = menu_entries(); |
| 6 | 6 | $example = menu_current(); |
| 7 | -if(isset($menus[$example])) |
|
| 7 | +if (isset($menus[$example])) |
|
| 8 | 8 | { |
| 9 | 9 | renderExample($example); |
| 10 | 10 | exit(); |
@@ -4,7 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | $menus = menu_entries(); |
| 6 | 6 | $example = menu_current(); |
| 7 | -if(isset($menus[$example])) |
|
| 7 | +if (isset($menus[$example])) |
|
| 8 | 8 | { |
| 9 | 9 | require dirname(__DIR__) . "/examples/$example/code.php"; |
| 10 | 10 | |
@@ -43,16 +43,16 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | private function setScheme(array $aServerParams): void |
| 45 | 45 | { |
| 46 | - if(isset($this->aUrl['scheme'])) |
|
| 46 | + if (isset($this->aUrl['scheme'])) |
|
| 47 | 47 | { |
| 48 | 48 | return; |
| 49 | 49 | } |
| 50 | - if(isset($aServerParams['HTTP_SCHEME'])) |
|
| 50 | + if (isset($aServerParams['HTTP_SCHEME'])) |
|
| 51 | 51 | { |
| 52 | 52 | $this->aUrl['scheme'] = $aServerParams['HTTP_SCHEME']; |
| 53 | 53 | return; |
| 54 | 54 | } |
| 55 | - if((isset($aServerParams['HTTPS']) && strtolower($aServerParams['HTTPS']) === 'on') || |
|
| 55 | + if ((isset($aServerParams['HTTPS']) && strtolower($aServerParams['HTTPS']) === 'on') || |
|
| 56 | 56 | (isset($aServerParams['HTTP_X_FORWARDED_SSL']) && $aServerParams['HTTP_X_FORWARDED_SSL'] === 'on') || |
| 57 | 57 | (isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] === 'https')) |
| 58 | 58 | { |
@@ -72,11 +72,11 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | private function setHostFromServer(array $aServerParams, string $sKey): void |
| 74 | 74 | { |
| 75 | - if(isset($this->aUrl['host']) || empty($aServerParams[$sKey])) |
|
| 75 | + if (isset($this->aUrl['host']) || empty($aServerParams[$sKey])) |
|
| 76 | 76 | { |
| 77 | 77 | return; |
| 78 | 78 | } |
| 79 | - if(strpos($aServerParams[$sKey], ':') === false) |
|
| 79 | + if (strpos($aServerParams[$sKey], ':') === false) |
|
| 80 | 80 | { |
| 81 | 81 | $this->aUrl['host'] = $aServerParams[$sKey]; |
| 82 | 82 | return; |
@@ -95,11 +95,11 @@ discard block |
||
| 95 | 95 | $this->setHostFromServer($aServerParams, 'HTTP_X_FORWARDED_HOST'); |
| 96 | 96 | $this->setHostFromServer($aServerParams, 'HTTP_HOST'); |
| 97 | 97 | $this->setHostFromServer($aServerParams, 'SERVER_NAME'); |
| 98 | - if(empty($this->aUrl['host'])) |
|
| 98 | + if (empty($this->aUrl['host'])) |
|
| 99 | 99 | { |
| 100 | 100 | throw new UriException(); |
| 101 | 101 | } |
| 102 | - if(empty($this->aUrl['port']) && isset($aServerParams['SERVER_PORT'])) |
|
| 102 | + if (empty($this->aUrl['port']) && isset($aServerParams['SERVER_PORT'])) |
|
| 103 | 103 | { |
| 104 | 104 | $this->aUrl['port'] = $aServerParams['SERVER_PORT']; |
| 105 | 105 | } |
@@ -112,16 +112,16 @@ discard block |
||
| 112 | 112 | */ |
| 113 | 113 | private function setPath(array $aServerParams): void |
| 114 | 114 | { |
| 115 | - if(isset($this->aUrl['path']) && strlen(basename($this->aUrl['path'])) === 0) |
|
| 115 | + if (isset($this->aUrl['path']) && strlen(basename($this->aUrl['path'])) === 0) |
|
| 116 | 116 | { |
| 117 | 117 | unset($this->aUrl['path']); |
| 118 | 118 | } |
| 119 | - if(isset($this->aUrl['path'])) |
|
| 119 | + if (isset($this->aUrl['path'])) |
|
| 120 | 120 | { |
| 121 | 121 | return; |
| 122 | 122 | } |
| 123 | 123 | $aPath = parse_url($aServerParams['PATH_INFO'] ?? ($aServerParams['PHP_SELF'] ?? '')); |
| 124 | - if(isset($aPath['path'])) |
|
| 124 | + if (isset($aPath['path'])) |
|
| 125 | 125 | { |
| 126 | 126 | $this->aUrl['path'] = $aPath['path']; |
| 127 | 127 | } |
@@ -140,12 +140,12 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | private function getUser(): string |
| 142 | 142 | { |
| 143 | - if(empty($this->aUrl['user'])) |
|
| 143 | + if (empty($this->aUrl['user'])) |
|
| 144 | 144 | { |
| 145 | 145 | return ''; |
| 146 | 146 | } |
| 147 | 147 | $sUrl = $this->aUrl['user']; |
| 148 | - if(isset($this->aUrl['pass'])) |
|
| 148 | + if (isset($this->aUrl['pass'])) |
|
| 149 | 149 | { |
| 150 | 150 | $sUrl .= ':' . $this->aUrl['pass']; |
| 151 | 151 | } |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | */ |
| 158 | 158 | private function getPort(): string |
| 159 | 159 | { |
| 160 | - if(isset($this->aUrl['port']) && |
|
| 160 | + if (isset($this->aUrl['port']) && |
|
| 161 | 161 | (($this->aUrl['scheme'] === 'http' && $this->aUrl['port'] != 80) || |
| 162 | 162 | ($this->aUrl['scheme'] === 'https' && $this->aUrl['port'] != 443))) |
| 163 | 163 | { |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | */ |
| 174 | 174 | private function setQuery(array $aServerParams): void |
| 175 | 175 | { |
| 176 | - if(empty($this->aUrl['query'])) |
|
| 176 | + if (empty($this->aUrl['query'])) |
|
| 177 | 177 | { |
| 178 | 178 | $this->aUrl['query'] = $aServerParams['QUERY_STRING'] ?? ''; |
| 179 | 179 | } |
@@ -184,19 +184,19 @@ discard block |
||
| 184 | 184 | */ |
| 185 | 185 | private function getQuery(): string |
| 186 | 186 | { |
| 187 | - if(empty($this->aUrl['query'])) |
|
| 187 | + if (empty($this->aUrl['query'])) |
|
| 188 | 188 | { |
| 189 | 189 | return ''; |
| 190 | 190 | } |
| 191 | 191 | $aQueries = explode('&', $this->aUrl['query']); |
| 192 | - foreach($aQueries as $sKey => $sQuery) |
|
| 192 | + foreach ($aQueries as $sKey => $sQuery) |
|
| 193 | 193 | { |
| 194 | - if(substr($sQuery, 0, 11) === 'jxnGenerate') |
|
| 194 | + if (substr($sQuery, 0, 11) === 'jxnGenerate') |
|
| 195 | 195 | { |
| 196 | 196 | unset($aQueries[$sKey]); |
| 197 | 197 | } |
| 198 | 198 | } |
| 199 | - if(empty($aQueries)) |
|
| 199 | + if (empty($aQueries)) |
|
| 200 | 200 | { |
| 201 | 201 | return ''; |
| 202 | 202 | } |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | { |
| 216 | 216 | $this->aUrl = []; |
| 217 | 217 | // Try to get the request URL |
| 218 | - if(isset($aServerParams['REQUEST_URI'])) |
|
| 218 | + if (isset($aServerParams['REQUEST_URI'])) |
|
| 219 | 219 | { |
| 220 | 220 | $this->aUrl = parse_url($aServerParams['REQUEST_URI']); |
| 221 | 221 | } |
@@ -255,17 +255,17 @@ discard block |
||
| 255 | 255 | { |
| 256 | 256 | $aQueryParts = []; |
| 257 | 257 | parse_str($sQueryPart, $aQueryParts); |
| 258 | - if(empty($aQueryParts)) |
|
| 258 | + if (empty($aQueryParts)) |
|
| 259 | 259 | { |
| 260 | 260 | // Couldn't break up the query, but there's one there. |
| 261 | 261 | // Possibly "http://url/page.html?query1234" type of query? |
| 262 | 262 | // Try to get data from the server environment var. |
| 263 | 263 | parse_str($aServerParams['QUERY_STRING'] ?? '', $aQueryParts); |
| 264 | 264 | } |
| 265 | - if(($aQueryParts)) |
|
| 265 | + if (($aQueryParts)) |
|
| 266 | 266 | { |
| 267 | 267 | $aNewQueryParts = []; |
| 268 | - foreach($aQueryParts as $sKey => $sValue) |
|
| 268 | + foreach ($aQueryParts as $sKey => $sValue) |
|
| 269 | 269 | { |
| 270 | 270 | $aNewQueryParts[] = $this->makeQueryPart($sQueryPart, $sKey, $sValue); |
| 271 | 271 | } |
@@ -288,13 +288,13 @@ discard block |
||
| 288 | 288 | // Can't just use parse_url() cos we could be dealing with a relative URL which parse_url() can't deal with. |
| 289 | 289 | $sURL = trim($sURL); |
| 290 | 290 | $nQueryStart = strpos($sURL, '?', strrpos($sURL, '/')); |
| 291 | - if($nQueryStart === false) |
|
| 291 | + if ($nQueryStart === false) |
|
| 292 | 292 | { |
| 293 | 293 | return $sURL; |
| 294 | 294 | } |
| 295 | 295 | $nQueryStart++; |
| 296 | 296 | $nQueryEnd = strpos($sURL, '#', $nQueryStart); |
| 297 | - if($nQueryEnd === false) |
|
| 297 | + if ($nQueryEnd === false) |
|
| 298 | 298 | { |
| 299 | 299 | $nQueryEnd = strlen($sURL); |
| 300 | 300 | } |
@@ -29,40 +29,40 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | class UriDetector |
| 31 | 31 | { |
| 32 | - /** |
|
| 32 | +/** |
|
| 33 | 33 | * The URL components |
| 34 | 34 | * |
| 35 | 35 | * @var array |
| 36 | 36 | */ |
| 37 | - protected $aUrl; |
|
| 37 | +protected $aUrl; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 39 | +/** |
|
| 40 | 40 | * @param array $aServerParams The server environment variables |
| 41 | 41 | * |
| 42 | 42 | * @return void |
| 43 | 43 | */ |
| 44 | - private function setScheme(array $aServerParams): void |
|
| 45 | - { |
|
| 46 | - if(isset($this->aUrl['scheme'])) |
|
| 47 | - { |
|
| 48 | - return; |
|
| 49 | - } |
|
| 50 | - if(isset($aServerParams['HTTP_SCHEME'])) |
|
| 51 | - { |
|
| 52 | - $this->aUrl['scheme'] = $aServerParams['HTTP_SCHEME']; |
|
| 53 | - return; |
|
| 54 | - } |
|
| 55 | - if((isset($aServerParams['HTTPS']) && strtolower($aServerParams['HTTPS']) === 'on') || |
|
| 56 | - (isset($aServerParams['HTTP_X_FORWARDED_SSL']) && $aServerParams['HTTP_X_FORWARDED_SSL'] === 'on') || |
|
| 57 | - (isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] === 'https')) |
|
| 58 | - { |
|
| 59 | - $this->aUrl['scheme'] = 'https'; |
|
| 60 | - return; |
|
| 61 | - } |
|
| 62 | - $this->aUrl['scheme'] = 'http'; |
|
| 63 | - } |
|
| 44 | +private function setScheme(array $aServerParams): void |
|
| 45 | +{ |
|
| 46 | +if(isset($this->aUrl['scheme'])) |
|
| 47 | +{ |
|
| 48 | +return; |
|
| 49 | +} |
|
| 50 | +if(isset($aServerParams['HTTP_SCHEME'])) |
|
| 51 | +{ |
|
| 52 | +$this->aUrl['scheme'] = $aServerParams['HTTP_SCHEME']; |
|
| 53 | +return; |
|
| 54 | +} |
|
| 55 | +if((isset($aServerParams['HTTPS']) && strtolower($aServerParams['HTTPS']) === 'on') || |
|
| 56 | +(isset($aServerParams['HTTP_X_FORWARDED_SSL']) && $aServerParams['HTTP_X_FORWARDED_SSL'] === 'on') || |
|
| 57 | +(isset($aServerParams['HTTP_X_FORWARDED_PROTO']) && $aServerParams['HTTP_X_FORWARDED_PROTO'] === 'https')) |
|
| 58 | +{ |
|
| 59 | +$this->aUrl['scheme'] = 'https'; |
|
| 60 | +return; |
|
| 61 | +} |
|
| 62 | +$this->aUrl['scheme'] = 'http'; |
|
| 63 | +} |
|
| 64 | 64 | |
| 65 | - /** |
|
| 65 | +/** |
|
| 66 | 66 | * Get the URL from the $aServerParams var |
| 67 | 67 | * |
| 68 | 68 | * @param array $aServerParams The server environment variables |
@@ -70,140 +70,140 @@ discard block |
||
| 70 | 70 | * |
| 71 | 71 | * @return void |
| 72 | 72 | */ |
| 73 | - private function setHostFromServer(array $aServerParams, string $sKey): void |
|
| 74 | - { |
|
| 75 | - if(isset($this->aUrl['host']) || empty($aServerParams[$sKey])) |
|
| 76 | - { |
|
| 77 | - return; |
|
| 78 | - } |
|
| 79 | - if(strpos($aServerParams[$sKey], ':') === false) |
|
| 80 | - { |
|
| 81 | - $this->aUrl['host'] = $aServerParams[$sKey]; |
|
| 82 | - return; |
|
| 83 | - } |
|
| 84 | - list($this->aUrl['host'], $this->aUrl['port']) = explode(':', $aServerParams[$sKey]); |
|
| 85 | - } |
|
| 73 | +private function setHostFromServer(array $aServerParams, string $sKey): void |
|
| 74 | +{ |
|
| 75 | +if(isset($this->aUrl['host']) || empty($aServerParams[$sKey])) |
|
| 76 | +{ |
|
| 77 | +return; |
|
| 78 | +} |
|
| 79 | +if(strpos($aServerParams[$sKey], ':') === false) |
|
| 80 | +{ |
|
| 81 | +$this->aUrl['host'] = $aServerParams[$sKey]; |
|
| 82 | +return; |
|
| 83 | +} |
|
| 84 | +list($this->aUrl['host'], $this->aUrl['port']) = explode(':', $aServerParams[$sKey]); |
|
| 85 | +} |
|
| 86 | 86 | |
| 87 | - /** |
|
| 87 | +/** |
|
| 88 | 88 | * @param array $aServerParams The server environment variables |
| 89 | 89 | * |
| 90 | 90 | * @return void |
| 91 | 91 | * @throws UriException |
| 92 | 92 | */ |
| 93 | - private function setHost(array $aServerParams): void |
|
| 94 | - { |
|
| 95 | - $this->setHostFromServer($aServerParams, 'HTTP_X_FORWARDED_HOST'); |
|
| 96 | - $this->setHostFromServer($aServerParams, 'HTTP_HOST'); |
|
| 97 | - $this->setHostFromServer($aServerParams, 'SERVER_NAME'); |
|
| 98 | - if(empty($this->aUrl['host'])) |
|
| 99 | - { |
|
| 100 | - throw new UriException(); |
|
| 101 | - } |
|
| 102 | - if(empty($this->aUrl['port']) && isset($aServerParams['SERVER_PORT'])) |
|
| 103 | - { |
|
| 104 | - $this->aUrl['port'] = $aServerParams['SERVER_PORT']; |
|
| 105 | - } |
|
| 106 | - } |
|
| 93 | +private function setHost(array $aServerParams): void |
|
| 94 | +{ |
|
| 95 | +$this->setHostFromServer($aServerParams, 'HTTP_X_FORWARDED_HOST'); |
|
| 96 | +$this->setHostFromServer($aServerParams, 'HTTP_HOST'); |
|
| 97 | +$this->setHostFromServer($aServerParams, 'SERVER_NAME'); |
|
| 98 | +if(empty($this->aUrl['host'])) |
|
| 99 | +{ |
|
| 100 | +throw new UriException(); |
|
| 101 | +} |
|
| 102 | +if(empty($this->aUrl['port']) && isset($aServerParams['SERVER_PORT'])) |
|
| 103 | +{ |
|
| 104 | +$this->aUrl['port'] = $aServerParams['SERVER_PORT']; |
|
| 105 | +} |
|
| 106 | +} |
|
| 107 | 107 | |
| 108 | - /** |
|
| 108 | +/** |
|
| 109 | 109 | * @param array $aServerParams The server environment variables |
| 110 | 110 | * |
| 111 | 111 | * @return void |
| 112 | 112 | */ |
| 113 | - private function setPath(array $aServerParams): void |
|
| 114 | - { |
|
| 115 | - if(isset($this->aUrl['path']) && strlen(basename($this->aUrl['path'])) === 0) |
|
| 116 | - { |
|
| 117 | - unset($this->aUrl['path']); |
|
| 118 | - } |
|
| 119 | - if(isset($this->aUrl['path'])) |
|
| 120 | - { |
|
| 121 | - return; |
|
| 122 | - } |
|
| 123 | - $aPath = parse_url($aServerParams['PATH_INFO'] ?? ($aServerParams['PHP_SELF'] ?? '')); |
|
| 124 | - if(isset($aPath['path'])) |
|
| 125 | - { |
|
| 126 | - $this->aUrl['path'] = $aPath['path']; |
|
| 127 | - } |
|
| 128 | - } |
|
| 113 | +private function setPath(array $aServerParams): void |
|
| 114 | +{ |
|
| 115 | +if(isset($this->aUrl['path']) && strlen(basename($this->aUrl['path'])) === 0) |
|
| 116 | +{ |
|
| 117 | +unset($this->aUrl['path']); |
|
| 118 | +} |
|
| 119 | +if(isset($this->aUrl['path'])) |
|
| 120 | +{ |
|
| 121 | +return; |
|
| 122 | +} |
|
| 123 | +$aPath = parse_url($aServerParams['PATH_INFO'] ?? ($aServerParams['PHP_SELF'] ?? '')); |
|
| 124 | +if(isset($aPath['path'])) |
|
| 125 | +{ |
|
| 126 | +$this->aUrl['path'] = $aPath['path']; |
|
| 127 | +} |
|
| 128 | +} |
|
| 129 | 129 | |
| 130 | - /** |
|
| 130 | +/** |
|
| 131 | 131 | * @return string |
| 132 | 132 | */ |
| 133 | - private function getPath(): string |
|
| 134 | - { |
|
| 135 | - return '/' . ltrim($this->aUrl['path'], '/'); |
|
| 136 | - } |
|
| 133 | +private function getPath(): string |
|
| 134 | +{ |
|
| 135 | +return '/' . ltrim($this->aUrl['path'], '/'); |
|
| 136 | +} |
|
| 137 | 137 | |
| 138 | - /** |
|
| 138 | +/** |
|
| 139 | 139 | * @return string |
| 140 | 140 | */ |
| 141 | - private function getUser(): string |
|
| 142 | - { |
|
| 143 | - if(empty($this->aUrl['user'])) |
|
| 144 | - { |
|
| 145 | - return ''; |
|
| 146 | - } |
|
| 147 | - $sUrl = $this->aUrl['user']; |
|
| 148 | - if(isset($this->aUrl['pass'])) |
|
| 149 | - { |
|
| 150 | - $sUrl .= ':' . $this->aUrl['pass']; |
|
| 151 | - } |
|
| 152 | - return $sUrl . '@'; |
|
| 153 | - } |
|
| 141 | +private function getUser(): string |
|
| 142 | +{ |
|
| 143 | +if(empty($this->aUrl['user'])) |
|
| 144 | +{ |
|
| 145 | +return ''; |
|
| 146 | +} |
|
| 147 | +$sUrl = $this->aUrl['user']; |
|
| 148 | +if(isset($this->aUrl['pass'])) |
|
| 149 | +{ |
|
| 150 | +$sUrl .= ':' . $this->aUrl['pass']; |
|
| 151 | +} |
|
| 152 | +return $sUrl . '@'; |
|
| 153 | +} |
|
| 154 | 154 | |
| 155 | - /** |
|
| 155 | +/** |
|
| 156 | 156 | * @return string |
| 157 | 157 | */ |
| 158 | - private function getPort(): string |
|
| 159 | - { |
|
| 160 | - if(isset($this->aUrl['port']) && |
|
| 161 | - (($this->aUrl['scheme'] === 'http' && $this->aUrl['port'] != 80) || |
|
| 162 | - ($this->aUrl['scheme'] === 'https' && $this->aUrl['port'] != 443))) |
|
| 163 | - { |
|
| 164 | - return ':' . $this->aUrl['port']; |
|
| 165 | - } |
|
| 166 | - return ''; |
|
| 167 | - } |
|
| 158 | +private function getPort(): string |
|
| 159 | +{ |
|
| 160 | +if(isset($this->aUrl['port']) && |
|
| 161 | +(($this->aUrl['scheme'] === 'http' && $this->aUrl['port'] != 80) || |
|
| 162 | + ($this->aUrl['scheme'] === 'https' && $this->aUrl['port'] != 443))) |
|
| 163 | +{ |
|
| 164 | +return ':' . $this->aUrl['port']; |
|
| 165 | +} |
|
| 166 | +return ''; |
|
| 167 | +} |
|
| 168 | 168 | |
| 169 | - /** |
|
| 169 | +/** |
|
| 170 | 170 | * @param array $aServerParams The server environment variables |
| 171 | 171 | * |
| 172 | 172 | * @return void |
| 173 | 173 | */ |
| 174 | - private function setQuery(array $aServerParams): void |
|
| 175 | - { |
|
| 176 | - if(empty($this->aUrl['query'])) |
|
| 177 | - { |
|
| 178 | - $this->aUrl['query'] = $aServerParams['QUERY_STRING'] ?? ''; |
|
| 179 | - } |
|
| 180 | - } |
|
| 174 | +private function setQuery(array $aServerParams): void |
|
| 175 | +{ |
|
| 176 | +if(empty($this->aUrl['query'])) |
|
| 177 | +{ |
|
| 178 | +$this->aUrl['query'] = $aServerParams['QUERY_STRING'] ?? ''; |
|
| 179 | +} |
|
| 180 | +} |
|
| 181 | 181 | |
| 182 | - /** |
|
| 182 | +/** |
|
| 183 | 183 | * @return string |
| 184 | 184 | */ |
| 185 | - private function getQuery(): string |
|
| 186 | - { |
|
| 187 | - if(empty($this->aUrl['query'])) |
|
| 188 | - { |
|
| 189 | - return ''; |
|
| 190 | - } |
|
| 191 | - $aQueries = explode('&', $this->aUrl['query']); |
|
| 192 | - foreach($aQueries as $sKey => $sQuery) |
|
| 193 | - { |
|
| 194 | - if(substr($sQuery, 0, 11) === 'jxnGenerate') |
|
| 195 | - { |
|
| 196 | - unset($aQueries[$sKey]); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - if(empty($aQueries)) |
|
| 200 | - { |
|
| 201 | - return ''; |
|
| 202 | - } |
|
| 203 | - return '?' . implode("&", $aQueries); |
|
| 204 | - } |
|
| 185 | +private function getQuery(): string |
|
| 186 | +{ |
|
| 187 | +if(empty($this->aUrl['query'])) |
|
| 188 | +{ |
|
| 189 | +return ''; |
|
| 190 | +} |
|
| 191 | +$aQueries = explode('&', $this->aUrl['query']); |
|
| 192 | +foreach($aQueries as $sKey => $sQuery) |
|
| 193 | +{ |
|
| 194 | +if(substr($sQuery, 0, 11) === 'jxnGenerate') |
|
| 195 | +{ |
|
| 196 | + unset($aQueries[$sKey]); |
|
| 197 | +} |
|
| 198 | +} |
|
| 199 | +if(empty($aQueries)) |
|
| 200 | +{ |
|
| 201 | +return ''; |
|
| 202 | +} |
|
| 203 | +return '?' . implode("&", $aQueries); |
|
| 204 | +} |
|
| 205 | 205 | |
| 206 | - /** |
|
| 206 | +/** |
|
| 207 | 207 | * Detect the URI of the current request |
| 208 | 208 | * |
| 209 | 209 | * @param array $aServerParams The server environment variables |
@@ -211,70 +211,70 @@ discard block |
||
| 211 | 211 | * @return string |
| 212 | 212 | * @throws UriException |
| 213 | 213 | */ |
| 214 | - public function detect(array $aServerParams): string |
|
| 215 | - { |
|
| 216 | - $this->aUrl = []; |
|
| 217 | - // Try to get the request URL |
|
| 218 | - if(isset($aServerParams['REQUEST_URI'])) |
|
| 219 | - { |
|
| 220 | - $this->aUrl = parse_url($aServerParams['REQUEST_URI']); |
|
| 221 | - } |
|
| 214 | +public function detect(array $aServerParams): string |
|
| 215 | +{ |
|
| 216 | +$this->aUrl = []; |
|
| 217 | +// Try to get the request URL |
|
| 218 | +if(isset($aServerParams['REQUEST_URI'])) |
|
| 219 | +{ |
|
| 220 | +$this->aUrl = parse_url($aServerParams['REQUEST_URI']); |
|
| 221 | +} |
|
| 222 | 222 | |
| 223 | - // Fill in the empty values |
|
| 224 | - $this->setScheme($aServerParams); |
|
| 225 | - $this->setHost($aServerParams); |
|
| 226 | - $this->setPath($aServerParams); |
|
| 227 | - $this->setQuery($aServerParams); |
|
| 223 | +// Fill in the empty values |
|
| 224 | +$this->setScheme($aServerParams); |
|
| 225 | +$this->setHost($aServerParams); |
|
| 226 | +$this->setPath($aServerParams); |
|
| 227 | +$this->setQuery($aServerParams); |
|
| 228 | 228 | |
| 229 | - // Build the URL: Start with scheme, user and pass |
|
| 230 | - return $this->aUrl['scheme'] . '://' . $this->getUser() . $this->aUrl['host'] . |
|
| 231 | - $this->getPort() . str_replace(['"', "'", '<', '>'], |
|
| 232 | - ['%22', '%27', '%3C', '%3E'], $this->getPath() . $this->getQuery()); |
|
| 233 | - } |
|
| 229 | +// Build the URL: Start with scheme, user and pass |
|
| 230 | +return $this->aUrl['scheme'] . '://' . $this->getUser() . $this->aUrl['host'] . |
|
| 231 | +$this->getPort() . str_replace(['"', "'", '<', '>'], |
|
| 232 | + ['%22', '%27', '%3C', '%3E'], $this->getPath() . $this->getQuery()); |
|
| 233 | +} |
|
| 234 | 234 | |
| 235 | - /** |
|
| 235 | +/** |
|
| 236 | 236 | * @param string $sQueryPart |
| 237 | 237 | * @param string $sKey |
| 238 | 238 | * @param string $sValue |
| 239 | 239 | * |
| 240 | 240 | * @return string |
| 241 | 241 | */ |
| 242 | - private function makeQueryPart(string $sQueryPart, string $sKey, string $sValue): string |
|
| 243 | - { |
|
| 244 | - return $sValue === '' && strpos($sQueryPart, "$sKey=") === false ? |
|
| 245 | - rawurlencode($sKey) : rawurlencode($sKey) . '=' . rawurlencode($sValue); |
|
| 246 | - } |
|
| 242 | +private function makeQueryPart(string $sQueryPart, string $sKey, string $sValue): string |
|
| 243 | +{ |
|
| 244 | +return $sValue === '' && strpos($sQueryPart, "$sKey=") === false ? |
|
| 245 | +rawurlencode($sKey) : rawurlencode($sKey) . '=' . rawurlencode($sValue); |
|
| 246 | +} |
|
| 247 | 247 | |
| 248 | - /** |
|
| 248 | +/** |
|
| 249 | 249 | * @param string $sQueryPart |
| 250 | 250 | * @param array $aServerParams |
| 251 | 251 | * |
| 252 | 252 | * @return string |
| 253 | 253 | */ |
| 254 | - private function parseQueryPart(string $sQueryPart, array $aServerParams): string |
|
| 255 | - { |
|
| 256 | - $aQueryParts = []; |
|
| 257 | - parse_str($sQueryPart, $aQueryParts); |
|
| 258 | - if(empty($aQueryParts)) |
|
| 259 | - { |
|
| 260 | - // Couldn't break up the query, but there's one there. |
|
| 261 | - // Possibly "http://url/page.html?query1234" type of query? |
|
| 262 | - // Try to get data from the server environment var. |
|
| 263 | - parse_str($aServerParams['QUERY_STRING'] ?? '', $aQueryParts); |
|
| 264 | - } |
|
| 265 | - if(($aQueryParts)) |
|
| 266 | - { |
|
| 267 | - $aNewQueryParts = []; |
|
| 268 | - foreach($aQueryParts as $sKey => $sValue) |
|
| 269 | - { |
|
| 270 | - $aNewQueryParts[] = $this->makeQueryPart($sQueryPart, $sKey, $sValue); |
|
| 271 | - } |
|
| 272 | - return '?' . implode('&', $aNewQueryParts); |
|
| 273 | - } |
|
| 274 | - return trim($sQueryPart); |
|
| 275 | - } |
|
| 254 | +private function parseQueryPart(string $sQueryPart, array $aServerParams): string |
|
| 255 | +{ |
|
| 256 | +$aQueryParts = []; |
|
| 257 | +parse_str($sQueryPart, $aQueryParts); |
|
| 258 | +if(empty($aQueryParts)) |
|
| 259 | +{ |
|
| 260 | +// Couldn't break up the query, but there's one there. |
|
| 261 | +// Possibly "http://url/page.html?query1234" type of query? |
|
| 262 | +// Try to get data from the server environment var. |
|
| 263 | +parse_str($aServerParams['QUERY_STRING'] ?? '', $aQueryParts); |
|
| 264 | +} |
|
| 265 | +if(($aQueryParts)) |
|
| 266 | +{ |
|
| 267 | +$aNewQueryParts = []; |
|
| 268 | +foreach($aQueryParts as $sKey => $sValue) |
|
| 269 | +{ |
|
| 270 | + $aNewQueryParts[] = $this->makeQueryPart($sQueryPart, $sKey, $sValue); |
|
| 271 | +} |
|
| 272 | +return '?' . implode('&', $aNewQueryParts); |
|
| 273 | +} |
|
| 274 | +return trim($sQueryPart); |
|
| 275 | +} |
|
| 276 | 276 | |
| 277 | - /** |
|
| 277 | +/** |
|
| 278 | 278 | * Make the specified URL suitable for redirect |
| 279 | 279 | * |
| 280 | 280 | * @param string $sURL The relative or fully qualified URL |
@@ -282,23 +282,23 @@ discard block |
||
| 282 | 282 | * |
| 283 | 283 | * @return string |
| 284 | 284 | */ |
| 285 | - public function redirect(string $sURL, array $aServerParams): string |
|
| 286 | - { |
|
| 287 | - // We need to parse the query part so that the values are rawurlencode()'ed. |
|
| 288 | - // Can't just use parse_url() cos we could be dealing with a relative URL which parse_url() can't deal with. |
|
| 289 | - $sURL = trim($sURL); |
|
| 290 | - $nQueryStart = strpos($sURL, '?', strrpos($sURL, '/')); |
|
| 291 | - if($nQueryStart === false) |
|
| 292 | - { |
|
| 293 | - return $sURL; |
|
| 294 | - } |
|
| 295 | - $nQueryStart++; |
|
| 296 | - $nQueryEnd = strpos($sURL, '#', $nQueryStart); |
|
| 297 | - if($nQueryEnd === false) |
|
| 298 | - { |
|
| 299 | - $nQueryEnd = strlen($sURL); |
|
| 300 | - } |
|
| 301 | - $sQueryPart = substr($sURL, $nQueryStart, $nQueryEnd - $nQueryStart); |
|
| 302 | - return str_replace('?' . $sQueryPart, $this->parseQueryPart($sQueryPart, $aServerParams), $sURL); |
|
| 303 | - } |
|
| 285 | +public function redirect(string $sURL, array $aServerParams): string |
|
| 286 | +{ |
|
| 287 | +// We need to parse the query part so that the values are rawurlencode()'ed. |
|
| 288 | +// Can't just use parse_url() cos we could be dealing with a relative URL which parse_url() can't deal with. |
|
| 289 | +$sURL = trim($sURL); |
|
| 290 | +$nQueryStart = strpos($sURL, '?', strrpos($sURL, '/')); |
|
| 291 | +if($nQueryStart === false) |
|
| 292 | +{ |
|
| 293 | +return $sURL; |
|
| 294 | +} |
|
| 295 | +$nQueryStart++; |
|
| 296 | +$nQueryEnd = strpos($sURL, '#', $nQueryStart); |
|
| 297 | +if($nQueryEnd === false) |
|
| 298 | +{ |
|
| 299 | +$nQueryEnd = strlen($sURL); |
|
| 300 | +} |
|
| 301 | +$sQueryPart = substr($sURL, $nQueryStart, $nQueryEnd - $nQueryStart); |
|
| 302 | +return str_replace('?' . $sQueryPart, $this->parseQueryPart($sQueryPart, $aServerParams), $sURL); |
|
| 303 | +} |
|
| 304 | 304 | } |
@@ -138,13 +138,13 @@ discard block |
||
| 138 | 138 | // Get the namespace name |
| 139 | 139 | $namespace = $this->__default_namespace__; |
| 140 | 140 | $separatorPosition = strrpos($template, '::'); |
| 141 | - if($separatorPosition !== false) |
|
| 141 | + if ($separatorPosition !== false) |
|
| 142 | 142 | { |
| 143 | 143 | $namespace = substr($template, 0, $separatorPosition); |
| 144 | 144 | $template = substr($template, $separatorPosition + 2); |
| 145 | 145 | } |
| 146 | 146 | // Check if the namespace is defined |
| 147 | - if(!isset($this->__namespaces__[$namespace])) |
|
| 147 | + if (!isset($this->__namespaces__[$namespace])) |
|
| 148 | 148 | { |
| 149 | 149 | return $template; |
| 150 | 150 | } |
@@ -165,13 +165,13 @@ discard block |
||
| 165 | 165 | { |
| 166 | 166 | // Get the template path |
| 167 | 167 | $templatePath = $this->__path(); |
| 168 | - if(!@is_readable($templatePath)) |
|
| 168 | + if (!@is_readable($templatePath)) |
|
| 169 | 169 | { |
| 170 | 170 | return ''; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | // Save the template properties. |
| 174 | - foreach($vars as $name => $value) |
|
| 174 | + foreach ($vars as $name => $value) |
|
| 175 | 175 | { |
| 176 | 176 | $this->__set((string)$name, $value); |
| 177 | 177 | } |
@@ -182,8 +182,7 @@ discard block |
||
| 182 | 182 | include $templatePath; |
| 183 | 183 | $content = ob_get_clean(); |
| 184 | 184 | |
| 185 | - return $this->__extends__ === null ? $content : |
|
| 186 | - // Render the extended template with the same properties. |
|
| 185 | + return $this->__extends__ === null ? $content : // Render the extended template with the same properties. |
|
| 187 | 186 | $this->__extends__->__render($this->__properties__); |
| 188 | 187 | }; |
| 189 | 188 | |
@@ -30,54 +30,54 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | class Context |
| 32 | 32 | { |
| 33 | - /** |
|
| 33 | +/** |
|
| 34 | 34 | * @var Context|null |
| 35 | 35 | */ |
| 36 | - private $__extends__ = null; |
|
| 36 | +private $__extends__ = null; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * @var string |
| 40 | 40 | */ |
| 41 | - private $__block_name__; |
|
| 41 | +private $__block_name__; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 43 | +/** |
|
| 44 | 44 | * @var array |
| 45 | 45 | */ |
| 46 | - private $__properties__ = []; |
|
| 46 | +private $__properties__ = []; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 48 | +/** |
|
| 49 | 49 | * The constructor |
| 50 | 50 | * |
| 51 | 51 | * @param array $__namespaces__ |
| 52 | 52 | * @param string $__default_namespace__ |
| 53 | 53 | * @param string $__template__ |
| 54 | 54 | */ |
| 55 | - public function __construct(protected array $__namespaces__, |
|
| 56 | - protected string $__default_namespace__, protected string $__template__) |
|
| 57 | - {} |
|
| 55 | +public function __construct(protected array $__namespaces__, |
|
| 56 | +protected string $__default_namespace__, protected string $__template__) |
|
| 57 | +{} |
|
| 58 | 58 | |
| 59 | - /** |
|
| 59 | +/** |
|
| 60 | 60 | * @param string $name |
| 61 | 61 | * |
| 62 | 62 | * @return mixed |
| 63 | 63 | */ |
| 64 | - public function __get(string $name): mixed |
|
| 65 | - { |
|
| 66 | - return $this->__properties__[$name] ?? ''; |
|
| 67 | - } |
|
| 64 | +public function __get(string $name): mixed |
|
| 65 | +{ |
|
| 66 | +return $this->__properties__[$name] ?? ''; |
|
| 67 | +} |
|
| 68 | 68 | |
| 69 | - /** |
|
| 69 | +/** |
|
| 70 | 70 | * @param string $name |
| 71 | 71 | * @param mixed $value |
| 72 | 72 | * |
| 73 | 73 | * @return void |
| 74 | 74 | */ |
| 75 | - public function __set(string $name, $value): void |
|
| 76 | - { |
|
| 77 | - $this->__properties__[$name] = $value; |
|
| 78 | - } |
|
| 75 | +public function __set(string $name, $value): void |
|
| 76 | +{ |
|
| 77 | +$this->__properties__[$name] = $value; |
|
| 78 | +} |
|
| 79 | 79 | |
| 80 | - /** |
|
| 80 | +/** |
|
| 81 | 81 | * Include a template |
| 82 | 82 | * |
| 83 | 83 | * @param string $template The name of template to be rendered |
@@ -85,110 +85,110 @@ discard block |
||
| 85 | 85 | * |
| 86 | 86 | * @return void |
| 87 | 87 | */ |
| 88 | - protected function include(string $template, array $vars = []): void |
|
| 89 | - { |
|
| 90 | - $context = new Context($this->__namespaces__, |
|
| 91 | - $this->__default_namespace__, $template); |
|
| 92 | - echo $context->__render($vars); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 88 | +protected function include(string $template, array $vars = []): void |
|
| 89 | +{ |
|
| 90 | +$context = new Context($this->__namespaces__, |
|
| 91 | +$this->__default_namespace__, $template); |
|
| 92 | +echo $context->__render($vars); |
|
| 93 | +} |
|
| 94 | + |
|
| 95 | +/** |
|
| 96 | 96 | * @param string $template The name of template to be rendered |
| 97 | 97 | * |
| 98 | 98 | * @return void |
| 99 | 99 | */ |
| 100 | - public function extends(string $template): void |
|
| 101 | - { |
|
| 102 | - $this->__extends__ = new Context($this->__namespaces__, |
|
| 103 | - $this->__default_namespace__, $template); |
|
| 104 | - } |
|
| 100 | +public function extends(string $template): void |
|
| 101 | +{ |
|
| 102 | +$this->__extends__ = new Context($this->__namespaces__, |
|
| 103 | +$this->__default_namespace__, $template); |
|
| 104 | +} |
|
| 105 | 105 | |
| 106 | - /** |
|
| 106 | +/** |
|
| 107 | 107 | * Start a new block |
| 108 | 108 | * |
| 109 | 109 | * @param string $name |
| 110 | 110 | * |
| 111 | 111 | * @return void |
| 112 | 112 | */ |
| 113 | - public function block(string $name): void |
|
| 114 | - { |
|
| 115 | - ob_start(); |
|
| 116 | - $this->__block_name__ = $name; |
|
| 117 | - } |
|
| 113 | +public function block(string $name): void |
|
| 114 | +{ |
|
| 115 | +ob_start(); |
|
| 116 | +$this->__block_name__ = $name; |
|
| 117 | +} |
|
| 118 | 118 | |
| 119 | - /** |
|
| 119 | +/** |
|
| 120 | 120 | * End the current block |
| 121 | 121 | * |
| 122 | 122 | * @param Closure|null $filter |
| 123 | 123 | * |
| 124 | 124 | * @return void |
| 125 | 125 | */ |
| 126 | - public function endblock(?Closure $filter = null): void |
|
| 127 | - { |
|
| 128 | - $content = ob_get_clean(); |
|
| 129 | - $this->__set($this->__block_name__, !$filter ? $content : $filter($content)); |
|
| 130 | - } |
|
| 126 | +public function endblock(?Closure $filter = null): void |
|
| 127 | +{ |
|
| 128 | +$content = ob_get_clean(); |
|
| 129 | +$this->__set($this->__block_name__, !$filter ? $content : $filter($content)); |
|
| 130 | +} |
|
| 131 | 131 | |
| 132 | - /** |
|
| 132 | +/** |
|
| 133 | 133 | * @return string |
| 134 | 134 | */ |
| 135 | - private function __path(): string |
|
| 136 | - { |
|
| 137 | - $template = trim($this->__template__); |
|
| 138 | - // Get the namespace name |
|
| 139 | - $namespace = $this->__default_namespace__; |
|
| 140 | - $separatorPosition = strrpos($template, '::'); |
|
| 141 | - if($separatorPosition !== false) |
|
| 142 | - { |
|
| 143 | - $namespace = substr($template, 0, $separatorPosition); |
|
| 144 | - $template = substr($template, $separatorPosition + 2); |
|
| 145 | - } |
|
| 146 | - // Check if the namespace is defined |
|
| 147 | - if(!isset($this->__namespaces__[$namespace])) |
|
| 148 | - { |
|
| 149 | - return $template; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - $namespace = $this->__namespaces__[$namespace]; |
|
| 153 | - // Get the template path |
|
| 154 | - return $namespace['directory'] . $template . $namespace['extension']; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 135 | +private function __path(): string |
|
| 136 | +{ |
|
| 137 | +$template = trim($this->__template__); |
|
| 138 | +// Get the namespace name |
|
| 139 | +$namespace = $this->__default_namespace__; |
|
| 140 | +$separatorPosition = strrpos($template, '::'); |
|
| 141 | +if($separatorPosition !== false) |
|
| 142 | +{ |
|
| 143 | +$namespace = substr($template, 0, $separatorPosition); |
|
| 144 | +$template = substr($template, $separatorPosition + 2); |
|
| 145 | +} |
|
| 146 | +// Check if the namespace is defined |
|
| 147 | +if(!isset($this->__namespaces__[$namespace])) |
|
| 148 | +{ |
|
| 149 | +return $template; |
|
| 150 | +} |
|
| 151 | + |
|
| 152 | +$namespace = $this->__namespaces__[$namespace]; |
|
| 153 | +// Get the template path |
|
| 154 | +return $namespace['directory'] . $template . $namespace['extension']; |
|
| 155 | +} |
|
| 156 | + |
|
| 157 | +/** |
|
| 158 | 158 | * Render a template |
| 159 | 159 | * |
| 160 | 160 | * @param array $vars The template vars |
| 161 | 161 | * |
| 162 | 162 | * @return string |
| 163 | 163 | */ |
| 164 | - public function __render(array $vars): string |
|
| 165 | - { |
|
| 166 | - // Get the template path |
|
| 167 | - $templatePath = $this->__path(); |
|
| 168 | - if(!@is_readable($templatePath)) |
|
| 169 | - { |
|
| 170 | - return ''; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // Save the template properties. |
|
| 174 | - foreach($vars as $name => $value) |
|
| 175 | - { |
|
| 176 | - $this->__set((string)$name, $value); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Render the template |
|
| 180 | - $renderer = function() use($templatePath) { |
|
| 181 | - ob_start(); |
|
| 182 | - include $templatePath; |
|
| 183 | - $content = ob_get_clean(); |
|
| 184 | - |
|
| 185 | - return $this->__extends__ === null ? $content : |
|
| 186 | - // Render the extended template with the same properties. |
|
| 187 | - $this->__extends__->__render($this->__properties__); |
|
| 188 | - }; |
|
| 189 | - |
|
| 190 | - // Call the closure in the context of this object. |
|
| 191 | - // So the keyword '$this' in the template will refer to this object. |
|
| 192 | - return call_user_func($renderer->bindTo($this)); |
|
| 193 | - } |
|
| 164 | +public function __render(array $vars): string |
|
| 165 | +{ |
|
| 166 | +// Get the template path |
|
| 167 | +$templatePath = $this->__path(); |
|
| 168 | +if(!@is_readable($templatePath)) |
|
| 169 | +{ |
|
| 170 | +return ''; |
|
| 171 | +} |
|
| 172 | + |
|
| 173 | +// Save the template properties. |
|
| 174 | +foreach($vars as $name => $value) |
|
| 175 | +{ |
|
| 176 | +$this->__set((string)$name, $value); |
|
| 177 | +} |
|
| 178 | + |
|
| 179 | +// Render the template |
|
| 180 | +$renderer = function() use($templatePath) { |
|
| 181 | +ob_start(); |
|
| 182 | +include $templatePath; |
|
| 183 | +$content = ob_get_clean(); |
|
| 184 | + |
|
| 185 | +return $this->__extends__ === null ? $content : |
|
| 186 | + // Render the extended template with the same properties. |
|
| 187 | + $this->__extends__->__render($this->__properties__); |
|
| 188 | +}; |
|
| 189 | + |
|
| 190 | +// Call the closure in the context of this object. |
|
| 191 | +// So the keyword '$this' in the template will refer to this object. |
|
| 192 | +return call_user_func($renderer->bindTo($this)); |
|
| 193 | +} |
|
| 194 | 194 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | public function setLocale(string $sLocale): void |
| 58 | 58 | { |
| 59 | - if(($sLocale = trim($sLocale))) |
|
| 59 | + if (($sLocale = trim($sLocale))) |
|
| 60 | 60 | { |
| 61 | 61 | $this->sDefaultLocale = $sLocale; |
| 62 | 62 | } |
@@ -73,11 +73,11 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | private function _loadTranslations(string $sLanguage, string $sPrefix, array $aTranslations): void |
| 75 | 75 | { |
| 76 | - foreach($aTranslations as $sKey => $xTranslation) |
|
| 76 | + foreach ($aTranslations as $sKey => $xTranslation) |
|
| 77 | 77 | { |
| 78 | 78 | $sKey = trim($sKey); |
| 79 | 79 | $sKey = ($sPrefix) ? $sPrefix . '.' . $sKey : $sKey; |
| 80 | - if(is_array($xTranslation)) |
|
| 80 | + if (is_array($xTranslation)) |
|
| 81 | 81 | { |
| 82 | 82 | // Recursively read the translations in the array |
| 83 | 83 | $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
@@ -100,18 +100,18 @@ discard block |
||
| 100 | 100 | */ |
| 101 | 101 | public function loadTranslations(string $sFilePath, string $sLanguage): bool |
| 102 | 102 | { |
| 103 | - if(!file_exists($sFilePath)) |
|
| 103 | + if (!file_exists($sFilePath)) |
|
| 104 | 104 | { |
| 105 | 105 | return false; |
| 106 | 106 | } |
| 107 | 107 | $aTranslations = require($sFilePath); |
| 108 | - if(!is_array($aTranslations)) |
|
| 108 | + if (!is_array($aTranslations)) |
|
| 109 | 109 | { |
| 110 | 110 | return false; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | // Load the translations |
| 114 | - if(!isset($this->aTranslations[$sLanguage])) |
|
| 114 | + if (!isset($this->aTranslations[$sLanguage])) |
|
| 115 | 115 | { |
| 116 | 116 | $this->aTranslations[$sLanguage] = []; |
| 117 | 117 | } |
@@ -133,16 +133,16 @@ discard block |
||
| 133 | 133 | public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
| 134 | 134 | { |
| 135 | 135 | $sText = trim($sText); |
| 136 | - if(empty($sLanguage)) |
|
| 136 | + if (empty($sLanguage)) |
|
| 137 | 137 | { |
| 138 | 138 | $sLanguage = $this->sDefaultLocale; |
| 139 | 139 | } |
| 140 | - if(!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 140 | + if (!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 141 | 141 | { |
| 142 | 142 | return $sText; |
| 143 | 143 | } |
| 144 | 144 | $sMessage = $this->aTranslations[$sLanguage][$sText]; |
| 145 | - if(!empty($aPlaceHolders)) |
|
| 145 | + if (!empty($aPlaceHolders)) |
|
| 146 | 146 | { |
| 147 | 147 | $aVars = array_map(function($sVar) { |
| 148 | 148 | return ':' . $sVar; |
@@ -162,16 +162,16 @@ discard block |
||
| 162 | 162 | */ |
| 163 | 163 | public function translations(string $sKey, string $sLanguage = ''): array |
| 164 | 164 | { |
| 165 | - if(empty($sLanguage)) |
|
| 165 | + if (empty($sLanguage)) |
|
| 166 | 166 | { |
| 167 | 167 | $sLanguage = $this->sDefaultLocale; |
| 168 | 168 | } |
| 169 | 169 | $aKeys = explode('.', $sKey); |
| 170 | 170 | |
| 171 | 171 | $aTranslations = $this->aRawTranslations[$sLanguage]; |
| 172 | - foreach($aKeys as $sKey) |
|
| 172 | + foreach ($aKeys as $sKey) |
|
| 173 | 173 | { |
| 174 | - if($sKey !== '') |
|
| 174 | + if ($sKey !== '') |
|
| 175 | 175 | { |
| 176 | 176 | $aTranslations = $aTranslations[$sKey] ?? []; |
| 177 | 177 | } |
@@ -81,8 +81,7 @@ |
||
| 81 | 81 | { |
| 82 | 82 | // Recursively read the translations in the array |
| 83 | 83 | $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
| 84 | - } |
|
| 85 | - else |
|
| 84 | + } else |
|
| 86 | 85 | { |
| 87 | 86 | // Save this translation |
| 88 | 87 | $this->aTranslations[$sLanguage][$sKey] = $xTranslation; |
@@ -26,43 +26,43 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | class Translator |
| 28 | 28 | { |
| 29 | - /** |
|
| 29 | +/** |
|
| 30 | 30 | * The default locale |
| 31 | 31 | * |
| 32 | 32 | * @var string |
| 33 | 33 | */ |
| 34 | - protected $sDefaultLocale = 'en'; |
|
| 34 | +protected $sDefaultLocale = 'en'; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 36 | +/** |
|
| 37 | 37 | * The translations in a flattened array |
| 38 | 38 | * |
| 39 | 39 | * @var array |
| 40 | 40 | */ |
| 41 | - protected $aTranslations = []; |
|
| 41 | +protected $aTranslations = []; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 43 | +/** |
|
| 44 | 44 | * The translations as received as input |
| 45 | 45 | * |
| 46 | 46 | * @var array |
| 47 | 47 | */ |
| 48 | - protected $aRawTranslations = []; |
|
| 48 | +protected $aRawTranslations = []; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * Set the default locale |
| 52 | 52 | * |
| 53 | 53 | * @param string $sLocale |
| 54 | 54 | * |
| 55 | 55 | * @return void |
| 56 | 56 | */ |
| 57 | - public function setLocale(string $sLocale): void |
|
| 58 | - { |
|
| 59 | - if(($sLocale = trim($sLocale))) |
|
| 60 | - { |
|
| 61 | - $this->sDefaultLocale = $sLocale; |
|
| 62 | - } |
|
| 63 | - } |
|
| 57 | +public function setLocale(string $sLocale): void |
|
| 58 | +{ |
|
| 59 | +if(($sLocale = trim($sLocale))) |
|
| 60 | +{ |
|
| 61 | +$this->sDefaultLocale = $sLocale; |
|
| 62 | +} |
|
| 63 | +} |
|
| 64 | 64 | |
| 65 | - /** |
|
| 65 | +/** |
|
| 66 | 66 | * Recursively load translated strings from an array |
| 67 | 67 | * |
| 68 | 68 | * @param string $sLanguage The language of the translations |
@@ -71,26 +71,26 @@ discard block |
||
| 71 | 71 | * |
| 72 | 72 | * @return void |
| 73 | 73 | */ |
| 74 | - private function _loadTranslations(string $sLanguage, string $sPrefix, array $aTranslations): void |
|
| 75 | - { |
|
| 76 | - foreach($aTranslations as $sKey => $xTranslation) |
|
| 77 | - { |
|
| 78 | - $sKey = trim($sKey); |
|
| 79 | - $sKey = ($sPrefix) ? $sPrefix . '.' . $sKey : $sKey; |
|
| 80 | - if(is_array($xTranslation)) |
|
| 81 | - { |
|
| 82 | - // Recursively read the translations in the array |
|
| 83 | - $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
|
| 84 | - } |
|
| 85 | - else |
|
| 86 | - { |
|
| 87 | - // Save this translation |
|
| 88 | - $this->aTranslations[$sLanguage][$sKey] = $xTranslation; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 74 | +private function _loadTranslations(string $sLanguage, string $sPrefix, array $aTranslations): void |
|
| 75 | +{ |
|
| 76 | +foreach($aTranslations as $sKey => $xTranslation) |
|
| 77 | +{ |
|
| 78 | +$sKey = trim($sKey); |
|
| 79 | +$sKey = ($sPrefix) ? $sPrefix . '.' . $sKey : $sKey; |
|
| 80 | +if(is_array($xTranslation)) |
|
| 81 | +{ |
|
| 82 | + // Recursively read the translations in the array |
|
| 83 | + $this->_loadTranslations($sLanguage, $sKey, $xTranslation); |
|
| 84 | +} |
|
| 85 | +else |
|
| 86 | +{ |
|
| 87 | + // Save this translation |
|
| 88 | + $this->aTranslations[$sLanguage][$sKey] = $xTranslation; |
|
| 89 | +} |
|
| 90 | +} |
|
| 91 | +} |
|
| 92 | 92 | |
| 93 | - /** |
|
| 93 | +/** |
|
| 94 | 94 | * Load translated strings from a file |
| 95 | 95 | * |
| 96 | 96 | * @param string $sFilePath The file full path |
@@ -98,30 +98,30 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @return bool |
| 100 | 100 | */ |
| 101 | - public function loadTranslations(string $sFilePath, string $sLanguage): bool |
|
| 102 | - { |
|
| 103 | - if(!file_exists($sFilePath)) |
|
| 104 | - { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - $aTranslations = require($sFilePath); |
|
| 108 | - if(!is_array($aTranslations)) |
|
| 109 | - { |
|
| 110 | - return false; |
|
| 111 | - } |
|
| 101 | +public function loadTranslations(string $sFilePath, string $sLanguage): bool |
|
| 102 | +{ |
|
| 103 | +if(!file_exists($sFilePath)) |
|
| 104 | +{ |
|
| 105 | +return false; |
|
| 106 | +} |
|
| 107 | +$aTranslations = require($sFilePath); |
|
| 108 | +if(!is_array($aTranslations)) |
|
| 109 | +{ |
|
| 110 | +return false; |
|
| 111 | +} |
|
| 112 | 112 | |
| 113 | - // Load the translations |
|
| 114 | - if(!isset($this->aTranslations[$sLanguage])) |
|
| 115 | - { |
|
| 116 | - $this->aTranslations[$sLanguage] = []; |
|
| 117 | - } |
|
| 118 | - $this->aRawTranslations[$sLanguage] = |
|
| 119 | - array_merge($this->aRawTranslations[$sLanguage] ?? [], $aTranslations); |
|
| 120 | - $this->_loadTranslations($sLanguage, '', $aTranslations); |
|
| 121 | - return true; |
|
| 122 | - } |
|
| 113 | +// Load the translations |
|
| 114 | +if(!isset($this->aTranslations[$sLanguage])) |
|
| 115 | +{ |
|
| 116 | +$this->aTranslations[$sLanguage] = []; |
|
| 117 | +} |
|
| 118 | +$this->aRawTranslations[$sLanguage] = |
|
| 119 | +array_merge($this->aRawTranslations[$sLanguage] ?? [], $aTranslations); |
|
| 120 | +$this->_loadTranslations($sLanguage, '', $aTranslations); |
|
| 121 | +return true; |
|
| 122 | +} |
|
| 123 | 123 | |
| 124 | - /** |
|
| 124 | +/** |
|
| 125 | 125 | * Get a translated string |
| 126 | 126 | * |
| 127 | 127 | * @param string $sText The key of the translated string |
@@ -130,29 +130,29 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | * @return string |
| 132 | 132 | */ |
| 133 | - public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
|
| 134 | - { |
|
| 135 | - $sText = trim($sText); |
|
| 136 | - if(empty($sLanguage)) |
|
| 137 | - { |
|
| 138 | - $sLanguage = $this->sDefaultLocale; |
|
| 139 | - } |
|
| 140 | - if(!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 141 | - { |
|
| 142 | - return $sText; |
|
| 143 | - } |
|
| 144 | - $sMessage = $this->aTranslations[$sLanguage][$sText]; |
|
| 145 | - if(!empty($aPlaceHolders)) |
|
| 146 | - { |
|
| 147 | - $aVars = array_map(function($sVar) { |
|
| 148 | - return ':' . $sVar; |
|
| 149 | - }, array_keys($aPlaceHolders)); |
|
| 150 | - $sMessage = str_replace($aVars, array_values($aPlaceHolders), $sMessage); |
|
| 151 | - } |
|
| 152 | - return $sMessage; |
|
| 153 | - } |
|
| 133 | +public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string |
|
| 134 | +{ |
|
| 135 | +$sText = trim($sText); |
|
| 136 | +if(empty($sLanguage)) |
|
| 137 | +{ |
|
| 138 | +$sLanguage = $this->sDefaultLocale; |
|
| 139 | +} |
|
| 140 | +if(!isset($this->aTranslations[$sLanguage][$sText])) |
|
| 141 | +{ |
|
| 142 | +return $sText; |
|
| 143 | +} |
|
| 144 | +$sMessage = $this->aTranslations[$sLanguage][$sText]; |
|
| 145 | +if(!empty($aPlaceHolders)) |
|
| 146 | +{ |
|
| 147 | +$aVars = array_map(function($sVar) { |
|
| 148 | + return ':' . $sVar; |
|
| 149 | +}, array_keys($aPlaceHolders)); |
|
| 150 | +$sMessage = str_replace($aVars, array_values($aPlaceHolders), $sMessage); |
|
| 151 | +} |
|
| 152 | +return $sMessage; |
|
| 153 | +} |
|
| 154 | 154 | |
| 155 | - /** |
|
| 155 | +/** |
|
| 156 | 156 | * Get all the translations under a given key |
| 157 | 157 | * |
| 158 | 158 | * @param string $sKey |
@@ -160,22 +160,22 @@ discard block |
||
| 160 | 160 | * |
| 161 | 161 | * @return array |
| 162 | 162 | */ |
| 163 | - public function translations(string $sKey, string $sLanguage = ''): array |
|
| 164 | - { |
|
| 165 | - if(empty($sLanguage)) |
|
| 166 | - { |
|
| 167 | - $sLanguage = $this->sDefaultLocale; |
|
| 168 | - } |
|
| 169 | - $aKeys = explode('.', $sKey); |
|
| 163 | +public function translations(string $sKey, string $sLanguage = ''): array |
|
| 164 | +{ |
|
| 165 | +if(empty($sLanguage)) |
|
| 166 | +{ |
|
| 167 | +$sLanguage = $this->sDefaultLocale; |
|
| 168 | +} |
|
| 169 | +$aKeys = explode('.', $sKey); |
|
| 170 | 170 | |
| 171 | - $aTranslations = $this->aRawTranslations[$sLanguage]; |
|
| 172 | - foreach($aKeys as $sKey) |
|
| 173 | - { |
|
| 174 | - if($sKey !== '') |
|
| 175 | - { |
|
| 176 | - $aTranslations = $aTranslations[$sKey] ?? []; |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - return $aTranslations; |
|
| 180 | - } |
|
| 171 | +$aTranslations = $this->aRawTranslations[$sLanguage]; |
|
| 172 | +foreach($aKeys as $sKey) |
|
| 173 | +{ |
|
| 174 | +if($sKey !== '') |
|
| 175 | +{ |
|
| 176 | + $aTranslations = $aTranslations[$sKey] ?? []; |
|
| 177 | +} |
|
| 178 | +} |
|
| 179 | +return $aTranslations; |
|
| 180 | +} |
|
| 181 | 181 | } |