@@ -42,8 +42,8 @@ discard block |
||
| 42 | 42 | private readonly string $clientSignature, |
| 43 | 43 | private readonly int $lifetime, |
| 44 | 44 | string $id = null |
| 45 | - ) { |
|
| 46 | - if (!empty($id) && $this->validID($id)) { |
|
| 45 | + ){ |
|
| 46 | + if (!empty($id) && $this->validID($id)){ |
|
| 47 | 47 | $this->id = $id; |
| 48 | 48 | } |
| 49 | 49 | } |
@@ -65,24 +65,24 @@ discard block |
||
| 65 | 65 | |
| 66 | 66 | public function resume(): void |
| 67 | 67 | { |
| 68 | - if ($this->isStarted()) { |
|
| 68 | + if ($this->isStarted()){ |
|
| 69 | 69 | return; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - if (!empty($this->id)) { |
|
| 72 | + if (!empty($this->id)){ |
|
| 73 | 73 | \session_id($this->id); |
| 74 | - } else { |
|
| 74 | + }else{ |
|
| 75 | 75 | // always new id |
| 76 | 76 | \session_id(\session_create_id()); |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - try { |
|
| 79 | + try{ |
|
| 80 | 80 | \session_start(['use_cookies' => false]); |
| 81 | - } catch (\Throwable $e) { |
|
| 82 | - throw new SessionException('Unable to start session', (int) $e->getCode(), $e); |
|
| 81 | + }catch (\Throwable $e){ |
|
| 82 | + throw new SessionException('Unable to start session', (int)$e->getCode(), $e); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - if (empty($this->id)) { |
|
| 85 | + if (empty($this->id)){ |
|
| 86 | 86 | //Sign newly created session |
| 87 | 87 | $_SESSION[self::CLIENT_SIGNATURE] = $this->clientSignature; |
| 88 | 88 | $_SESSION[self::SESSION_CREATED] = \time(); |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | $this->started = true; |
| 94 | 94 | |
| 95 | 95 | //Ensure that session is valid |
| 96 | - if (!$this->validSession()) { |
|
| 96 | + if (!$this->validSession()){ |
|
| 97 | 97 | $this->invalidateSession(); |
| 98 | 98 | } |
| 99 | 99 | } |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | |
| 124 | 124 | public function commit(): bool |
| 125 | 125 | { |
| 126 | - if (!$this->isStarted()) { |
|
| 126 | + if (!$this->isStarted()){ |
|
| 127 | 127 | return false; |
| 128 | 128 | } |
| 129 | 129 | |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | |
| 136 | 136 | public function abort(): bool |
| 137 | 137 | { |
| 138 | - if (!$this->isStarted()) { |
|
| 138 | + if (!$this->isStarted()){ |
|
| 139 | 139 | return false; |
| 140 | 140 | } |
| 141 | 141 | |
@@ -171,12 +171,12 @@ discard block |
||
| 171 | 171 | if ( |
| 172 | 172 | !\array_key_exists(self::CLIENT_SIGNATURE, $_SESSION) |
| 173 | 173 | || !\array_key_exists(self::SESSION_CREATED, $_SESSION) |
| 174 | - ) { |
|
| 174 | + ){ |
|
| 175 | 175 | //Missing session signature or timestamp! |
| 176 | 176 | return false; |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - if ($_SESSION[self::SESSION_CREATED] < \time() - $this->lifetime) { |
|
| 179 | + if ($_SESSION[self::SESSION_CREATED] < \time() - $this->lifetime){ |
|
| 180 | 180 | //Session expired |
| 181 | 181 | return false; |
| 182 | 182 | } |
@@ -43,7 +43,8 @@ discard block |
||
| 43 | 43 | private readonly int $lifetime, |
| 44 | 44 | string $id = null |
| 45 | 45 | ) { |
| 46 | - if (!empty($id) && $this->validID($id)) { |
|
| 46 | + if (!empty($id) && $this->validID($id)) |
|
| 47 | + { |
|
| 47 | 48 | $this->id = $id; |
| 48 | 49 | } |
| 49 | 50 | } |
@@ -65,24 +66,32 @@ discard block |
||
| 65 | 66 | |
| 66 | 67 | public function resume(): void |
| 67 | 68 | { |
| 68 | - if ($this->isStarted()) { |
|
| 69 | + if ($this->isStarted()) |
|
| 70 | + { |
|
| 69 | 71 | return; |
| 70 | 72 | } |
| 71 | 73 | |
| 72 | - if (!empty($this->id)) { |
|
| 74 | + if (!empty($this->id)) |
|
| 75 | + { |
|
| 73 | 76 | \session_id($this->id); |
| 74 | - } else { |
|
| 77 | + } |
|
| 78 | + else |
|
| 79 | + { |
|
| 75 | 80 | // always new id |
| 76 | 81 | \session_id(\session_create_id()); |
| 77 | 82 | } |
| 78 | 83 | |
| 79 | - try { |
|
| 84 | + try |
|
| 85 | + { |
|
| 80 | 86 | \session_start(['use_cookies' => false]); |
| 81 | - } catch (\Throwable $e) { |
|
| 87 | + } |
|
| 88 | + catch (\Throwable $e) |
|
| 89 | + { |
|
| 82 | 90 | throw new SessionException('Unable to start session', (int) $e->getCode(), $e); |
| 83 | 91 | } |
| 84 | 92 | |
| 85 | - if (empty($this->id)) { |
|
| 93 | + if (empty($this->id)) |
|
| 94 | + { |
|
| 86 | 95 | //Sign newly created session |
| 87 | 96 | $_SESSION[self::CLIENT_SIGNATURE] = $this->clientSignature; |
| 88 | 97 | $_SESSION[self::SESSION_CREATED] = \time(); |
@@ -93,7 +102,8 @@ discard block |
||
| 93 | 102 | $this->started = true; |
| 94 | 103 | |
| 95 | 104 | //Ensure that session is valid |
| 96 | - if (!$this->validSession()) { |
|
| 105 | + if (!$this->validSession()) |
|
| 106 | + { |
|
| 97 | 107 | $this->invalidateSession(); |
| 98 | 108 | } |
| 99 | 109 | } |
@@ -123,7 +133,8 @@ discard block |
||
| 123 | 133 | |
| 124 | 134 | public function commit(): bool |
| 125 | 135 | { |
| 126 | - if (!$this->isStarted()) { |
|
| 136 | + if (!$this->isStarted()) |
|
| 137 | + { |
|
| 127 | 138 | return false; |
| 128 | 139 | } |
| 129 | 140 | |
@@ -135,7 +146,8 @@ discard block |
||
| 135 | 146 | |
| 136 | 147 | public function abort(): bool |
| 137 | 148 | { |
| 138 | - if (!$this->isStarted()) { |
|
| 149 | + if (!$this->isStarted()) |
|
| 150 | + { |
|
| 139 | 151 | return false; |
| 140 | 152 | } |
| 141 | 153 | |
@@ -176,7 +188,8 @@ discard block |
||
| 176 | 188 | return false; |
| 177 | 189 | } |
| 178 | 190 | |
| 179 | - if ($_SESSION[self::SESSION_CREATED] < \time() - $this->lifetime) { |
|
| 191 | + if ($_SESSION[self::SESSION_CREATED] < \time() - $this->lifetime) |
|
| 192 | + { |
|
| 180 | 193 | //Session expired |
| 181 | 194 | return false; |
| 182 | 195 | } |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | public function __construct( |
| 15 | 15 | private readonly FilesInterface $files, |
| 16 | 16 | private readonly string $directory |
| 17 | - ) { |
|
| 17 | + ){ |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | public function close(): bool |
@@ -35,8 +35,8 @@ discard block |
||
| 35 | 35 | */ |
| 36 | 36 | public function gc(int $maxlifetime): int |
| 37 | 37 | { |
| 38 | - foreach ($this->files->getFiles($this->directory) as $filename) { |
|
| 39 | - if ($this->files->time($filename) < \time() - $maxlifetime) { |
|
| 38 | + foreach ($this->files->getFiles($this->directory) as $filename){ |
|
| 39 | + if ($this->files->time($filename) < \time() - $maxlifetime){ |
|
| 40 | 40 | $this->files->delete($filename); |
| 41 | 41 | } |
| 42 | 42 | } |
@@ -35,8 +35,10 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | public function gc(int $maxlifetime): int |
| 37 | 37 | { |
| 38 | - foreach ($this->files->getFiles($this->directory) as $filename) { |
|
| 39 | - if ($this->files->time($filename) < \time() - $maxlifetime) { |
|
| 38 | + foreach ($this->files->getFiles($this->directory) as $filename) |
|
| 39 | + { |
|
| 40 | + if ($this->files->time($filename) < \time() - $maxlifetime) |
|
| 41 | + { |
|
| 40 | 42 | $this->files->delete($filename); |
| 41 | 43 | } |
| 42 | 44 | } |
@@ -19,21 +19,21 @@ |
||
| 19 | 19 | public function __construct( |
| 20 | 20 | private readonly SessionConfig $config, |
| 21 | 21 | private readonly FactoryInterface $factory |
| 22 | - ) { |
|
| 22 | + ){ |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | public function initSession(string $clientSignature, string $id = null): SessionInterface |
| 26 | 26 | { |
| 27 | - if (\session_status() === PHP_SESSION_ACTIVE) { |
|
| 27 | + if (\session_status() === PHP_SESSION_ACTIVE){ |
|
| 28 | 28 | throw new MultipleSessionException('Unable to initiate session, session already started'); |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | // Initiating proper session handler |
| 32 | - if ($this->config->getHandler() !== null) { |
|
| 33 | - try { |
|
| 32 | + if ($this->config->getHandler() !== null){ |
|
| 33 | + try{ |
|
| 34 | 34 | $handler = $this->config->getHandler()->resolve($this->factory); |
| 35 | - } catch (\Throwable | ContainerExceptionInterface $e) { |
|
| 36 | - throw new SessionException($e->getMessage(), (int) $e->getCode(), $e); |
|
| 35 | + }catch (\Throwable | ContainerExceptionInterface $e){ |
|
| 36 | + throw new SessionException($e->getMessage(), (int)$e->getCode(), $e); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | \session_set_save_handler($handler, true); |
@@ -24,15 +24,20 @@ |
||
| 24 | 24 | |
| 25 | 25 | public function initSession(string $clientSignature, string $id = null): SessionInterface |
| 26 | 26 | { |
| 27 | - if (\session_status() === PHP_SESSION_ACTIVE) { |
|
| 27 | + if (\session_status() === PHP_SESSION_ACTIVE) |
|
| 28 | + { |
|
| 28 | 29 | throw new MultipleSessionException('Unable to initiate session, session already started'); |
| 29 | 30 | } |
| 30 | 31 | |
| 31 | 32 | // Initiating proper session handler |
| 32 | - if ($this->config->getHandler() !== null) { |
|
| 33 | - try { |
|
| 33 | + if ($this->config->getHandler() !== null) |
|
| 34 | + { |
|
| 35 | + try |
|
| 36 | + { |
|
| 34 | 37 | $handler = $this->config->getHandler()->resolve($this->factory); |
| 35 | - } catch (\Throwable | ContainerExceptionInterface $e) { |
|
| 38 | + } |
|
| 39 | + catch (\Throwable | ContainerExceptionInterface $e) |
|
| 40 | + { |
|
| 36 | 41 | throw new SessionException($e->getMessage(), (int) $e->getCode(), $e); |
| 37 | 42 | } |
| 38 | 43 | |
@@ -19,7 +19,7 @@ |
||
| 19 | 19 | |
| 20 | 20 | $this->assertSame('a:2:{i:0;s:4:"some";i:1;s:8:"elements";}', $serializer->serialize(['some', 'elements'])); |
| 21 | 21 | $this->assertSame(['some', 'elements'], $serializer->unserialize('a:2:{i:0;s:4:"some";i:1;s:8:"elements";}')); |
| 22 | - $this->assertSame(['some', 'elements'], $serializer->unserialize(new class() implements \Stringable { |
|
| 22 | + $this->assertSame(['some', 'elements'], $serializer->unserialize(new class() implements \Stringable{ |
|
| 23 | 23 | public function __toString(): string |
| 24 | 24 | { |
| 25 | 25 | return 'a:2:{i:0;s:4:"some";i:1;s:8:"elements";}'; |
@@ -17,7 +17,7 @@ |
||
| 17 | 17 | |
| 18 | 18 | $this->assertSame('["some","elements"]', $serializer->serialize(['some', 'elements'])); |
| 19 | 19 | $this->assertSame(['some', 'elements'], $serializer->unserialize('["some","elements"]')); |
| 20 | - $this->assertSame(['some', 'elements'], $serializer->unserialize(new class() implements \Stringable { |
|
| 20 | + $this->assertSame(['some', 'elements'], $serializer->unserialize(new class() implements \Stringable{ |
|
| 21 | 21 | public function __toString(): string |
| 22 | 22 | { |
| 23 | 23 | return '["some","elements"]'; |
@@ -10,33 +10,33 @@ |
||
| 10 | 10 | |
| 11 | 11 | final class PhpSerializer implements SerializerInterface |
| 12 | 12 | { |
| 13 | - public function serialize(mixed $payload): string|\Stringable |
|
| 13 | + public function serialize(mixed $payload): string | \Stringable |
|
| 14 | 14 | { |
| 15 | 15 | return \serialize($payload); |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | - public function unserialize(\Stringable|string $payload, object|string|null $type = null): mixed |
|
| 18 | + public function unserialize(\Stringable | string $payload, object | string | null $type = null): mixed |
|
| 19 | 19 | { |
| 20 | - if (\is_object($type)) { |
|
| 20 | + if (\is_object($type)){ |
|
| 21 | 21 | $type = $type::class; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - if (\is_string($type) && !\class_exists($type) && !\interface_exists($type)) { |
|
| 24 | + if (\is_string($type) && !\class_exists($type) && !\interface_exists($type)){ |
|
| 25 | 25 | throw new InvalidArgumentException(\sprintf('Class or interface `%s` doesn\'t exist.', $type)); |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | return $this->runUnserialize($payload, $type); |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - private function runUnserialize(\Stringable|string $payload, ?string $type = null): mixed |
|
| 31 | + private function runUnserialize(\Stringable | string $payload, ?string $type = null): mixed |
|
| 32 | 32 | { |
| 33 | - $result = \unserialize((string) $payload, $type ? [] : ['allowed_classes' => false]); |
|
| 33 | + $result = \unserialize((string)$payload, $type ? [] : ['allowed_classes' => false]); |
|
| 34 | 34 | |
| 35 | - if ($result === false) { |
|
| 35 | + if ($result === false){ |
|
| 36 | 36 | throw new UnserializeException('Failed to unserialize data.'); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - if ($type !== null && !$result instanceof $type) { |
|
| 39 | + if ($type !== null && !$result instanceof $type){ |
|
| 40 | 40 | throw new InvalidArgumentException(\sprintf( |
| 41 | 41 | 'Data received after unserializing must be of type: `%s`, received `%s`', |
| 42 | 42 | $type, |
@@ -17,11 +17,13 @@ discard block |
||
| 17 | 17 | |
| 18 | 18 | public function unserialize(\Stringable|string $payload, object|string|null $type = null): mixed |
| 19 | 19 | { |
| 20 | - if (\is_object($type)) { |
|
| 20 | + if (\is_object($type)) |
|
| 21 | + { |
|
| 21 | 22 | $type = $type::class; |
| 22 | 23 | } |
| 23 | 24 | |
| 24 | - if (\is_string($type) && !\class_exists($type) && !\interface_exists($type)) { |
|
| 25 | + if (\is_string($type) && !\class_exists($type) && !\interface_exists($type)) |
|
| 26 | + { |
|
| 25 | 27 | throw new InvalidArgumentException(\sprintf('Class or interface `%s` doesn\'t exist.', $type)); |
| 26 | 28 | } |
| 27 | 29 | |
@@ -32,11 +34,13 @@ discard block |
||
| 32 | 34 | { |
| 33 | 35 | $result = \unserialize((string) $payload, $type ? [] : ['allowed_classes' => false]); |
| 34 | 36 | |
| 35 | - if ($result === false) { |
|
| 37 | + if ($result === false) |
|
| 38 | + { |
|
| 36 | 39 | throw new UnserializeException('Failed to unserialize data.'); |
| 37 | 40 | } |
| 38 | 41 | |
| 39 | - if ($type !== null && !$result instanceof $type) { |
|
| 42 | + if ($type !== null && !$result instanceof $type) |
|
| 43 | + { |
|
| 40 | 44 | throw new InvalidArgumentException(\sprintf( |
| 41 | 45 | 'Data received after unserializing must be of type: `%s`, received `%s`', |
| 42 | 46 | $type, |
@@ -25,7 +25,7 @@ |
||
| 25 | 25 | return ($this->serializeCallback)($payload); |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - public function unserialize(string|\Stringable $payload, string|object|null $type = null): mixed |
|
| 28 | + public function unserialize(string | \Stringable $payload, string | object | null $type = null): mixed |
|
| 29 | 29 | { |
| 30 | 30 | return ($this->unserializeCallback)($payload, $type); |
| 31 | 31 | } |
@@ -14,11 +14,11 @@ discard block |
||
| 14 | 14 | /** |
| 15 | 15 | * @throws SerializeException |
| 16 | 16 | */ |
| 17 | - public function serialize(mixed $payload): string|\Stringable |
|
| 17 | + public function serialize(mixed $payload): string | \Stringable |
|
| 18 | 18 | { |
| 19 | - try { |
|
| 19 | + try{ |
|
| 20 | 20 | return \json_encode($payload, JSON_THROW_ON_ERROR); |
| 21 | - } catch (\JsonException $e) { |
|
| 21 | + }catch (\JsonException $e){ |
|
| 22 | 22 | throw new SerializeException($e->getMessage(), $e->getCode(), $e); |
| 23 | 23 | } |
| 24 | 24 | } |
@@ -26,17 +26,17 @@ discard block |
||
| 26 | 26 | /** |
| 27 | 27 | * @throws \JsonException |
| 28 | 28 | */ |
| 29 | - public function unserialize(\Stringable|string $payload, object|string|null $type = null): mixed |
|
| 29 | + public function unserialize(\Stringable | string $payload, object | string | null $type = null): mixed |
|
| 30 | 30 | { |
| 31 | - if ($type !== null) { |
|
| 31 | + if ($type !== null){ |
|
| 32 | 32 | throw new InvalidArgumentException( |
| 33 | 33 | \sprintf('Serializer `%s` does not support data hydration to an object.', self::class) |
| 34 | 34 | ); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - try { |
|
| 38 | - return \json_decode((string) $payload, true, 512, JSON_THROW_ON_ERROR); |
|
| 39 | - } catch (\JsonException $e) { |
|
| 37 | + try{ |
|
| 38 | + return \json_decode((string)$payload, true, 512, JSON_THROW_ON_ERROR); |
|
| 39 | + }catch (\JsonException $e){ |
|
| 40 | 40 | throw new UnserializeException($e->getMessage(), $e->getCode(), $e); |
| 41 | 41 | } |
| 42 | 42 | } |
@@ -16,9 +16,12 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | public function serialize(mixed $payload): string|\Stringable |
| 18 | 18 | { |
| 19 | - try { |
|
| 19 | + try |
|
| 20 | + { |
|
| 20 | 21 | return \json_encode($payload, JSON_THROW_ON_ERROR); |
| 21 | - } catch (\JsonException $e) { |
|
| 22 | + } |
|
| 23 | + catch (\JsonException $e) |
|
| 24 | + { |
|
| 22 | 25 | throw new SerializeException($e->getMessage(), $e->getCode(), $e); |
| 23 | 26 | } |
| 24 | 27 | } |
@@ -28,15 +31,19 @@ discard block |
||
| 28 | 31 | */ |
| 29 | 32 | public function unserialize(\Stringable|string $payload, object|string|null $type = null): mixed |
| 30 | 33 | { |
| 31 | - if ($type !== null) { |
|
| 34 | + if ($type !== null) |
|
| 35 | + { |
|
| 32 | 36 | throw new InvalidArgumentException( |
| 33 | 37 | \sprintf('Serializer `%s` does not support data hydration to an object.', self::class) |
| 34 | 38 | ); |
| 35 | 39 | } |
| 36 | 40 | |
| 37 | - try { |
|
| 41 | + try |
|
| 42 | + { |
|
| 38 | 43 | return \json_decode((string) $payload, true, 512, JSON_THROW_ON_ERROR); |
| 39 | - } catch (\JsonException $e) { |
|
| 44 | + } |
|
| 45 | + catch (\JsonException $e) |
|
| 46 | + { |
|
| 40 | 47 | throw new UnserializeException($e->getMessage(), $e->getCode(), $e); |
| 41 | 48 | } |
| 42 | 49 | } |
@@ -6,7 +6,7 @@ |
||
| 6 | 6 | |
| 7 | 7 | interface SerializerInterface |
| 8 | 8 | { |
| 9 | - public function serialize(mixed $payload): string|\Stringable; |
|
| 9 | + public function serialize(mixed $payload): string | \Stringable; |
|
| 10 | 10 | |
| 11 | - public function unserialize(string|\Stringable $payload, string|object|null $type = null): mixed; |
|
| 11 | + public function unserialize(string | \Stringable $payload, string | object | null $type = null): mixed; |
|
| 12 | 12 | } |