Skip to content

Commit

Permalink
Working on session component.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 23, 2016
1 parent 8d41c27 commit 7103b5b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 50 deletions.
106 changes: 85 additions & 21 deletions src/Illuminate/Session/DatabaseSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Session;

use Carbon\Carbon;
use Illuminate\Support\Arr;
use SessionHandlerInterface;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\ConnectionInterface;
Expand Down Expand Up @@ -85,12 +86,10 @@ public function read($sessionId)
{
$session = (object) $this->getQuery()->find($sessionId);

if (isset($session->last_activity)) {
if ($session->last_activity < Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
$this->exists = true;
if ($this->expired($session)) {
$this->exists = true;

return;
}
return;
}

if (isset($session->payload)) {
Expand All @@ -100,6 +99,18 @@ public function read($sessionId)
}
}

/**
* Determine if the session is expired.
*
* @param StdClass $session
* @return bool
*/
protected function expired($session)
{
return (isset($session->last_activity) &&
$session->last_activity < Carbon::now()->subMinutes($this->minutes)->getTimestamp());
}

/**
* {@inheritdoc}
*/
Expand All @@ -114,14 +125,10 @@ public function write($sessionId, $data)
if ($this->exists) {
$this->getQuery()->where('id', $sessionId)->update($payload);
} else {
$payload['id'] = $sessionId;

$this->getQuery()->insert($payload);
$this->getQuery()->insert(Arr::set($payload, 'id', $sessionId));
}

$this->exists = true;

return true;
return $this->exists = true;
}

/**
Expand All @@ -132,25 +139,82 @@ public function write($sessionId, $data)
*/
protected function getDefaultPayload($data)
{
$payload = ['payload' => base64_encode($data), 'last_activity' => Carbon::now()->getTimestamp()];
$payload = [
'payload' => base64_encode($data),
'last_activity' => Carbon::now()->getTimestamp()
];

if (! $container = $this->container) {
if (! $this->container) {
return $payload;
}

if ($container->bound(Guard::class)) {
$payload['user_id'] = $container->make(Guard::class)->id();
return tap($payload, function ($payload) {
$this->addUserInformation($payload)
->addRequestInformation($payload);
});
}

/**
* Add the user information to the session payload.
*
* @param array $payload
* @return $this
*/
protected function addUserInformation(&$payload)
{
if ($this->container->bound(Guard::class)) {
$payload['user_id'] = $this->userId();
}

if ($container->bound('request')) {
$payload['ip_address'] = $container->make('request')->ip();
return $this;
}

$payload['user_agent'] = substr(
(string) $container->make('request')->header('User-Agent'), 0, 500
);
/**
* Get the currently authenticated user's ID.
*
* @return mixed
*/
protected function userId()
{
return $this->container->make(Guard::class)->id();
}

/**
* Add the request information to the session payload.
*
* @param array $payload
* @return $this
*/
protected function addRequestInformation(&$payload)
{
if ($this->container->bound('request')) {
$payload = array_merge($payload, [
'ip_address' => $this->ipAddress(),
'user_agent' => $this->userAgent(),
]);
}

return $payload;
return $this;
}

/**
* Get the IP address for the current request.
*
* @return string
*/
protected function ipAddress()
{
return $this->container->make('request')->ip();
}

/**
* Get the user agent for the current request.
*
* @return string
*/
protected function userAgent()
{
return substr((string) $this->container->make('request')->header('User-Agent'), 0, 500);
}

/**
Expand Down
56 changes: 30 additions & 26 deletions src/Illuminate/Session/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ protected function createArrayDriver()
*/
protected function createCookieDriver()
{
$lifetime = $this->app['config']['session.lifetime'];

return $this->buildSession(new CookieSessionHandler($this->app['cookie'], $lifetime));
return $this->buildSession(new CookieSessionHandler(
$this->app['cookie'], $this->app['config']['session.lifetime']
));
}

/**
Expand All @@ -57,11 +57,11 @@ protected function createFileDriver()
*/
protected function createNativeDriver()
{
$path = $this->app['config']['session.files'];

$lifetime = $this->app['config']['session.lifetime'];

return $this->buildSession(new FileSessionHandler($this->app['files'], $path, $lifetime));
return $this->buildSession(new FileSessionHandler(
$this->app['files'], $this->app['config']['session.files'], $lifetime
));
}

/**
Expand All @@ -71,13 +71,13 @@ protected function createNativeDriver()
*/
protected function createDatabaseDriver()
{
$connection = $this->getDatabaseConnection();

$table = $this->app['config']['session.table'];

$lifetime = $this->app['config']['session.lifetime'];

return $this->buildSession(new DatabaseSessionHandler($connection, $table, $lifetime, $this->app));
return $this->buildSession(new DatabaseSessionHandler(
$this->getDatabaseConnection(), $table, $lifetime, $this->app
));
}

/**
Expand Down Expand Up @@ -112,16 +112,6 @@ protected function createMemcachedDriver()
return $this->createCacheBased('memcached');
}

/**
* Create an instance of the Wincache session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createWincacheDriver()
{
return $this->createCacheBased('wincache');
}

/**
* Create an instance of the Redis session driver.
*
Expand All @@ -131,7 +121,9 @@ protected function createRedisDriver()
{
$handler = $this->createCacheHandler('redis');

$handler->getCache()->getStore()->setConnection($this->app['config']['session.connection']);
$handler->getCache()->getStore()->setConnection(
$this->app['config']['session.connection']
);

return $this->buildSession($handler);
}
Expand All @@ -157,9 +149,10 @@ protected function createCacheHandler($driver)
{
$store = $this->app['config']->get('session.store') ?: $driver;

$minutes = $this->app['config']['session.lifetime'];

return new CacheBasedSessionHandler(clone $this->app['cache']->store($store), $minutes);
return new CacheBasedSessionHandler(
clone $this->app['cache']->store($store),
$this->app['config']['session.lifetime']
);
}

/**
Expand All @@ -171,14 +164,25 @@ protected function createCacheHandler($driver)
protected function buildSession($handler)
{
if ($this->app['config']['session.encrypt']) {
return new EncryptedStore(
$this->app['config']['session.cookie'], $handler, $this->app['encrypter']
);
return $this->buildEncryptedSession($handler);
} else {
return new Store($this->app['config']['session.cookie'], $handler);
}
}

/**
* Build the encrypted session instance.
*
* @param \SessionHandlerInterface $handler
* @return \Illuminate\Session\EncryptedStore
*/
protected function buildEncryptedSession($handler)
{
return new EncryptedStore(
$this->app['config']['session.cookie'], $handler, $this->app['encrypter']
);
}

/**
* Get the session configuration.
*
Expand Down
4 changes: 1 addition & 3 deletions src/Illuminate/Session/SessionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ protected function registerSessionDriver()
// First, we will create the session manager which is responsible for the
// creation of the various session drivers when they are needed by the
// application instance, and will resolve them on a lazy load basis.
$manager = $app['session'];

return $manager->driver();
return $app->make('session')->driver();
});
}
}

0 comments on commit 7103b5b

Please sign in to comment.