• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / session / 5802848605

pending completion
5802848605

push

github

natanfelles
Get save handler configs from main instances

9 of 9 new or added lines in 1 file covered. (100.0%)

963 of 1028 relevant lines covered (93.68%)

39.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.11
/src/Debug/SessionCollector.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Session Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\Session\Debug;
11

12
use Framework\Debug\Collector;
13
use Framework\Debug\Debugger;
14
use Framework\Helpers\ArraySimple;
15
use Framework\Session\SaveHandler;
16
use Framework\Session\SaveHandlers\DatabaseHandler;
17
use Framework\Session\SaveHandlers\FilesHandler;
18
use Framework\Session\SaveHandlers\MemcachedHandler;
19
use Framework\Session\SaveHandlers\RedisHandler;
20
use Framework\Session\Session;
21

22
/**
23
 * Class SessionCollector.
24
 *
25
 * @package session
26
 */
27
class SessionCollector extends Collector
28
{
29
    protected Session $session;
30
    /**
31
     * @var array<string>
32
     */
33
    protected array $options = [];
34
    protected SaveHandler $saveHandler;
35

36
    public function setSession(Session $session) : static
37
    {
38
        $this->session = $session;
10✔
39
        return $this;
10✔
40
    }
41

42
    /**
43
     * @param array<string> $options
44
     *
45
     * @return static
46
     */
47
    public function setOptions(array $options) : static
48
    {
49
        $this->options = $options;
10✔
50
        return $this;
10✔
51
    }
52

53
    public function setSaveHandler(SaveHandler $handler) : static
54
    {
55
        $this->saveHandler = $handler;
5✔
56
        return $this;
5✔
57
    }
58

59
    public function getContents() : string
60
    {
61
        if ( ! isset($this->session)) {
11✔
62
            return '<p>No Session instance has been set on this collector.</p>';
1✔
63
        }
64
        if ( ! $this->session->isActive()) {
10✔
65
            return '<p>Session is inactive.</p>';
1✔
66
        }
67
        \ob_start(); ?>
9✔
68
        <p><strong>Name:</strong> <?= \ini_get('session.name') ?></p>
9✔
69
        <p><strong>Id:</strong> <?= $this->session->id() ?></p>
9✔
70
        <h1>Data</h1>
71
        <?= $this->renderData() ?>
9✔
72
        <h1>Flash</h1>
9✔
73
        <?= $this->renderFlash() ?>
9✔
74
        <h1>Temp</h1>
9✔
75
        <?= $this->renderTemp() ?>
9✔
76
        <h1>Save Handler</h1>
9✔
77
        <?= $this->renderSaveHandler() ?>
9✔
78
        <h1>Cookie Params</h1>
9✔
79
        <?= $this->renderCookieParams() ?>
9✔
80
        <h1>Auto Regenerate Id</h1>
9✔
81
        <?php
82
        echo $this->renderAutoRegenerateId();
9✔
83
        return \ob_get_clean(); // @phpstan-ignore-line
9✔
84
    }
85

86
    protected function renderData() : string
87
    {
88
        $data = $this->session->getAll();
9✔
89
        unset($data['$']);
9✔
90
        if (empty($data)) {
9✔
91
            return '<p>No data.</p>';
9✔
92
        }
93
        \ksort($data);
1✔
94
        \ob_start(); ?>
1✔
95
        <table>
1✔
96
            <thead>
1✔
97
            <tr>
1✔
98
                <th>Key</th>
1✔
99
                <th>Value</th>
1✔
100
            </tr>
1✔
101
            </thead>
1✔
102
            <tbody>
1✔
103
            <?php foreach ($data as $key => $value): ?>
1✔
104
                <tr>
1✔
105
                    <td><?= \htmlentities((string) $key) ?></td>
1✔
106
                    <td><pre><code class="language-php"><?=
1✔
107
                                \htmlentities(Debugger::makeDebugValue($value))
1✔
108
                ?></code></pre>
1✔
109
                    </td>
110
                </tr>
111
            <?php endforeach ?>
112
            </tbody>
1✔
113
        </table>
1✔
114
        <?php
1✔
115
        return \ob_get_clean(); // @phpstan-ignore-line
1✔
116
    }
117

118
    protected function renderFlash() : string
119
    {
120
        $old = $this->renderFlashOld();
9✔
121
        $new = $this->renderFlashNew();
9✔
122
        if ($old === '' && $new === '') {
9✔
123
            return '<p>No flash data.</p>';
9✔
124
        }
125
        return $old . $new;
1✔
126
    }
127

128
    protected function renderFlashOld() : string
129
    {
130
        $data = ArraySimple::value('$[flash][old]', $this->session->getAll());
9✔
131
        if (empty($data)) {
9✔
132
            return '';
9✔
133
        }
134
        \ob_start(); ?>
1✔
135
        <h2>Old</h2>
1✔
136
        <p>Flash data available in the current request.</p>
1✔
137
        <table>
1✔
138
            <thead>
1✔
139
            <tr>
1✔
140
                <th>Key</th>
1✔
141
                <th>Value</th>
1✔
142
            </tr>
1✔
143
            </thead>
1✔
144
            <tbody>
1✔
145
            <?php foreach ($data as $key => $value): ?>
1✔
146
                <tr>
1✔
147
                    <td><?= \htmlentities($key) ?></td>
1✔
148
                    <td><pre><code class="language-php"><?=
1✔
149
                                \htmlentities(Debugger::makeDebugValue($value))
1✔
150
                ?></code></pre>
1✔
151
                    </td>
152
                </tr>
153
            <?php endforeach ?>
154
            </tbody>
1✔
155
        </table>
1✔
156
        <?php
1✔
157
        return \ob_get_clean(); // @phpstan-ignore-line
1✔
158
    }
159

160
    protected function renderFlashNew() : string
161
    {
162
        $data = ArraySimple::value('$[flash][new]', $this->session->getAll());
9✔
163
        if (empty($data)) {
9✔
164
            return '';
9✔
165
        }
166
        \ob_start(); ?>
1✔
167
        <h2>New</h2>
1✔
168
        <p>Flash data available in the next request.</p>
1✔
169
        <table>
1✔
170
            <thead>
1✔
171
            <tr>
1✔
172
                <th>Key</th>
1✔
173
                <th>Value</th>
1✔
174
            </tr>
1✔
175
            </thead>
1✔
176
            <tbody>
1✔
177
            <?php foreach ($data as $key => $value): ?>
1✔
178
                <tr>
1✔
179
                    <td><?= \htmlentities($key) ?></td>
1✔
180
                    <td><pre><code class="language-php"><?=
1✔
181
                                \htmlentities(Debugger::makeDebugValue($value))
1✔
182
                ?></code></pre>
1✔
183
                    </td>
184
                </tr>
185
            <?php endforeach ?>
186
            </tbody>
1✔
187
        </table>
1✔
188
        <?php
1✔
189
        return \ob_get_clean(); // @phpstan-ignore-line
1✔
190
    }
191

192
    protected function renderTemp() : string
193
    {
194
        $data = ArraySimple::value('$[temp]', $this->session->getAll());
9✔
195
        if (empty($data)) {
9✔
196
            return '<p>No temp data.</p>';
9✔
197
        }
198
        \ob_start(); ?>
1✔
199
        <table>
1✔
200
            <thead>
1✔
201
            <tr>
1✔
202
                <th>Key</th>
1✔
203
                <th>Value</th>
1✔
204
                <th>TTL</th>
1✔
205
            </tr>
1✔
206
            </thead>
1✔
207
            <tbody>
1✔
208
            <?php foreach ($data as $key => $value): ?>
1✔
209
                <tr>
1✔
210
                    <td><?= \htmlentities($key) ?></td>
1✔
211
                    <td><pre><code class="language-php"><?=
1✔
212
                                \htmlentities(Debugger::makeDebugValue($value))
1✔
213
                ?></code></pre>
1✔
214
                    </td>
215
                    <td><?= \date('Y-m-d H:i:s', $value['ttl']) ?></td>
1✔
216
                </tr>
217
            <?php endforeach ?>
218
            </tbody>
1✔
219
        </table>
1✔
220
        <?php
1✔
221
        return \ob_get_clean(); // @phpstan-ignore-line
1✔
222
    }
223

224
    protected function renderSaveHandler() : string
225
    {
226
        \ob_start(); ?>
9✔
227
        <p><strong>Save Handler:</strong> <?= \ini_get('session.save_handler') ?></p>
9✔
228
        <p><strong>Serializer:</strong> <?= \ini_get('session.serialize_handler') ?></p>
9✔
229
        <?php
230
        if (isset($this->saveHandler)): ?>
9✔
231
            <table>
5✔
232
                <tbody>
5✔
233
                <tr>
5✔
234
                    <th>Class</th>
5✔
235
                    <td><?= $this->saveHandler::class ?></td>
5✔
236
                </tr>
237
                <?php foreach ($this->getSaveHandlerConfigs() as $key => $value): ?>
5✔
238
                    <?= $this->renderSaveHandlerTableRows($key, $value) ?>
4✔
239
                <?php endforeach ?>
4✔
240
                </tbody>
5✔
241
            </table>
5✔
242
        <?php
5✔
243
        endif;
244
        return \ob_get_clean(); // @phpstan-ignore-line
9✔
245
    }
246

247
    protected function renderSaveHandlerTableRows(string $key, mixed $value) : string
248
    {
249
        \ob_start(); ?>
4✔
250
        <?php
4✔
251
        if (\is_array($value)):
4✔
252
            $count = \count($value); ?>
2✔
253
            <tr>
2✔
254
                <th rowspan="<?= $count ?>"><?= $key ?></th>
2✔
255
                <td>
256
                    <table>
257
                        <?php foreach ($value[\array_key_first($value)] as $k => $v): ?>
2✔
258
                            <tr>
2✔
259
                                <th><?= \htmlentities($k) ?></th>
2✔
260
                                <td><?= \htmlentities((string) $v) ?></td>
2✔
261
                            </tr>
262
                        <?php endforeach ?>
263
                    </table>
2✔
264
                </td>
2✔
265
            </tr>
2✔
266
            <?php
2✔
267
            for ($i = 1; $i < $count; $i++): ?>
2✔
268
                <tr>
×
269
                    <td>
×
270
                        <table>
×
271
                            <?php foreach ($value[$i] as $k => $v): ?>
×
272
                                <tr>
×
273
                                    <th><?= \htmlentities($k) ?></th>
×
274
                                    <td><?= \htmlentities((string) $v) ?></td>
×
275
                                </tr>
276
                            <?php endforeach ?>
277
                        </table>
×
278
                    </td>
×
279
                </tr>
×
280
            <?php
×
281
            endfor;
282
            return \ob_get_clean(); // @phpstan-ignore-line
2✔
283
        endif; ?>
284
        <tr>
4✔
285
            <th><?= \htmlentities($key) ?></th>
4✔
286
            <td><?= \htmlentities((string) $value) ?></td>
4✔
287
        </tr>
288
        <?php
289
        return \ob_get_clean(); // @phpstan-ignore-line
4✔
290
    }
291

292
    protected function renderCookieParams() : string
293
    {
294
        $params = \session_get_cookie_params();
9✔
295
        \ob_start(); ?>
9✔
296
        <table>
9✔
297
            <thead>
9✔
298
            <tr>
9✔
299
                <th>Lifetime</th>
9✔
300
                <th>Path</th>
9✔
301
                <th>Domain</th>
9✔
302
                <th>Is Secure</th>
9✔
303
                <th>Is HTTP Only</th>
9✔
304
                <th>SameSite</th>
9✔
305
            </tr>
9✔
306
            </thead>
9✔
307
            <tbody>
9✔
308
            <tr>
9✔
309
                <td><?= $params['lifetime'] ?></td>
9✔
310
                <td><?= $params['path'] ?></td>
9✔
311
                <td><?= $params['domain'] ?></td>
9✔
312
                <td><?= $params['secure'] ? 'Yes' : 'No' ?></td>
9✔
313
                <td><?= $params['httponly'] ? 'Yes' : 'No' ?></td>
9✔
314
                <td><?= $params['samesite'] ?></td>
9✔
315
            </tr>
316
            </tbody>
317
        </table>
318
        <?php
319
        return \ob_get_clean(); // @phpstan-ignore-line
9✔
320
    }
321

322
    protected function renderAutoRegenerateId() : string
323
    {
324
        if ($this->options['auto_regenerate_maxlifetime'] < 1) {
9✔
325
            return '<p>Auto regenerate id is inactive.</p>';
9✔
326
        }
327
        $maxlifetime = (int) $this->options['auto_regenerate_maxlifetime'];
1✔
328
        $regeneratedAt = ArraySimple::value('$[regenerated_at]', $this->session->getAll());
1✔
329
        $nextRegeneration = $regeneratedAt + $maxlifetime;
1✔
330
        \ob_start(); ?>
1✔
331
        <table>
1✔
332
            <thead>
1✔
333
            <tr>
1✔
334
                <th>Maxlifetime</th>
1✔
335
                <th>Destroy</th>
1✔
336
                <th>Regenerated At</th>
1✔
337
                <th>Next Regeneration</th>
1✔
338
            </tr>
1✔
339
            </thead>
1✔
340
            <tbody>
1✔
341
            <tr>
1✔
342
                <td><?= $maxlifetime ?></td>
1✔
343
                <td><?= $this->options['auto_regenerate_destroy'] ? 'Yes' : 'No' ?></td>
1✔
344
                <td><?= $regeneratedAt ? \date('Y-m-d H:i:s', $regeneratedAt) : '' ?></td>
1✔
345
                <td><?= \date('Y-m-d H:i:s', $nextRegeneration) ?></td>
1✔
346
            </tr>
347
            </tbody>
348
        </table>
349
        <?php
350
        return \ob_get_clean(); // @phpstan-ignore-line
1✔
351
    }
352

353
    /**
354
     * @return array<string,mixed>
355
     */
356
    protected function getSaveHandlerConfigs() : array
357
    {
358
        $config = $this->saveHandler->getConfig();
5✔
359
        if ($this->saveHandler instanceof FilesHandler) {
5✔
360
            return [
1✔
361
                'Directory' => $config['directory'],
1✔
362
                'Prefix' => $config['prefix'],
1✔
363
                'Match IP' => $config['match_ip'] ? 'Yes' : 'No',
1✔
364
                'Match User-Agent' => $config['match_ua'] ? 'Yes' : 'No',
1✔
365
            ];
1✔
366
        }
367
        if ($this->saveHandler instanceof MemcachedHandler) {
4✔
368
            $serverList = $this->saveHandler->getMemcached()?->getServerList();
1✔
369
            $config['servers'] = $serverList ?: [];
1✔
370
            $servers = [];
1✔
371
            foreach ($config['servers'] as $server) {
1✔
372
                $servers[] = [
1✔
373
                    'Host' => $server['host'],
1✔
374
                    'Port' => $server['port'] ?? 11211,
1✔
375
                ];
1✔
376
            }
377
            return [
1✔
378
                'Servers' => $servers,
1✔
379
                'Prefix' => $config['prefix'],
1✔
380
                'Lock Attempts' => $config['lock_attempts'],
1✔
381
                'Lock Sleep' => $config['lock_sleep'],
1✔
382
                'Lock TTL' => $config['lock_ttl'],
1✔
383
                'Maxlifetime' => $config['maxlifetime'] ?? \ini_get('session.gc_maxlifetime'),
1✔
384
                'Match IP' => $config['match_ip'] ? 'Yes' : 'No',
1✔
385
                'Match User-Agent' => $config['match_ua'] ? 'Yes' : 'No',
1✔
386
            ];
1✔
387
        }
388
        if ($this->saveHandler instanceof RedisHandler) {
3✔
389
            $redis = $this->saveHandler->getRedis();
1✔
390
            return [
1✔
391
                'Host' => $redis?->getHost() ?: $config['host'],
1✔
392
                'Port' => $redis?->getPort() ?: $config['port'],
1✔
393
                'Timeout' => $redis?->getTimeout() ?: $config['timeout'],
1✔
394
                'Database' => $config['database'],
1✔
395
                'Prefix' => $config['prefix'],
1✔
396
                'Lock Attempts' => $config['lock_attempts'],
1✔
397
                'Lock Sleep' => $config['lock_sleep'],
1✔
398
                'Lock TTL' => $config['lock_ttl'],
1✔
399
                'Maxlifetime' => $config['maxlifetime'] ?? \ini_get('session.gc_maxlifetime'),
1✔
400
                'Match IP' => $config['match_ip'] ? 'Yes' : 'No',
1✔
401
                'Match User-Agent' => $config['match_ua'] ? 'Yes' : 'No',
1✔
402
            ];
1✔
403
        }
404
        if ($this->saveHandler instanceof DatabaseHandler) {
2✔
405
            $databaseConfig = $this->saveHandler->getDatabase()?->getConfig();
1✔
406
            return [
1✔
407
                'Host' => $databaseConfig['host'] ?? '',
1✔
408
                'Schema' => $databaseConfig['schema'] ?? '',
1✔
409
                'Table' => $config['table'],
1✔
410
                'Columns' => [
1✔
411
                    [
1✔
412
                        'id' => $config['columns']['id'],
1✔
413
                        'data' => $config['columns']['data'],
1✔
414
                        'timestamp' => $config['columns']['timestamp'],
1✔
415
                        'ip' => $config['columns']['ip'],
1✔
416
                        'ua' => $config['columns']['ua'],
1✔
417
                        'user_id' => $config['columns']['user_id'],
1✔
418
                    ],
1✔
419
                ],
1✔
420
                'Maxlifetime' => $config['maxlifetime'] ?? \ini_get('session.gc_maxlifetime'),
1✔
421
                'Match IP' => $config['match_ip'] ? 'Yes' : 'No',
1✔
422
                'Match User-Agent' => $config['match_ua'] ? 'Yes' : 'No',
1✔
423
                'Save IP' => $config['save_ip'] ? 'Yes' : 'No',
1✔
424
                'Save User-Agent' => $config['save_ua'] ? 'Yes' : 'No',
1✔
425
                'Save User Id' => $config['save_user_id'] ? 'Yes' : 'No',
1✔
426
            ];
1✔
427
        }
428
        return [];
1✔
429
    }
430
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc