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

nette / http / 4455935658

pending completion
4455935658

push

github

David Grudl
SessionExtension: don't set readAndClose if null (#213)

3 of 3 new or added lines in 2 files covered. (100.0%)

905 of 1094 relevant lines covered (82.72%)

0.83 hits per line

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

74.0
/src/Bridges/HttpDI/SessionExtension.php
1
<?php
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
declare(strict_types=1);
9

10
namespace Nette\Bridges\HttpDI;
11

12
use Nette;
13
use Nette\Http\IResponse;
14
use Nette\Schema\Expect;
15

16

17
/**
18
 * Session extension for Nette DI.
19
 */
20
class SessionExtension extends Nette\DI\CompilerExtension
21
{
22
        /** @var bool */
23
        private $debugMode;
24

25
        /** @var bool */
26
        private $cliMode;
27

28

29
        public function __construct(bool $debugMode = false, bool $cliMode = false)
1✔
30
        {
31
                $this->debugMode = $debugMode;
1✔
32
                $this->cliMode = $cliMode;
1✔
33
        }
1✔
34

35

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

49

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

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

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

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

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

70
                if (isset($config->cookieSecure)) {
1✔
71
                        trigger_error("The item 'session\u{a0}›\u{a0}cookieSecure' is deprecated, use 'http\u{a0}›\u{a0}cookieSecure' (it has default value 'auto').", E_USER_DEPRECATED);
×
72
                        unset($config->cookieSecure);
×
73
                }
74

75
                if ($config->cookieSamesite === true) {
1✔
76
                        trigger_error("In 'session\u{a0}›\u{a0}cookieSamesite' replace true with 'Lax'.", E_USER_DEPRECATED);
×
77
                        $config->cookieSamesite = IResponse::SAME_SITE_LAX;
×
78
                }
79

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

82
                if ($this->debugMode && $config->debugger) {
1✔
83
                        $session->addSetup('@Tracy\Bar::addPanel', [
×
84
                                new Nette\DI\Definitions\Statement(Nette\Bridges\HttpTracy\SessionPanel::class),
×
85
                        ]);
86
                }
87

88
                $options = (array) $config;
1✔
89
                unset($options['expiration'], $options['handler'], $options['autoStart'], $options['debugger']);
1✔
90
                if ($config->autoStart === 'never') {
1✔
91
                        $options['autoStart'] = false;
×
92
                }
93

94
                if ($config->readAndClose === null) {
1✔
95
                        unset($options['readAndClose']);
1✔
96
                }
97

98
                if (!empty($options)) {
1✔
99
                        $session->addSetup('setOptions', [$options]);
1✔
100
                }
101

102
                if ($this->name === 'session') {
1✔
103
                        $builder->addAlias('session', $this->prefix('session'));
1✔
104
                }
105

106
                if (!$this->cliMode) {
1✔
107
                        $name = $this->prefix('session');
×
108

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

112
                        } elseif ($config->autoStart === 'always' || $config->autoStart === true) {
×
113
                                $this->initialization->addBody('$this->getService(?)->start();', [$name]);
×
114
                        }
115
                }
116
        }
1✔
117
}
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

© 2026 Coveralls, Inc