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