diff --git a/Bootstraps/Laravel.php b/Bootstraps/Laravel.php index a0b145c..cade4de 100644 --- a/Bootstraps/Laravel.php +++ b/Bootstraps/Laravel.php @@ -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; } @@ -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; } @@ -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); } } diff --git a/Bridges/HttpKernel.php b/Bridges/HttpKernel.php index 5fcc395..23dabc4 100644 --- a/Bridges/HttpKernel.php +++ b/Bridges/HttpKernel.php @@ -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 @@ -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); diff --git a/Laravel/SessionGuard.php b/Laravel/SessionGuard.php new file mode 100644 index 0000000..df08d01 --- /dev/null +++ b/Laravel/SessionGuard.php @@ -0,0 +1,73 @@ +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; + } +}