1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\HttpDI;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class SessionExtension extends Nette\DI\CompilerExtension
17: {
18: public $defaults = array(
19: 'debugger' => FALSE,
20: 'autoStart' => 'smart',
21: 'expiration' => NULL,
22: );
23:
24:
25: private $debugMode;
26:
27:
28: public function __construct($debugMode = FALSE)
29: {
30: $this->debugMode = $debugMode;
31: }
32:
33:
34: public function loadConfiguration()
35: {
36: $container = $this->getContainerBuilder();
37: $config = $this->getConfig() + $this->defaults;
38: $this->setConfig($config);
39:
40: $session = $container->addDefinition($this->prefix('session'))
41: ->setClass('Nette\Http\Session');
42:
43: if ($config['expiration']) {
44: $session->addSetup('setExpiration', array($config['expiration']));
45: }
46:
47: if ($this->debugMode && $config['debugger']) {
48: $session->addSetup('@Tracy\Bar::addPanel', array(
49: new Nette\DI\Statement('Nette\Bridges\HttpTracy\SessionPanel'),
50: ));
51: }
52:
53: unset($config['expiration'], $config['autoStart'], $config['debugger']);
54: if (!empty($config)) {
55: $session->addSetup('setOptions', array($config));
56: }
57:
58: if ($this->name === 'session') {
59: $container->addAlias('session', $this->prefix('session'));
60: }
61: }
62:
63:
64: public function afterCompile(Nette\PhpGenerator\ClassType $class)
65: {
66: if (PHP_SAPI === 'cli') {
67: return;
68: }
69:
70: $initialize = $class->getMethod('initialize');
71: $config = $this->getConfig();
72: $name = $this->prefix('session');
73:
74: if ($config['autoStart'] === 'smart') {
75: $initialize->addBody('$this->getService(?)->exists() && $this->getService(?)->start();', array($name, $name));
76:
77: } elseif ($config['autoStart']) {
78: $initialize->addBody('$this->getService(?)->start();', array($name));
79: }
80: }
81:
82: }
83: