Skip to content

Commit

Permalink
Refactor session away from Symfony's interface.
Browse files Browse the repository at this point in the history
Session does not implement Symfony's SessionInterface any longer. That
caused us to have to implement a bunch of logic around "Bags" which we
never actually used in order to just support the interface. Instead
there is a new Illuminate\Contracts\Session\Session contract to define
our own interface for session handling, which allows us to slim the
session store down to what Laravel actually provides instead of having
to satisfy Symfony requirements.

Legacy method changes:

->set() should be changed to ->put()

->getToken() should be ->token()

Any type hints of Symfony session interface should be changed to new
Illuminate\Contracts\Session\Session contract.
  • Loading branch information
taylorotwell committed Dec 23, 2016
1 parent 7103b5b commit 66976ba
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 374 deletions.
10 changes: 5 additions & 5 deletions src/Illuminate/Auth/SessionGuard.php
Expand Up @@ -5,13 +5,13 @@
use RuntimeException;
use Illuminate\Support\Str;
use Illuminate\Http\Response;
use Illuminate\Contracts\Session\Session;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Auth\StatefulGuard;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Contracts\Auth\SupportsBasicAuth;
use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

class SessionGuard implements StatefulGuard, SupportsBasicAuth
Expand Down Expand Up @@ -44,7 +44,7 @@ class SessionGuard implements StatefulGuard, SupportsBasicAuth
/**
* The session used by the guard.
*
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
* @var \Illuminate\Contracts\Session\Session
*/
protected $session;

