Skip to content

Commit

Permalink
Include a session on the PSR7 request.
Browse files Browse the repository at this point in the history
Having access to the session on the request will be handy in middleware
objects as it can be useful to have authentication data available.

Refs #6960
  • Loading branch information
markstory committed Jun 24, 2016
1 parent 38c903b commit 77c8c7e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Http/ServerRequestFactory.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Http; namespace Cake\Http;


use Cake\Core\Configure; use Cake\Core\Configure;
use Cake\Network\Session;
use Cake\Utility\Hash; use Cake\Utility\Hash;
use Zend\Diactoros\ServerRequestFactory as BaseFactory; use Zend\Diactoros\ServerRequestFactory as BaseFactory;


Expand All @@ -39,8 +40,16 @@ public static function fromGlobals(
) { ) {
$request = parent::fromGlobals($server, $query, $body, $cookies, $files); $request = parent::fromGlobals($server, $query, $body, $cookies, $files);
list($base, $webroot) = static::getBase($request); list($base, $webroot) = static::getBase($request);

$sessionConfig = (array)Configure::read('Session') + [
'defaults' => 'php',
'cookiePath' => $webroot
];
$session = Session::create($sessionConfig);
$request = $request->withAttribute('base', $base) $request = $request->withAttribute('base', $base)
->withAttribute('webroot', $webroot); ->withAttribute('webroot', $webroot)
->withAttribute('session', $session);

if ($base) { if ($base) {
$request = static::updatePath($base, $request); $request = static::updatePath($base, $request);
} }
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Http/RequestTransformerTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public function testToCakeBaseSessionPath()
$psr = ServerRequestFactory::fromGlobals($server); $psr = ServerRequestFactory::fromGlobals($server);
$cake = RequestTransformer::toCake($psr); $cake = RequestTransformer::toCake($psr);


$this->assertEquals('/thisapp', ini_get('session.cookie_path')); $this->assertEquals('/thisapp/', ini_get('session.cookie_path'));
} }


/** /**
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/Http/ServerRequestFactoryTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ public function tearDown()
$_SERVER = $this->server; $_SERVER = $this->server;
} }


/**
* Test fromGlobals includes the session
*
* @return void
*/
public function testFromGlobalsUrlSession()
{
Configure::write('App.base', '/basedir');
$server = [
'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
'PHP_SELF' => '/index.php',
'REQUEST_URI' => '/posts/add',
];
$res = ServerRequestFactory::fromGlobals($server);
$session = $res->getAttribute('session');
$this->assertInstanceOf('Cake\Network\Session', $session);
$this->assertEquals('/basedir/', ini_get('session.cookie_path'), 'Needs trailing / for cookie to work');
}

/** /**
* Test fromGlobals with App.base defined. * Test fromGlobals with App.base defined.
* *
Expand Down

0 comments on commit 77c8c7e

Please sign in to comment.