@@ -25,15 +25,15 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | public function __construct( |
| 27 | 27 | private readonly CsrfConfig $config |
| 28 | - ) { |
|
| 28 | + ){ |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | public function process(Request $request, RequestHandlerInterface $handler): Response |
| 32 | 32 | { |
| 33 | 33 | $cookie = null; |
| 34 | - if (isset($request->getCookieParams()[$this->config->getCookie()])) { |
|
| 34 | + if (isset($request->getCookieParams()[$this->config->getCookie()])){ |
|
| 35 | 35 | $token = $request->getCookieParams()[$this->config->getCookie()]; |
| 36 | - } else { |
|
| 36 | + }else{ |
|
| 37 | 37 | //Making new token |
| 38 | 38 | $token = $this->random($this->config->getTokenLength()); |
| 39 | 39 | |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | //CSRF issues must be handled by Firewall middleware |
| 45 | 45 | $response = $handler->handle($request->withAttribute(static::ATTRIBUTE, $token)); |
| 46 | 46 | |
| 47 | - if (!empty($cookie)) { |
|
| 47 | + if (!empty($cookie)){ |
|
| 48 | 48 | return $response->withAddedHeader('Set-Cookie', $cookie); |
| 49 | 49 | } |
| 50 | 50 | |
@@ -75,12 +75,12 @@ discard block |
||
| 75 | 75 | */ |
| 76 | 76 | private function random(int $length = 32): string |
| 77 | 77 | { |
| 78 | - try { |
|
| 79 | - if (empty($string = random_bytes($length))) { |
|
| 78 | + try{ |
|
| 79 | + if (empty($string = random_bytes($length))){ |
|
| 80 | 80 | throw new \RuntimeException('Unable to generate random string'); |
| 81 | 81 | } |
| 82 | - } catch (\Throwable $e) { |
|
| 83 | - throw new \RuntimeException('Unable to generate random string', (int) $e->getCode(), $e); |
|
| 82 | + }catch (\Throwable $e){ |
|
| 83 | + throw new \RuntimeException('Unable to generate random string', (int)$e->getCode(), $e); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | return \substr(\base64_encode($string), 0, $length); |
@@ -31,9 +31,12 @@ discard block |
||
| 31 | 31 | public function process(Request $request, RequestHandlerInterface $handler): Response |
| 32 | 32 | { |
| 33 | 33 | $cookie = null; |
| 34 | - if (isset($request->getCookieParams()[$this->config->getCookie()])) { |
|
| 34 | + if (isset($request->getCookieParams()[$this->config->getCookie()])) |
|
| 35 | + { |
|
| 35 | 36 | $token = $request->getCookieParams()[$this->config->getCookie()]; |
| 36 | - } else { |
|
| 37 | + } |
|
| 38 | + else |
|
| 39 | + { |
|
| 37 | 40 | //Making new token |
| 38 | 41 | $token = $this->random($this->config->getTokenLength()); |
| 39 | 42 | |
@@ -44,7 +47,8 @@ discard block |
||
| 44 | 47 | //CSRF issues must be handled by Firewall middleware |
| 45 | 48 | $response = $handler->handle($request->withAttribute(static::ATTRIBUTE, $token)); |
| 46 | 49 | |
| 47 | - if (!empty($cookie)) { |
|
| 50 | + if (!empty($cookie)) |
|
| 51 | + { |
|
| 48 | 52 | return $response->withAddedHeader('Set-Cookie', $cookie); |
| 49 | 53 | } |
| 50 | 54 | |
@@ -75,11 +79,15 @@ discard block |
||
| 75 | 79 | */ |
| 76 | 80 | private function random(int $length = 32): string |
| 77 | 81 | { |
| 78 | - try { |
|
| 79 | - if (empty($string = random_bytes($length))) { |
|
| 82 | + try |
|
| 83 | + { |
|
| 84 | + if (empty($string = random_bytes($length))) |
|
| 85 | + { |
|
| 80 | 86 | throw new \RuntimeException('Unable to generate random string'); |
| 81 | 87 | } |
| 82 | - } catch (\Throwable $e) { |
|
| 88 | + } |
|
| 89 | + catch (\Throwable $e) |
|
| 90 | + { |
|
| 83 | 91 | throw new \RuntimeException('Unable to generate random string', (int) $e->getCode(), $e); |
| 84 | 92 | } |
| 85 | 93 | |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function stream_eof(): bool |
| 45 | 45 | { |
| 46 | - if ($this->stream === null) { |
|
| 46 | + if ($this->stream === null){ |
|
| 47 | 47 | throw new WrapperException('Stream is not opened.'); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | */ |
| 56 | 56 | public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool |
| 57 | 57 | { |
| 58 | - if (!isset(self::$uris[$path])) { |
|
| 58 | + if (!isset(self::$uris[$path])){ |
|
| 59 | 59 | return false; |
| 60 | 60 | } |
| 61 | 61 | |
@@ -70,9 +70,9 @@ discard block |
||
| 70 | 70 | /** |
| 71 | 71 | * Read data from StreamInterface. |
| 72 | 72 | */ |
| 73 | - public function stream_read(int $length): string|false |
|
| 73 | + public function stream_read(int $length): string | false |
|
| 74 | 74 | { |
| 75 | - if ($this->stream === null) { |
|
| 75 | + if ($this->stream === null){ |
|
| 76 | 76 | throw new WrapperException('Stream is not opened.'); |
| 77 | 77 | } |
| 78 | 78 | |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | */ |
| 85 | 85 | public function stream_seek(int $offset, int $whence = SEEK_SET): bool |
| 86 | 86 | { |
| 87 | - if ($this->stream === null) { |
|
| 87 | + if ($this->stream === null){ |
|
| 88 | 88 | throw new WrapperException('Stream is not opened.'); |
| 89 | 89 | } |
| 90 | 90 | |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | */ |
| 103 | 103 | public function stream_stat(): array |
| 104 | 104 | { |
| 105 | - if ($this->stream === null) { |
|
| 105 | + if ($this->stream === null){ |
|
| 106 | 106 | throw new WrapperException('Stream is not opened.'); |
| 107 | 107 | } |
| 108 | 108 | |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | */ |
| 115 | 115 | public function stream_tell(): int |
| 116 | 116 | { |
| 117 | - if ($this->stream === null) { |
|
| 117 | + if ($this->stream === null){ |
|
| 118 | 118 | throw new WrapperException('Stream is not opened.'); |
| 119 | 119 | } |
| 120 | 120 | |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | */ |
| 129 | 129 | public function stream_write(string $data): int |
| 130 | 130 | { |
| 131 | - if ($this->stream === null) { |
|
| 131 | + if ($this->stream === null){ |
|
| 132 | 132 | throw new WrapperException('Stream is not opened.'); |
| 133 | 133 | } |
| 134 | 134 | |
@@ -140,14 +140,14 @@ discard block |
||
| 140 | 140 | * |
| 141 | 141 | * @see stat() |
| 142 | 142 | */ |
| 143 | - public function url_stat(string $path, int $flag): array|false |
|
| 143 | + public function url_stat(string $path, int $flag): array | false |
|
| 144 | 144 | { |
| 145 | - try { |
|
| 146 | - if (isset(self::$uris[$path])) { |
|
| 145 | + try{ |
|
| 146 | + if (isset(self::$uris[$path])){ |
|
| 147 | 147 | return $this->getStreamStats(self::$uris[$path]); |
| 148 | 148 | } |
| 149 | - } catch (\Throwable $e) { |
|
| 150 | - if (($flag & \STREAM_URL_STAT_QUIET) === \STREAM_URL_STAT_QUIET) { |
|
| 149 | + }catch (\Throwable $e){ |
|
| 150 | + if (($flag & \STREAM_URL_STAT_QUIET) === \STREAM_URL_STAT_QUIET){ |
|
| 151 | 151 | return false; |
| 152 | 152 | } |
| 153 | 153 | \trigger_error($e->getMessage(), \E_USER_ERROR); |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | */ |
| 162 | 162 | public static function register(): void |
| 163 | 163 | { |
| 164 | - if (self::$registered) { |
|
| 164 | + if (self::$registered){ |
|
| 165 | 165 | return; |
| 166 | 166 | } |
| 167 | 167 | |
@@ -173,10 +173,10 @@ discard block |
||
| 173 | 173 | /** |
| 174 | 174 | * Check if given uri or stream has been allocated. |
| 175 | 175 | */ |
| 176 | - public static function has(StreamInterface|string $file): bool |
|
| 176 | + public static function has(StreamInterface | string $file): bool |
|
| 177 | 177 | { |
| 178 | - if ($file instanceof StreamInterface) { |
|
| 179 | - $file = 'spiral://' . \spl_object_hash($file); |
|
| 178 | + if ($file instanceof StreamInterface){ |
|
| 179 | + $file = 'spiral://'.\spl_object_hash($file); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | return isset(self::$uris[$file]); |
@@ -191,15 +191,15 @@ discard block |
||
| 191 | 191 | public static function getResource(StreamInterface $stream) |
| 192 | 192 | { |
| 193 | 193 | $mode = null; |
| 194 | - if ($stream->isReadable()) { |
|
| 194 | + if ($stream->isReadable()){ |
|
| 195 | 195 | $mode = 'r'; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - if ($stream->isWritable()) { |
|
| 198 | + if ($stream->isWritable()){ |
|
| 199 | 199 | $mode = !empty($mode) ? 'r+' : 'w'; |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | - if (empty($mode)) { |
|
| 202 | + if (empty($mode)){ |
|
| 203 | 203 | throw new WrapperException('Stream is not available in read or write modes'); |
| 204 | 204 | } |
| 205 | 205 | |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | { |
| 214 | 214 | self::register(); |
| 215 | 215 | |
| 216 | - $uri = 'spiral://' . \spl_object_hash($stream); |
|
| 216 | + $uri = 'spiral://'.\spl_object_hash($stream); |
|
| 217 | 217 | self::$uris[$uri] = $stream; |
| 218 | 218 | |
| 219 | 219 | return $uri; |
@@ -225,10 +225,10 @@ discard block |
||
| 225 | 225 | * |
| 226 | 226 | * @param string|StreamInterface $file String uri or StreamInterface. |
| 227 | 227 | */ |
| 228 | - public static function release(StreamInterface|string $file): void |
|
| 228 | + public static function release(StreamInterface | string $file): void |
|
| 229 | 229 | { |
| 230 | - if ($file instanceof StreamInterface) { |
|
| 231 | - $file = 'spiral://' . \spl_object_hash($file); |
|
| 230 | + if ($file instanceof StreamInterface){ |
|
| 231 | + $file = 'spiral://'.\spl_object_hash($file); |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | unset(self::$uris[$file]); |
@@ -240,12 +240,12 @@ discard block |
||
| 240 | 240 | private function getStreamStats(StreamInterface $stream): array |
| 241 | 241 | { |
| 242 | 242 | $mode = $this->mode; |
| 243 | - if (empty($mode)) { |
|
| 244 | - if ($stream->isReadable()) { |
|
| 243 | + if (empty($mode)){ |
|
| 244 | + if ($stream->isReadable()){ |
|
| 245 | 245 | $mode = 'r'; |
| 246 | 246 | } |
| 247 | 247 | |
| 248 | - if ($stream->isWritable()) { |
|
| 248 | + if ($stream->isWritable()){ |
|
| 249 | 249 | $mode = !empty($mode) ? 'r+' : 'w'; |
| 250 | 250 | } |
| 251 | 251 | } |
@@ -43,7 +43,8 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function stream_eof(): bool |
| 45 | 45 | { |
| 46 | - if ($this->stream === null) { |
|
| 46 | + if ($this->stream === null) |
|
| 47 | + { |
|
| 47 | 48 | throw new WrapperException('Stream is not opened.'); |
| 48 | 49 | } |
| 49 | 50 | |
@@ -55,7 +56,8 @@ discard block |
||
| 55 | 56 | */ |
| 56 | 57 | public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool |
| 57 | 58 | { |
| 58 | - if (!isset(self::$uris[$path])) { |
|
| 59 | + if (!isset(self::$uris[$path])) |
|
| 60 | + { |
|
| 59 | 61 | return false; |
| 60 | 62 | } |
| 61 | 63 | |
@@ -72,7 +74,8 @@ discard block |
||
| 72 | 74 | */ |
| 73 | 75 | public function stream_read(int $length): string|false |
| 74 | 76 | { |
| 75 | - if ($this->stream === null) { |
|
| 77 | + if ($this->stream === null) |
|
| 78 | + { |
|
| 76 | 79 | throw new WrapperException('Stream is not opened.'); |
| 77 | 80 | } |
| 78 | 81 | |
@@ -84,7 +87,8 @@ discard block |
||
| 84 | 87 | */ |
| 85 | 88 | public function stream_seek(int $offset, int $whence = SEEK_SET): bool |
| 86 | 89 | { |
| 87 | - if ($this->stream === null) { |
|
| 90 | + if ($this->stream === null) |
|
| 91 | + { |
|
| 88 | 92 | throw new WrapperException('Stream is not opened.'); |
| 89 | 93 | } |
| 90 | 94 | |
@@ -102,7 +106,8 @@ discard block |
||
| 102 | 106 | */ |
| 103 | 107 | public function stream_stat(): array |
| 104 | 108 | { |
| 105 | - if ($this->stream === null) { |
|
| 109 | + if ($this->stream === null) |
|
| 110 | + { |
|
| 106 | 111 | throw new WrapperException('Stream is not opened.'); |
| 107 | 112 | } |
| 108 | 113 | |
@@ -114,7 +119,8 @@ discard block |
||
| 114 | 119 | */ |
| 115 | 120 | public function stream_tell(): int |
| 116 | 121 | { |
| 117 | - if ($this->stream === null) { |
|
| 122 | + if ($this->stream === null) |
|
| 123 | + { |
|
| 118 | 124 | throw new WrapperException('Stream is not opened.'); |
| 119 | 125 | } |
| 120 | 126 | |
@@ -128,7 +134,8 @@ discard block |
||
| 128 | 134 | */ |
| 129 | 135 | public function stream_write(string $data): int |
| 130 | 136 | { |
| 131 | - if ($this->stream === null) { |
|
| 137 | + if ($this->stream === null) |
|
| 138 | + { |
|
| 132 | 139 | throw new WrapperException('Stream is not opened.'); |
| 133 | 140 | } |
| 134 | 141 | |
@@ -142,12 +149,17 @@ discard block |
||
| 142 | 149 | */ |
| 143 | 150 | public function url_stat(string $path, int $flag): array|false |
| 144 | 151 | { |
| 145 | - try { |
|
| 146 | - if (isset(self::$uris[$path])) { |
|
| 152 | + try |
|
| 153 | + { |
|
| 154 | + if (isset(self::$uris[$path])) |
|
| 155 | + { |
|
| 147 | 156 | return $this->getStreamStats(self::$uris[$path]); |
| 148 | 157 | } |
| 149 | - } catch (\Throwable $e) { |
|
| 150 | - if (($flag & \STREAM_URL_STAT_QUIET) === \STREAM_URL_STAT_QUIET) { |
|
| 158 | + } |
|
| 159 | + catch (\Throwable $e) |
|
| 160 | + { |
|
| 161 | + if (($flag & \STREAM_URL_STAT_QUIET) === \STREAM_URL_STAT_QUIET) |
|
| 162 | + { |
|
| 151 | 163 | return false; |
| 152 | 164 | } |
| 153 | 165 | \trigger_error($e->getMessage(), \E_USER_ERROR); |
@@ -161,7 +173,8 @@ discard block |
||
| 161 | 173 | */ |
| 162 | 174 | public static function register(): void |
| 163 | 175 | { |
| 164 | - if (self::$registered) { |
|
| 176 | + if (self::$registered) |
|
| 177 | + { |
|
| 165 | 178 | return; |
| 166 | 179 | } |
| 167 | 180 | |
@@ -175,7 +188,8 @@ discard block |
||
| 175 | 188 | */ |
| 176 | 189 | public static function has(StreamInterface|string $file): bool |
| 177 | 190 | { |
| 178 | - if ($file instanceof StreamInterface) { |
|
| 191 | + if ($file instanceof StreamInterface) |
|
| 192 | + { |
|
| 179 | 193 | $file = 'spiral://' . \spl_object_hash($file); |
| 180 | 194 | } |
| 181 | 195 | |
@@ -191,15 +205,18 @@ discard block |
||
| 191 | 205 | public static function getResource(StreamInterface $stream) |
| 192 | 206 | { |
| 193 | 207 | $mode = null; |
| 194 | - if ($stream->isReadable()) { |
|
| 208 | + if ($stream->isReadable()) |
|
| 209 | + { |
|
| 195 | 210 | $mode = 'r'; |
| 196 | 211 | } |
| 197 | 212 | |
| 198 | - if ($stream->isWritable()) { |
|
| 213 | + if ($stream->isWritable()) |
|
| 214 | + { |
|
| 199 | 215 | $mode = !empty($mode) ? 'r+' : 'w'; |
| 200 | 216 | } |
| 201 | 217 | |
| 202 | - if (empty($mode)) { |
|
| 218 | + if (empty($mode)) |
|
| 219 | + { |
|
| 203 | 220 | throw new WrapperException('Stream is not available in read or write modes'); |
| 204 | 221 | } |
| 205 | 222 | |
@@ -227,7 +244,8 @@ discard block |
||
| 227 | 244 | */ |
| 228 | 245 | public static function release(StreamInterface|string $file): void |
| 229 | 246 | { |
| 230 | - if ($file instanceof StreamInterface) { |
|
| 247 | + if ($file instanceof StreamInterface) |
|
| 248 | + { |
|
| 231 | 249 | $file = 'spiral://' . \spl_object_hash($file); |
| 232 | 250 | } |
| 233 | 251 | |
@@ -240,12 +258,15 @@ discard block |
||
| 240 | 258 | private function getStreamStats(StreamInterface $stream): array |
| 241 | 259 | { |
| 242 | 260 | $mode = $this->mode; |
| 243 | - if (empty($mode)) { |
|
| 244 | - if ($stream->isReadable()) { |
|
| 261 | + if (empty($mode)) |
|
| 262 | + { |
|
| 263 | + if ($stream->isReadable()) |
|
| 264 | + { |
|
| 245 | 265 | $mode = 'r'; |
| 246 | 266 | } |
| 247 | 267 | |
| 248 | - if ($stream->isWritable()) { |
|
| 268 | + if ($stream->isWritable()) |
|
| 269 | + { |
|
| 249 | 270 | $mode = !empty($mode) ? 'r+' : 'w'; |
| 250 | 271 | } |
| 251 | 272 | } |
@@ -21,8 +21,8 @@ |
||
| 21 | 21 | protected function makeApp(array $env): App |
| 22 | 22 | { |
| 23 | 23 | $config = [ |
| 24 | - 'root' => __DIR__ . '/App', |
|
| 25 | - 'app' => __DIR__ . '/App', |
|
| 24 | + 'root' => __DIR__.'/App', |
|
| 25 | + 'app' => __DIR__.'/App', |
|
| 26 | 26 | ]; |
| 27 | 27 | |
| 28 | 28 | return (App::create($config, false))->run(new Environment($env)); |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | array $headers = [] |
| 78 | 78 | ): ResponseInterface { |
| 79 | 79 | $r = $this->request($uri, 'GET', [], $headers, []); |
| 80 | - foreach ($attributes as $k => $v) { |
|
| 80 | + foreach ($attributes as $k => $v){ |
|
| 81 | 81 | $r = $r->withAttribute($k, $v); |
| 82 | 82 | } |
| 83 | 83 | |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | $factory = $this->app->getContainer()->get(ServerRequestFactoryInterface::class); |
| 111 | 111 | $request = $factory->createServerRequest($method, $uri); |
| 112 | 112 | |
| 113 | - foreach ($headers as $name => $value) { |
|
| 113 | + foreach ($headers as $name => $value){ |
|
| 114 | 114 | $request = $request->withAddedHeader($name, $value); |
| 115 | 115 | } |
| 116 | 116 | |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | public function fetchCookies(array $header) |
| 123 | 123 | { |
| 124 | 124 | $result = []; |
| 125 | - foreach ($header as $line) { |
|
| 125 | + foreach ($header as $line){ |
|
| 126 | 126 | $cookie = explode('=', $line); |
| 127 | 127 | $result[$cookie[0]] = rawurldecode(substr($cookie[1], 0, strpos($cookie[1], ';'))); |
| 128 | 128 | } |
@@ -77,7 +77,8 @@ discard block |
||
| 77 | 77 | array $headers = [] |
| 78 | 78 | ): ResponseInterface { |
| 79 | 79 | $r = $this->request($uri, 'GET', [], $headers, []); |
| 80 | - foreach ($attributes as $k => $v) { |
|
| 80 | + foreach ($attributes as $k => $v) |
|
| 81 | + { |
|
| 81 | 82 | $r = $r->withAttribute($k, $v); |
| 82 | 83 | } |
| 83 | 84 | |
@@ -110,7 +111,8 @@ discard block |
||
| 110 | 111 | $factory = $this->app->getContainer()->get(ServerRequestFactoryInterface::class); |
| 111 | 112 | $request = $factory->createServerRequest($method, $uri); |
| 112 | 113 | |
| 113 | - foreach ($headers as $name => $value) { |
|
| 114 | + foreach ($headers as $name => $value) |
|
| 115 | + { |
|
| 114 | 116 | $request = $request->withAddedHeader($name, $value); |
| 115 | 117 | } |
| 116 | 118 | |
@@ -122,7 +124,8 @@ discard block |
||
| 122 | 124 | public function fetchCookies(array $header) |
| 123 | 125 | { |
| 124 | 126 | $result = []; |
| 125 | - foreach ($header as $line) { |
|
| 127 | + foreach ($header as $line) |
|
| 128 | + { |
|
| 126 | 129 | $cookie = explode('=', $line); |
| 127 | 130 | $result[$cookie[0]] = rawurldecode(substr($cookie[1], 0, strpos($cookie[1], ';'))); |
| 128 | 131 | } |
@@ -32,22 +32,22 @@ |
||
| 32 | 32 | |
| 33 | 33 | public function __construct( |
| 34 | 34 | private readonly RulesInterface $repository |
| 35 | - ) { |
|
| 35 | + ){ |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public function allows(ActorInterface $actor, string $permission, array $context): bool |
| 39 | 39 | { |
| 40 | 40 | $allowed = 0; |
| 41 | - foreach (static::RULES as $rule) { |
|
| 41 | + foreach (static::RULES as $rule){ |
|
| 42 | 42 | $rule = $this->repository->get($rule); |
| 43 | 43 | |
| 44 | - if ($rule->allows($actor, $permission, $context)) { |
|
| 45 | - if (static::BEHAVIOUR === self::AT_LEAST_ONE) { |
|
| 44 | + if ($rule->allows($actor, $permission, $context)){ |
|
| 45 | + if (static::BEHAVIOUR === self::AT_LEAST_ONE){ |
|
| 46 | 46 | return true; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | $allowed++; |
| 50 | - } elseif (static::BEHAVIOUR === self::ALL) { |
|
| 50 | + } elseif (static::BEHAVIOUR === self::ALL){ |
|
| 51 | 51 | return false; |
| 52 | 52 | } |
| 53 | 53 | } |
@@ -38,16 +38,21 @@ |
||
| 38 | 38 | public function allows(ActorInterface $actor, string $permission, array $context): bool |
| 39 | 39 | { |
| 40 | 40 | $allowed = 0; |
| 41 | - foreach (static::RULES as $rule) { |
|
| 41 | + foreach (static::RULES as $rule) |
|
| 42 | + { |
|
| 42 | 43 | $rule = $this->repository->get($rule); |
| 43 | 44 | |
| 44 | - if ($rule->allows($actor, $permission, $context)) { |
|
| 45 | - if (static::BEHAVIOUR === self::AT_LEAST_ONE) { |
|
| 45 | + if ($rule->allows($actor, $permission, $context)) |
|
| 46 | + { |
|
| 47 | + if (static::BEHAVIOUR === self::AT_LEAST_ONE) |
|
| 48 | + { |
|
| 46 | 49 | return true; |
| 47 | 50 | } |
| 48 | 51 | |
| 49 | 52 | $allowed++; |
| 50 | - } elseif (static::BEHAVIOUR === self::ALL) { |
|
| 53 | + } |
|
| 54 | + elseif (static::BEHAVIOUR === self::ALL) |
|
| 55 | + { |
|
| 51 | 56 | return false; |
| 52 | 57 | } |
| 53 | 58 | } |
@@ -21,6 +21,6 @@ |
||
| 21 | 21 | |
| 22 | 22 | public function allows(ActorInterface $actor, string $permission, array $context): bool |
| 23 | 23 | { |
| 24 | - return (bool) ($this->callable)($actor, $permission, $context); |
|
| 24 | + return (bool)($this->callable)($actor, $permission, $context); |
|
| 25 | 25 | } |
| 26 | 26 | } |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | public function __construct( |
| 33 | 33 | private readonly RulesInterface $rules, |
| 34 | 34 | private readonly string $defaultRule = ForbidRule::class |
| 35 | - ) { |
|
| 35 | + ){ |
|
| 36 | 36 | $this->matcher = new Matcher(); |
| 37 | 37 | } |
| 38 | 38 | |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | public function addRole(string $role): PermissionManager |
| 45 | 45 | { |
| 46 | - if ($this->hasRole($role)) { |
|
| 46 | + if ($this->hasRole($role)){ |
|
| 47 | 47 | throw new RoleException(\sprintf("Role '%s' already exists", $role)); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | |
| 57 | 57 | public function removeRole(string $role): PermissionManager |
| 58 | 58 | { |
| 59 | - if (!$this->hasRole($role)) { |
|
| 59 | + if (!$this->hasRole($role)){ |
|
| 60 | 60 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 61 | 61 | } |
| 62 | 62 | |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | |
| 73 | 73 | public function getPermissions(string $role): array |
| 74 | 74 | { |
| 75 | - if (!$this->hasRole($role)) { |
|
| 75 | + if (!$this->hasRole($role)){ |
|
| 76 | 76 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 77 | 77 | } |
| 78 | 78 | |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | |
| 82 | 82 | public function getRule(string $role, string $permission): RuleInterface |
| 83 | 83 | { |
| 84 | - if (!$this->hasRole($role)) { |
|
| 84 | + if (!$this->hasRole($role)){ |
|
| 85 | 85 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 86 | 86 | } |
| 87 | 87 | |
@@ -91,11 +91,11 @@ discard block |
||
| 91 | 91 | |
| 92 | 92 | public function associate(string $role, string $permission, string $rule = AllowRule::class): PermissionManager |
| 93 | 93 | { |
| 94 | - if (!$this->hasRole($role)) { |
|
| 94 | + if (!$this->hasRole($role)){ |
|
| 95 | 95 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - if (!$this->rules->has($rule)) { |
|
| 98 | + if (!$this->rules->has($rule)){ |
|
| 99 | 99 | throw new PermissionException(\sprintf("Undefined rule '%s'", $rule)); |
| 100 | 100 | } |
| 101 | 101 | |
@@ -121,14 +121,14 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | private function findRule(string $role, string $permission): string |
| 123 | 123 | { |
| 124 | - if (isset($this->permissions[$role][$permission])) { |
|
| 124 | + if (isset($this->permissions[$role][$permission])){ |
|
| 125 | 125 | //O(1) check |
| 126 | 126 | return $this->permissions[$role][$permission]; |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | //Matching using star syntax |
| 130 | - foreach ($this->permissions[$role] as $pattern => $rule) { |
|
| 131 | - if ($this->matcher->matches($permission, $pattern)) { |
|
| 130 | + foreach ($this->permissions[$role] as $pattern => $rule){ |
|
| 131 | + if ($this->matcher->matches($permission, $pattern)){ |
|
| 132 | 132 | return $rule; |
| 133 | 133 | } |
| 134 | 134 | } |
@@ -43,7 +43,8 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | public function addRole(string $role): PermissionManager |
| 45 | 45 | { |
| 46 | - if ($this->hasRole($role)) { |
|
| 46 | + if ($this->hasRole($role)) |
|
| 47 | + { |
|
| 47 | 48 | throw new RoleException(\sprintf("Role '%s' already exists", $role)); |
| 48 | 49 | } |
| 49 | 50 | |
@@ -56,7 +57,8 @@ discard block |
||
| 56 | 57 | |
| 57 | 58 | public function removeRole(string $role): PermissionManager |
| 58 | 59 | { |
| 59 | - if (!$this->hasRole($role)) { |
|
| 60 | + if (!$this->hasRole($role)) |
|
| 61 | + { |
|
| 60 | 62 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 61 | 63 | } |
| 62 | 64 | |
@@ -72,7 +74,8 @@ discard block |
||
| 72 | 74 | |
| 73 | 75 | public function getPermissions(string $role): array |
| 74 | 76 | { |
| 75 | - if (!$this->hasRole($role)) { |
|
| 77 | + if (!$this->hasRole($role)) |
|
| 78 | + { |
|
| 76 | 79 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 77 | 80 | } |
| 78 | 81 | |
@@ -81,7 +84,8 @@ discard block |
||
| 81 | 84 | |
| 82 | 85 | public function getRule(string $role, string $permission): RuleInterface |
| 83 | 86 | { |
| 84 | - if (!$this->hasRole($role)) { |
|
| 87 | + if (!$this->hasRole($role)) |
|
| 88 | + { |
|
| 85 | 89 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 86 | 90 | } |
| 87 | 91 | |
@@ -91,11 +95,13 @@ discard block |
||
| 91 | 95 | |
| 92 | 96 | public function associate(string $role, string $permission, string $rule = AllowRule::class): PermissionManager |
| 93 | 97 | { |
| 94 | - if (!$this->hasRole($role)) { |
|
| 98 | + if (!$this->hasRole($role)) |
|
| 99 | + { |
|
| 95 | 100 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 96 | 101 | } |
| 97 | 102 | |
| 98 | - if (!$this->rules->has($rule)) { |
|
| 103 | + if (!$this->rules->has($rule)) |
|
| 104 | + { |
|
| 99 | 105 | throw new PermissionException(\sprintf("Undefined rule '%s'", $rule)); |
| 100 | 106 | } |
| 101 | 107 | |
@@ -121,14 +127,17 @@ discard block |
||
| 121 | 127 | */ |
| 122 | 128 | private function findRule(string $role, string $permission): string |
| 123 | 129 | { |
| 124 | - if (isset($this->permissions[$role][$permission])) { |
|
| 130 | + if (isset($this->permissions[$role][$permission])) |
|
| 131 | + { |
|
| 125 | 132 | //O(1) check |
| 126 | 133 | return $this->permissions[$role][$permission]; |
| 127 | 134 | } |
| 128 | 135 | |
| 129 | 136 | //Matching using star syntax |
| 130 | - foreach ($this->permissions[$role] as $pattern => $rule) { |
|
| 131 | - if ($this->matcher->matches($permission, $pattern)) { |
|
| 137 | + foreach ($this->permissions[$role] as $pattern => $rule) |
|
| 138 | + { |
|
| 139 | + if ($this->matcher->matches($permission, $pattern)) |
|
| 140 | + { |
|
| 132 | 141 | return $rule; |
| 133 | 142 | } |
| 134 | 143 | } |
@@ -13,7 +13,7 @@ |
||
| 13 | 13 | { |
| 14 | 14 | public function __construct( |
| 15 | 15 | private readonly array $roles |
| 16 | - ) { |
|
| 16 | + ){ |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | public function getRoles(): array |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | public function getGuard(): GuardInterface |
| 21 | 21 | { |
| 22 | 22 | $container = ContainerScope::getContainer(); |
| 23 | - if (empty($container) || !$container->has(GuardInterface::class)) { |
|
| 23 | + if (empty($container) || !$container->has(GuardInterface::class)){ |
|
| 24 | 24 | throw new ScopeException( |
| 25 | 25 | 'Unable to get `GuardInterface`, binding is missing or container scope is not set' |
| 26 | 26 | ); |
@@ -44,9 +44,9 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | protected function resolvePermission(string $permission): string |
| 46 | 46 | { |
| 47 | - if (\defined('static::GUARD_NAMESPACE')) { |
|
| 47 | + if (\defined('static::GUARD_NAMESPACE')){ |
|
| 48 | 48 | // Yay! Isolation |
| 49 | - $permission = \constant(static::class . '::' . 'GUARD_NAMESPACE') . '.' . $permission; |
|
| 49 | + $permission = \constant(static::class.'::'.'GUARD_NAMESPACE').'.'.$permission; |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | return $permission; |
@@ -20,7 +20,8 @@ discard block |
||
| 20 | 20 | public function getGuard(): GuardInterface |
| 21 | 21 | { |
| 22 | 22 | $container = ContainerScope::getContainer(); |
| 23 | - if (empty($container) || !$container->has(GuardInterface::class)) { |
|
| 23 | + if (empty($container) || !$container->has(GuardInterface::class)) |
|
| 24 | + { |
|
| 24 | 25 | throw new ScopeException( |
| 25 | 26 | 'Unable to get `GuardInterface`, binding is missing or container scope is not set' |
| 26 | 27 | ); |
@@ -44,7 +45,8 @@ discard block |
||
| 44 | 45 | */ |
| 45 | 46 | protected function resolvePermission(string $permission): string |
| 46 | 47 | { |
| 47 | - if (\defined('static::GUARD_NAMESPACE')) { |
|
| 48 | + if (\defined('static::GUARD_NAMESPACE')) |
|
| 49 | + { |
|
| 48 | 50 | // Yay! Isolation |
| 49 | 51 | $permission = \constant(static::class . '::' . 'GUARD_NAMESPACE') . '.' . $permission; |
| 50 | 52 | } |