Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • HttpExtension
  • SessionExtension
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
 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: namespace Nette\Bridges\HttpDI;
 9: 
10: use Nette;
11: 
12: 
13: /**
14:  * Session extension for Nette DI.
15:  */
16: class SessionExtension extends Nette\DI\CompilerExtension
17: {
18:     public $defaults = [
19:         'debugger' => false,
20:         'autoStart' => 'smart', // true|false|smart
21:         'expiration' => null,
22:         'handler' => null,
23:     ];
24: 
25:     /** @var bool */
26:     private $debugMode;
27: 
28:     /** @var bool */
29:     private $cliMode;
30: 
31: 
32:     public function __construct($debugMode = false, $cliMode = false)
33:     {
34:         $this->debugMode = $debugMode;
35:         $this->cliMode = $cliMode;
36:     }
37: 
38: 
39:     public function loadConfiguration()
40:     {
41:         $builder = $this->getContainerBuilder();
42:         $config = $this->getConfig() + $this->defaults;
43:         $this->setConfig($config);
44: 
45:         $session = $builder->addDefinition($this->prefix('session'))
46:             ->setFactory(Nette\Http\Session::class);
47: 
48:         if ($config['expiration']) {
49:             $session->addSetup('setExpiration', [$config['expiration']]);
50:         }
51:         if ($config['handler']) {
52:             $session->addSetup('setHandler', [$config['handler']]);
53:         }
54:         if (isset($config['cookieDomain']) && $config['cookieDomain'] === 'domain') {
55:             $config['cookieDomain'] = $builder::literal('$this->getByType(Nette\Http\IRequest::class)->getUrl()->getDomain(2)');
56:         }
57:         if (isset($config['cookieSecure']) && $config['cookieSecure'] === 'auto') {
58:             $config['cookieSecure'] = $builder::literal('$this->getByType(Nette\Http\IRequest::class)->isSecured()');
59:         }
60: 
61:         if ($this->debugMode && $config['debugger']) {
62:             $session->addSetup('@Tracy\Bar::addPanel', [
63:                 new Nette\DI\Statement(Nette\Bridges\HttpTracy\SessionPanel::class),
64:             ]);
65:         }
66: 
67:         unset($config['expiration'], $config['handler'], $config['autoStart'], $config['debugger']);
68:         if (!empty($config)) {
69:             $session->addSetup('setOptions', [$config]);
70:         }
71: 
72:         if ($this->name === 'session') {
73:             $builder->addAlias('session', $this->prefix('session'));
74:         }
75:     }
76: 
77: 
78:     public function afterCompile(Nette\PhpGenerator\ClassType $class)
79:     {
80:         if ($this->cliMode) {
81:             return;
82:         }
83: 
84:         $initialize = $class->getMethod('initialize');
85:         $config = $this->getConfig();
86:         $name = $this->prefix('session');
87: 
88:         if ($config['autoStart'] === 'smart') {
89:             $initialize->addBody('$this->getService(?)->exists() && $this->getService(?)->start();', [$name, $name]);
90: 
91:         } elseif ($config['autoStart']) {
92:             $initialize->addBody('$this->getService(?)->start();', [$name]);
93:         }
94:     }
95: }
96: 
Nette 2.4-20191120 API API documentation generated by ApiGen 2.8.0