Skip to content

Commit

Permalink
Backporting fix of issue laravel#9251 to 5.1 branch. Original commit …
Browse files Browse the repository at this point in the history
…by Taylor Otwell: d9e0a6a
  • Loading branch information
Raymond Perez committed Jan 30, 2017
1 parent 98257f0 commit 3348e7c
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/Illuminate/Session/DatabaseSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use SessionHandlerInterface;
use Illuminate\Database\QueryException;
use Illuminate\Database\ConnectionInterface;

class DatabaseSessionHandler implements SessionHandlerInterface, ExistenceAwareInterface
Expand Down Expand Up @@ -95,16 +96,45 @@ public function read($sessionId)
public function write($sessionId, $data)
{
if ($this->exists) {
$this->getQuery()->where('id', $sessionId)->update([
'payload' => base64_encode($data), 'last_activity' => time(),
]);
$this->performUpdate($sessionId, $data);
} else {
$this->getQuery()->insert([
$this->performInsert($sessionId, $data);
}

$this->exists = true;
}


/**
* Perform an insert operation on the session ID.
*
* @param string $sessionId
* @param string $data
* @return void
*/
protected function performInsert($sessionId, $data)
{
try {
return $this->getQuery()->insert([
'id' => $sessionId, 'payload' => base64_encode($data), 'last_activity' => time(),
]);
} catch (QueryException $e) {
$this->performUpdate($sessionId, $data);
}
}

$this->exists = true;
/**
* Perform an update operation on the session ID.
*
* @param string $sessionId
* @param string $data
* @return int
*/
protected function performUpdate($sessionId, $data)
{
return $this->getQuery()->where('id', $sessionId)->update([
'payload' => base64_encode($data), 'last_activity' => time(),
]);
}

/**
Expand Down

0 comments on commit 3348e7c

Please sign in to comment.