Skip to content

Handles laravel session #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion Bootstraps/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getApplication()
if (file_exists('bootstrap/start.php')) {
$this->app = require_once 'bootstrap/start.php';
$this->app->boot();

return $this->app;
}

Expand All @@ -80,6 +80,24 @@ public function getApplication()

$kernel = $this->app->make($isLaravel ? 'Illuminate\Contracts\Http\Kernel' : 'Laravel\Lumen\Application');

$this->app->afterResolving('auth', function($auth) {
$auth->extend('session', function($app, $name, $config) {
$provider = $app['auth']->createUserProvider($config['provider']);
$guard = new \PHPPM\Laravel\SessionGuard($name, $provider, $app['session.store'], null, $app);
$guard->setCookieJar($app['cookie']);
$guard->setDispatcher($app['events']);
$guard->setRequest($app->refresh('request', $guard, 'setRequest'));

return $guard;
});
});

$app = $this->app;
$this->app->extend('session.store', function() use ($app) {
$manager = $app['session'];
return $manager->driver();
});

return $kernel;
}

Expand All @@ -97,5 +115,21 @@ public function preHandle($app)
public function postHandle($app)
{
//reset debugbar if available

$this->resetProvider('\Illuminate\Cookie\CookieServiceProvider');
$this->resetProvider('\Illuminate\Session\SessionServiceProvider');
}

/**
* @param string $providerName
*/
protected function resetProvider($providerName)
{
if (!$this->app->getProvider($providerName))
{
return;
}

$this->app->register($providerName, [], true);
}
}
5 changes: 5 additions & 0 deletions Bridges/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\StreamedResponse as SymfonyStreamedResponse;
use Symfony\Component\HttpKernel\TerminableInterface;
use Illuminate\Contracts\Http\Kernel;


class HttpKernel implements BridgeInterface
Expand Down Expand Up @@ -106,6 +107,10 @@ public function handle(ServerRequestInterface $request)
if ($this->application instanceof TerminableInterface) {
$this->application->terminate($syRequest, $syResponse);
}

if ($this->application instanceof Kernel) {
$this->application->terminate($syRequest, $syResponse);
}

if ($this->bootstrap instanceof HooksInterface) {
$this->bootstrap->postHandle($this->application);
Expand Down
73 changes: 73 additions & 0 deletions Laravel/SessionGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace PHPPM\Laravel;

use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Foundation\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class SessionGuard extends \Illuminate\Auth\SessionGuard
{

/**
* App instance
*
* @var mixed|\Illuminate\Foundation\Application $app
*/
protected $app;

/**
* Create a new authentication guard.
*
* @param string $name
* @param \Illuminate\Contracts\Auth\UserProvider $provider
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* @param \Symfony\Component\HttpFoundation\Request $request
* @param mixed|\Illuminate\Foundation\Application $app
* @return void
*/
public function __construct($name,
UserProvider $provider,
SessionInterface $session,
Request $request = null,
Application $app)
{
$this->name = $name;
$this->session = $session;
$this->request = $request;
$this->provider = $provider;
$this->app = $app;
}

/**
* Set the current request instance.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @return $this
*/
public function setRequest(Request $request)
{
// reset the current state
$this->reset();

// retrieve a new session from the app
$this->session = $this->app->make('session');

return parent::setRequest($request);
}

/**
* Reset the state of current class instance.
*
* @return void
*/
protected function reset()
{
$this->user = null;
$this->lastAttempted = null;
$this->viaRemember = false;
$this->loggedOut = false;
$this->tokenRetrievalAttempted = false;
}
}