22
33namespace GeekJOB ;
44
5+
6+ /**
7+ *
8+ */
9+ enum WorkStatus
10+ {
11+ case Debug;
12+ case Test;
13+ case Production;
14+ }
15+
16+
517/**
618 * Node.js epress-session compatible handler
719 *
@@ -15,8 +27,21 @@ class ExpressjsSessionHandler extends \SessionHandler
1527 /**
1628 * @var string
1729 */
18- private string $ secret ;
19- private array $ config ;
30+ #[\Assert \All(
31+ new \Assert \NotNull ,
32+ new \Assert \Length (min: 24 ))
33+ ]
34+ private string $ secret ;
35+
36+ /**
37+ *
38+ */
39+ final public const MDBG = WorkStatus::Production;
40+
41+ /**
42+ * @var object
43+ */
44+ public readonly object $ store ;
2045
2146 /**
2247 * @param array $cfg
@@ -44,62 +69,62 @@ public static function register(array $cfg): self
4469 /**
4570 * Constructor
4671 *
47- * @param string $secret The secret defined in your express-session library.
48- */
49- public function __construct (array $ cfg )
72+ * @param array $config
73+ * @throws \Exception
74+ */
75+ public function __construct (private array $ config )
5076 {
5177 // Set session cookie settings
52- if (!empty ($ cfg ['cookie ' ])) {
53- $ cfg ['cookie ' ]['maxage ' ] = (int )$ cfg ['cookie ' ]['maxage ' ];
78+ if (!empty ($ config ['cookie ' ])) {
79+ $ config ['cookie ' ]['maxage ' ] = (int )$ config ['cookie ' ]['maxage ' ];
5480
5581 session_set_cookie_params (
56- $ cfg ['cookie ' ]['maxage ' ],
57- $ cfg ['cookie ' ]['path ' ],
58- $ cfg ['cookie ' ]['domain ' ],
59- $ cfg ['cookie ' ]['secure ' ] ?? true ,
60- $ cfg ['cookie ' ]['httpOnly ' ] ?? true
82+ $ config ['cookie ' ]['maxage ' ],
83+ $ config ['cookie ' ]['path ' ],
84+ $ config ['cookie ' ]['domain ' ],
85+ $ config ['cookie ' ]['secure ' ] ?? true ,
86+ $ config ['cookie ' ]['httpOnly ' ] ?? true
6187 );
6288
63- if (empty ($ cfg ['cookie ' ]['expires ' ]))
64- $ cfg ['cookie ' ]['expires ' ] = str_replace (
89+ if (empty ($ config ['cookie ' ]['expires ' ]))
90+ $ config ['cookie ' ]['expires ' ] = str_replace (
6591 '+00:00 ' ,
6692 'Z ' ,
67- gmdate ('c ' , time () + $ cfg ['cookie ' ]['maxage ' ])
93+ gmdate ('c ' , time () + $ config ['cookie ' ]['maxage ' ])
6894 );
6995 }
7096
7197 // Configure storage.
7298 // A common method of storing sessions is the Radish database.
7399 // These settings can be made in php.ini for the production version
74- if (!empty ($ cfg ['store ' ])) {
75- ini_set ('session.save_handler ' , $ cfg ['store ' ]['handler ' ]);
76- $ save_path = $ cfg ['store ' ]['path ' ];
77- switch ($ cfg ['store ' ]['handler ' ])
100+ if (!empty ($ config ['store ' ])) {
101+ ini_set ('session.save_handler ' , $ config ['store ' ]['handler ' ]);
102+ $ save_path = $ config ['store ' ]['path ' ];
103+ switch ($ config ['store ' ]['handler ' ])
78104 {
79105 case 'dragonfly ' :
80106 case 'redis ' :
81- if (!empty ($ cfg ['store ' ]['prefix ' ]))
82- $ save_path .= "?prefix= {$ cfg ['store ' ]['prefix ' ]}" ;
107+ if (!empty ($ config ['store ' ]['prefix ' ]))
108+ $ save_path .= "?prefix= {$ config ['store ' ]['prefix ' ]}" ;
83109 break ;
84110
85111 case 'mongo ' :
86112 throw new \Exception ('Driver for Mongo coming soon :) ' );
87113
88114 default :
89- throw new \Exception ("Driver for {$ cfg ['store ' ]['handler ' ]} not implemented! " );
115+ throw new \Exception ("Driver for {$ config ['store ' ]['handler ' ]} not implemented! " );
90116 }
91117
92118 ini_set ('session.save_path ' , $ save_path );
93119 ini_set ('session.serialize_handler ' , 'php_serialize ' );
94120
95- if ($ cfg ['store ' ]['ttl ' ] > 0 )
96- ini_set ('session.gc_maxlifetime ' , (string )$ cfg ['store ' ]['ttl ' ]);
97- if (isset ($ cfg ['cookie ' ]['maxage ' ]))
98- ini_set ('session.cookie_lifetime ' , (string )$ cfg ['cookie ' ]['maxage ' ]);
121+ if ($ config ['store ' ]['ttl ' ] > 0 )
122+ ini_set ('session.gc_maxlifetime ' , (string )$ config ['store ' ]['ttl ' ]);
123+ if (isset ($ config ['cookie ' ]['maxage ' ]))
124+ ini_set ('session.cookie_lifetime ' , (string )$ config ['cookie ' ]['maxage ' ]);
99125 }
100126
101- $ this ->secret = (string )$ cfg ['secret ' ];
102- $ this ->config = $ cfg ;
127+ $ this ->secret = (string )$ config ['secret ' ];
103128 }
104129
105130 /**
@@ -237,6 +262,15 @@ public function register_session_cookie(): void
237262 $ _SESSION ['cookie ' ]['expires ' ] = $ this ->config ['cookie ' ]['expires ' ];
238263 }
239264
265+
266+ /**
267+ * @param Countable&Iterator $value
268+ * @return never
269+ */
270+ protected function _mongo_handler (Iterator &Countable $ value ): never {
271+ // @todo
272+ exit ();
273+ }
240274}
241275
242276
0 commit comments