PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / http / src / Bridges / HttpDI / SessionExtension.php

SessionExtension.php in Packeta trunk, at deps/nette/http/src/Bridges/HttpDI/SessionExtension.php

81 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 declare (strict_types=1);
8 namespace Packetery\Nette\Bridges\HttpDI;
9
10 use Packetery\Nette;
11 use Packetery\Nette\Http\IResponse;
12 use Packetery\Nette\Schema\Expect;
13 /**
14 * Session extension for Nette DI.
15 */
16 class SessionExtension extends \Packetery\Nette\DI\CompilerExtension
17 {
18 /** @var bool */
19 private $debugMode;
20 /** @var bool */
21 private $cliMode;
22 public function __construct(bool $debugMode = \false, bool $cliMode = \false)
23 {
24 $this->debugMode = $debugMode;
25 $this->cliMode = $cliMode;
26 }
27 public function getConfigSchema() : \Packetery\Nette\Schema\Schema
28 {
29 return Expect::structure(['debugger' => Expect::bool(\false), 'autoStart' => Expect::anyOf('smart', 'always', 'never', \true, \false)->firstIsDefault(), 'expiration' => Expect::string()->dynamic(), 'handler' => Expect::string()->dynamic(), 'readAndClose' => Expect::bool(), 'cookieSamesite' => Expect::anyOf(IResponse::SameSiteLax, IResponse::SameSiteStrict, IResponse::SameSiteNone, \true)->firstIsDefault()])->otherItems('mixed');
30 }
31 public function loadConfiguration()
32 {
33 $builder = $this->getContainerBuilder();
34 $config = $this->config;
35 $session = $builder->addDefinition($this->prefix('session'))->setFactory(\Packetery\Nette\Http\Session::class);
36 if ($config->expiration) {
37 $session->addSetup('setExpiration', [$config->expiration]);
38 }
39 if ($config->handler) {
40 $session->addSetup('setHandler', [$config->handler]);
41 }
42 if (($config->cookieDomain ?? null) === 'domain') {
43 $config->cookieDomain = $builder::literal('$this->getByType(\\Packetery\\Nette\\Http\\IRequest::class)->getUrl()->getDomain(2)');
44 }
45 if (isset($config->cookieSecure)) {
46 \trigger_error("The item 'session › cookieSecure' is deprecated, use 'http › cookieSecure' (it has default value 'auto').", \E_USER_DEPRECATED);
47 unset($config->cookieSecure);
48 }
49 if ($config->cookieSamesite === \true) {
50 \trigger_error("In 'session › cookieSamesite' replace true with 'Lax'.", \E_USER_DEPRECATED);
51 $config->cookieSamesite = IResponse::SameSiteLax;
52 }
53 $this->compiler->addExportedType(\Packetery\Nette\Http\IRequest::class);
54 if ($this->debugMode && $config->debugger) {
55 $session->addSetup('@Tracy\\Bar::addPanel', [new \Packetery\Nette\DI\Definitions\Statement(\Packetery\Nette\Bridges\HttpTracy\SessionPanel::class)]);
56 }
57 $options = (array) $config;
58 unset($options['expiration'], $options['handler'], $options['autoStart'], $options['debugger']);
59 if ($config->autoStart === 'never') {
60 $options['autoStart'] = \false;
61 }
62 if ($config->readAndClose === null) {
63 unset($options['readAndClose']);
64 }
65 if (!empty($options)) {
66 $session->addSetup('setOptions', [$options]);
67 }
68 if ($this->name === 'session') {
69 $builder->addAlias('session', $this->prefix('session'));
70 }
71 if (!$this->cliMode) {
72 $name = $this->prefix('session');
73 if ($config->autoStart === 'smart') {
74 $this->initialization->addBody('$this->getService(?)->autoStart(false);', [$name]);
75 } elseif ($config->autoStart === 'always' || $config->autoStart === \true) {
76 $this->initialization->addBody('$this->getService(?)->start();', [$name]);
77 }
78 }
79 }
80 }
81