Skip to content

Commit

Permalink
SessionExtension: added option handler to pass own SessionHandlerInte…
Browse files Browse the repository at this point in the history
…rface (#146)
  • Loading branch information
milo authored and dg committed Nov 23, 2018
1 parent fce11ea commit f1f8dad
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Bridges/HttpDI/SessionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SessionExtension extends Nette\DI\CompilerExtension
'debugger' => false,
'autoStart' => 'smart', // true|false|smart
'expiration' => null,
'handler' => null,
];

/** @var bool */
Expand Down Expand Up @@ -47,6 +48,9 @@ public function loadConfiguration()
if ($config['expiration']) {
$session->addSetup('setExpiration', [$config['expiration']]);
}
if ($config['handler']) {
$session->addSetup('setHandler', [$config['handler']]);
}
if (isset($config['cookieDomain']) && $config['cookieDomain'] === 'domain') {
$config['cookieDomain'] = $builder::literal('$this->getByType(Nette\Http\IRequest::class)->getUrl()->getDomain(2)');
}
Expand All @@ -60,7 +64,7 @@ public function loadConfiguration()
]);
}

unset($config['expiration'], $config['autoStart'], $config['debugger']);
unset($config['expiration'], $config['handler'], $config['autoStart'], $config['debugger']);
if (!empty($config)) {
$session->addSetup('setOptions', [$config]);
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Http.DI/SessionExtension.handler.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* Test: SessionExtension.
*/

use Nette\Bridges\HttpDI\HttpExtension;
use Nette\Bridges\HttpDI\SessionExtension;
use Nette\DI;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class TestHandler extends SessionHandler
{
public $called = false;


public function open($save_path, $session_name)
{
$this->called = true;
return parent::open($save_path, $session_name);
}
}


$compiler = new DI\Compiler;
$compiler->addExtension('foo', new HttpExtension);
$compiler->addExtension('session', new SessionExtension(false, PHP_SAPI === 'cli'));

$loader = new DI\Config\Loader;
$config = $loader->load(Tester\FileMock::create('
session:
handler: @handler
services:
foo.request: Nette\Http\Request(Nette\Http\UrlScript("http://www.nette.org"))
handler: TestHandler
', 'neon'));

eval($compiler->addConfig($config)->compile());

$container = new Container;
$container->getService('session')->start();

Assert::true($container->getService('handler')->called);

0 comments on commit f1f8dad

Please sign in to comment.