Skip to content

Commit cbc59c6

Browse files
committed
Add support for enums to the PHP session reader
1 parent 45c3b0d commit cbc59c6

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Session/Reader/PhpReader.php

+8
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ private function getSerializedSegmentLength(string $data): int|false
8383
// Add characters for the closing quote and semicolon
8484
return \strlen($matches[0]) + (int) substr($matches[0], 2, -2) + 2;
8585

86+
// Enum
87+
case 'E':
88+
if (!preg_match('/^E:\d+:"[^"]+";/', $data, $matches)) {
89+
return false;
90+
}
91+
92+
return strlen($matches[0]);
93+
8694
// Array or object value
8795
case 'a':
8896
case 'O':

tests/Session/Reader/PhpReaderTest.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class PhpReaderTest extends TestCase
1313
*/
1414
public static function dataRead(): \Generator
1515
{
16-
yield 'Basic Symfony session payload' => ['_sf2_attributes|a:3:{s:3:"foo";s:3:"bar";s:12:"messages.foo";s:3:"bar";s:4:"data";a:1:{s:3:"foo";s:3:"bar";}}_sf2_meta|a:3:{s:1:"u";i:1653958488;s:1:"c";i:1653958488;s:1:"l";i:0;}'];
16+
yield 'Basic Symfony session payload' => ['_sf2_attributes|a:5:{s:3:"foo";s:3:"bar";s:12:"messages.foo";s:3:"bar";s:4:"data";a:1:{s:3:"foo";s:3:"bar";}s:6:"status";E:58:"BabDev\WebSocket\Server\Tests\Session\Reader\Status:Active";s:4:"suit";E:55:"BabDev\WebSocket\Server\Tests\Session\Reader\Suit:Heart";}_sf2_meta|a:3:{s:1:"u";i:1653958488;s:1:"c";i:1653958488;s:1:"l";i:0;}'];
1717

1818
yield 'Symfony application session payload' => ['_sf2_attributes|a:3:{s:14:"_security_main";s:340:"O:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":3:{i:0;N;i:1;s:4:"main";i:2;a:5:{i:0;O:15:"App\Entity\User":3:{s:2:"id";i:123456;s:5:"email";s:17:"xxxxxxx@xxxxx.xxx";s:8:"password";s:60:"$2y$13$9klfX3owVXOQ4jjoqTMi8.71kW5i6rHixw5E2kRE1KW7yPELxmSHi";}i:1;b:1;i:2;N;i:3;a:0:{}i:4;a:1:{i:0;s:9:"ROLE_USER";}}}";s:23:"_security.last_username";s:17:"xxxxxxx@xxxxx.xxx";s:20:"_security.last_error";O:67:"Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException":5:{i:0;N;i:1;i:0;i:2;s:19:"Invalid CSRF token.";i:3;s:83:"/var/www/html/vendor/symfony/security-http/EventListener/CsrfProtectionListener.php";i:4;i:51;}}_sf2_meta|a:3:{s:1:"u";i:1724707524;s:1:"c";i:1724705819;s:1:"l";i:0;}'];
1919
}
@@ -30,3 +30,17 @@ public function testReadsData(string $input): void
3030
$this->assertArrayHasKey('_sf2_meta', $output);
3131
}
3232
}
33+
34+
enum Status: string
35+
{
36+
case Active = 'active';
37+
case Inactive = 'inactive';
38+
}
39+
40+
enum Suit
41+
{
42+
case Club;
43+
case Diamond;
44+
case Heart;
45+
case Spade;
46+
}

0 commit comments

Comments
 (0)