Expand Down Expand Up @@ -88,13 +88,13 @@ class SessionGuard implements StatefulGuard, SupportsBasicAuth
*
* @param string $name
* @param \Illuminate\Contracts\Auth\UserProvider $provider
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* @param \Illuminate\Contracts\Session\Session $session
* @param \Symfony\Component\HttpFoundation\Request $request
* @return void
*/
public function __construct($name,
UserProvider $provider,
SessionInterface $session,
Session $session,
Request $request = null)
{
$this->name = $name;
Expand Down Expand Up @@ -430,7 +430,7 @@ public function login(AuthenticatableContract $user, $remember = false)
*/
protected function updateSession($id)
{
$this->session->set($this->getName(), $id);
$this->session->put($this->getName(), $id);

$this->session->migrate(true);
}
Expand Down
165 changes: 165 additions & 0 deletions src/Illuminate/Contracts/Session/Session.php
@@ -0,0 +1,165 @@
<?php

namespace Illuminate\Contracts\Session;

interface Session
{
/**
* Get the name of the session.
*
* @return string
*/
public function getName();

/**
* Get the current session ID.
*
* @return string
*/
public function getId();

/**
* Set the session ID.
*
* @param string $id
* @return void
*/
public function setId($id);

/**
* Start the session, reading the data from a handler.
*
* @return bool
*/
public function start();

/**
* Save the session data to storage.
*
* @return bool
*/
public function save();

/**
* Get all of the session data.
*
* @return array
*/
public function all();

/**
* Checks if a key exists.
*
* @param string|array $key
* @return bool
*/
public function exists($key);

/**
* Checks if an a key is present and not null.
*
* @param string|array $key
* @return bool
*/
public function has($key);

/**
* Get an item from the session.
*
* @param string $key
* @param mixed $default
* @return void
*/
public function get($key, $default = null);

/**
* Put a key / value pair or array of key / value pairs in the session.
*
* @param string|array $key
* @param mixed $value
* @return void
*/
public function put($key, $value = null);

/**
* Get the CSRF token value.
*
* @return string
*/
public function token();

/**
* Remove an item from the session, returning its value.
*
* @param string $key
* @return mixed
*/
public function remove($key);

/**
* Remove one or many items from the session.
*
* @param string|array $keys
* @return void
*/
public function forget($keys);

/**
* Remove all of the items from the session.
*
* @return void
*/
public function flush();

/**
* Generate a new session ID for the session.
*
* @param bool $destroy
* @return bool
*/
public function migrate($destroy = false);

/**
* Determine if the session has been started.
*
* @return bool
*/
public function isStarted();

/**
* Get the previous URL from the session.
*
* @return string|null
*/
public function previousUrl();

/**
* Set the "previous" URL in the session.
*
* @param string $url
* @return void
*/
public function setPreviousUrl($url);

/**
* Get the session handler instance.
*
* @return \SessionHandlerInterface
*/
public function getHandler();

/**
* Determine if the session handler needs a request.
*
* @return bool
*/
public function handlerNeedsRequest();

/**
* Set the request on the handler instance.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
public function setRequestOnHandler($request);
}
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Application.php
Expand Up @@ -1094,7 +1094,7 @@ public function registerCoreContainerAliases()
'request' => ['Illuminate\Http\Request', 'Symfony\Component\HttpFoundation\Request'],
'router' => ['Illuminate\Routing\Router', 'Illuminate\Contracts\Routing\Registrar', 'Illuminate\Contracts\Routing\BindingRegistrar'],
'session' => ['Illuminate\Session\SessionManager'],
'session.store' => ['Illuminate\Session\Store', 'Symfony\Component\HttpFoundation\Session\SessionInterface'],
'session.store' => ['Illuminate\Session\Store', 'Illuminate\Contracts\Session\Session'],
'url' => ['Illuminate\Routing\UrlGenerator', 'Illuminate\Contracts\Routing\UrlGenerator'],
'validator' => ['Illuminate\Validation\Factory', 'Illuminate\Contracts\Validation\Factory'],
'view' => ['Illuminate\View\Factory', 'Illuminate\Contracts\View\Factory'],
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/helpers.php
Expand Up @@ -328,7 +328,7 @@ function csrf_token()
$session = app('session');

if (isset($session)) {
return $session->getToken();
return $session->token();
}

throw new RuntimeException('Application session store not set.');
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Http/Request.php
Expand Up @@ -391,6 +391,17 @@ public function session()
return $this->getSession();
}

/**
* Set the session instance on the request.
*
* @param \Illuminate\Contracts\Session\Session $session
* @return void
*/
public function setLaravelSession($session)
{
$this->session = $session;
}

/**
* Get the user making the request.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Session/EncryptedStore.php
Expand Up @@ -42,7 +42,7 @@ protected function prepareForUnserialize($data)
try {
return $this->encrypter->decrypt($data);
} catch (DecryptException $e) {
return json_encode([]);
return serialize([]);
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/Illuminate/Session/Middleware/StartSession.php
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Session\SessionManager;
use Illuminate\Session\SessionInterface;
use Illuminate\Contracts\Session\Session;
use Illuminate\Session\CookieSessionHandler;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -56,7 +56,7 @@ public function handle($request, Closure $next)
if ($this->sessionConfigured()) {
$session = $this->startSession($request);

$request->setSession($session);
$request->setLaravelSession($session);

$this->collectGarbage($session);
}
Expand Down Expand Up @@ -93,7 +93,7 @@ public function terminate($request, $response)
* Start the session for the given request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Session\SessionInterface
* @return \Illuminate\Contracts\Session\Session
*/
protected function startSession(Request $request)
{
Expand All @@ -110,7 +110,7 @@ protected function startSession(Request $request)
* Get the session implementation from the manager.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Session\SessionInterface
* @return \Illuminate\Contracts\Session\Session
*/
public function getSession(Request $request)
{
Expand All @@ -125,7 +125,7 @@ public function getSession(Request $request)
* Store the current URL for the request if necessary.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Session\SessionInterface $session
* @param \Illuminate\Contracts\Session\Session $session
* @return void
*/
protected function storeCurrentUrl(Request $request, $session)
Expand All @@ -138,10 +138,10 @@ protected function storeCurrentUrl(Request $request, $session)
/**
* Remove the garbage from the session if necessary.
*
* @param \Illuminate\Session\SessionInterface $session
* @param \Illuminate\Contracts\Session\Session $session
* @return void
*/
protected function collectGarbage(SessionInterface $session)
protected function collectGarbage(Session $session)
{
$config = $this->manager->getSessionConfig();

Expand All @@ -168,10 +168,10 @@ protected function configHitsLottery(array $config)
* Add the session cookie to the application response.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* @param \Illuminate\Session\SessionInterface $session
* @param \Illuminate\Contracts\Session\Session $session
* @return void
*/
protected function addCookieToResponse(Response $response, SessionInterface $session)
protected function addCookieToResponse(Response $response, Session $session)
{
if ($this->usingCookieSessions()) {
$this->manager->driver()->save();
Expand Down
39 changes: 0 additions & 39 deletions src/Illuminate/Session/SessionInterface.php

This file was deleted.

0 comments on commit 66976ba

Please sign in to comment.