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

nette / http / 40563854517

27 Oct 2026 06:51PM UTC coverage: 82.002% (-0.09%) from 82.088%
40563854517

push

github

dg
uses #Deprecated wip

1016 of 1239 relevant lines covered (82.0%)

0.82 hits per line

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

78.05
/src/Bridges/HttpDI/SessionExtension.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\Bridges\HttpDI;
9

10
use Nette;
11
use Nette\Http\SameSite;
12
use Nette\Schema\Expect;
13

14

15
/**
16
 * Session extension for Nette DI.
17
 *
18
 * @property object{
19
 *     debugger: bool,
20
 *     autoStart: 'smart'|'always'|'never'|bool,
21
 *     expiration: ?string,
22
 *     handler: ?string,
23
 *     readAndClose: ?bool,
24
 *     cookieSamesite: 'Lax'|'Strict'|'None',
25
 *     cookieDomain?: string,
26
 * } $config
27
 */
28
class SessionExtension extends Nette\DI\CompilerExtension
29
{
30
        public function __construct(
1 ✔
31
                private readonly bool $debugMode = false,
32
                private readonly bool $cliMode = false,
33
        ) {
34
        }
1 ✔
35

36

37
        public function getConfigSchema(): Nette\Schema\Schema
38
        {
39
                return Expect::structure([
1 ✔
40
                        'debugger' => Expect::bool(false),
1 ✔
41
                        'autoStart' => Expect::anyOf('smart', 'always', 'never', true, false)->firstIsDefault(),
1 ✔
42
                        'expiration' => Expect::string()->dynamic(),
1 ✔
43
                        'handler' => Expect::string()->dynamic(),
1 ✔
44
                        'readAndClose' => Expect::bool(),
1 ✔
45
                        'cookieSamesite' => Expect::anyOf(SameSite::Lax->value, SameSite::Strict->value, SameSite::None->value)
1 ✔
46
                                ->firstIsDefault(),
1 ✔
47
                ])->otherItems();
1 ✔
48
        }
49

50

51
        public function loadConfiguration(): void
52
        {
53
                $builder = $this->getContainerBuilder();
1 ✔
54
                $config = $this->config;
1 ✔
55

56
                $session = $builder->addDefinition($this->prefix('session'))
1 ✔
57
                        ->setFactory(Nette\Http\Session::class);
1 ✔
58

59
                if ($config->expiration) {
1 ✔
60
                        $session->addSetup('setExpiration', [$config->expiration]);
×
61
                }
62

63
                if ($config->handler) {
1 ✔
64
                        $session->addSetup('setHandler', [$config->handler]);
1 ✔
65
                }
66

67
                if (($config->cookieDomain ?? null) === 'domain') {
1 ✔
68
                        $config->cookieDomain = $builder::literal('$this->getByType(Nette\Http\IRequest::class)->getUrl()->getDomain(2)');
1 ✔
69
                }
70

71
                $this->compiler->addExportedType(Nette\Http\IRequest::class);
1 ✔
72

73
                if ($this->debugMode && $config->debugger) {
1 ✔
74
                        $session->addSetup('@Tracy\Bar::addPanel', [
×
75
                                new Nette\DI\Definitions\Statement(Nette\Bridges\HttpTracy\SessionPanel::class),
×
76
                        ]);
77
                }
78

79
                $options = (array) $config;
1 ✔
80
                unset($options['expiration'], $options['handler'], $options['autoStart'], $options['debugger']);
1 ✔
81
                if ($config->autoStart === 'never') {
1 ✔
82
                        $options['autoStart'] = false;
×
83
                }
84

85
                if ($config->readAndClose === null) {
1 ✔
86
                        unset($options['readAndClose']);
1 ✔
87
                }
88

89
                if (!empty($options)) {
1 ✔
90
                        $session->addSetup('setOptions', [$options]);
1 ✔
91
                }
92

93
                if (!$this->cliMode) {
1 ✔
94
                        $name = $this->prefix('session');
×
95

96
                        if ($config->autoStart === 'smart') {
×
97
                                $this->initialization->addBody('$this->getService(?)->autoStart(false);', [$name]);
×
98

99
                        } elseif ($config->autoStart === 'always' || $config->autoStart === true) {
×
100
                                $this->initialization->addBody('$this->getService(?)->start();', [$name]);
×
101
                        }
102
                }
103
        }
1 ✔
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc