| 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 |
|