Skip to content

Commit a790198

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # Bootstraps/Symfony.php
2 parents 07f7919 + f59e27d commit a790198

File tree

4 files changed

+141
-14
lines changed

4 files changed

+141
-14
lines changed

Diff for: Bootstraps/Laravel.php

+37
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public function getApplication()
6969
// Laravel 4
7070
if (file_exists('bootstrap/start.php')) {
7171
$this->app = require_once 'bootstrap/start.php';
72+
$this->app->boot();
73+
74+
return $this->app;
7275
}
7376

7477
if (!$this->app) {
@@ -77,6 +80,24 @@ public function getApplication()
7780

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

83+
$this->app->afterResolving('auth', function($auth) {
84+
$auth->extend('session', function($app, $name, $config) {
85+
$provider = $app['auth']->createUserProvider($config['provider']);
86+
$guard = new \PHPPM\Laravel\SessionGuard($name, $provider, $app['session.store'], null, $app);
87+
$guard->setCookieJar($app['cookie']);
88+
$guard->setDispatcher($app['events']);
89+
$guard->setRequest($app->refresh('request', $guard, 'setRequest'));
90+
91+
return $guard;
92+
});
93+
});
94+
95+
$app = $this->app;
96+
$this->app->extend('session.store', function() use ($app) {
97+
$manager = $app['session'];
98+
return $manager->driver();
99+
});
100+
80101
return $kernel;
81102
}
82103

@@ -94,5 +115,21 @@ public function preHandle($app)
94115
public function postHandle($app)
95116
{
96117
//reset debugbar if available
118+
119+
$this->resetProvider('\Illuminate\Cookie\CookieServiceProvider');
120+
$this->resetProvider('\Illuminate\Session\SessionServiceProvider');
121+
}
122+
123+
/**
124+
* @param string $providerName
125+
*/
126+
protected function resetProvider($providerName)
127+
{
128+
if (!$this->app->getProvider($providerName))
129+
{
130+
return;
131+
}
132+
133+
$this->app->register($providerName, [], true);
97134
}
98135
}

Diff for: Bootstraps/Symfony.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function postHandle($app)
163163
if ($container->has('logger')) {
164164
$logger = $container->get('logger');
165165
Utils::bindAndCall(function () {
166-
if ($debugLogger = $this->getDebugLogger()) {
166+
if (\method_exists($this, 'getDebugLogger') && $debugLogger = $this->getDebugLogger()) {
167167
//DebugLogger
168168
Utils::hijackProperty($debugLogger, 'records', []);
169169
}

Diff for: Bridges/HttpKernel.php

+30-13
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
use PHPPM\Bootstraps\RequestClassProviderInterface;
99
use Psr\Http\Message\ServerRequestInterface;
1010
use Psr\Http\Message\ResponseInterface;
11+
use Psr\Http\Message\UploadedFileInterface;
1112
use RingCentral\Psr7;
1213
use Symfony\Component\HttpFoundation\Cookie;
14+
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyFile;
1315
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
1416
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
1517
use Symfony\Component\HttpFoundation\StreamedResponse as SymfonyStreamedResponse;
1618
use Symfony\Component\HttpKernel\TerminableInterface;
19+
use Illuminate\Contracts\Http\Kernel;
20+
1721

1822
class HttpKernel implements BridgeInterface
1923
{
@@ -100,6 +104,10 @@ public function handle(ServerRequestInterface $request)
100104
if ($this->application instanceof TerminableInterface) {
101105
$this->application->terminate($syRequest, $syResponse);
102106
}
107+
108+
if ($this->application instanceof Kernel) {
109+
$this->application->terminate($syRequest, $syResponse);
110+
}
103111

104112
if ($this->bootstrap instanceof HooksInterface) {
105113
$this->bootstrap->postHandle($this->application);
@@ -127,6 +135,9 @@ protected function mapRequest(ServerRequestInterface $psrRequest)
127135
$cookies = explode(';', $cookieHeader);
128136

129137
foreach ($cookies as $cookie) {
138+
if (strpos($cookie, '=') == false) {
139+
continue;
140+
}
130141
list($name, $value) = explode('=', trim($cookie));
131142
$_COOKIE[$name] = $value;
132143

@@ -140,22 +151,28 @@ protected function mapRequest(ServerRequestInterface $psrRequest)
140151
$uploadedFiles = $psrRequest->getUploadedFiles();
141152

142153
$mapFiles = function(&$files) use (&$mapFiles) {
143-
foreach ($files as &$value) {
144-
if (is_array($value)) {
145-
$mapFiles($value);
146-
} else if ($value instanceof \React\Http\Io\UploadedFile) {
154+
foreach ($files as &$file) {
155+
if (is_array($file)) {
156+
$mapFiles($file);
157+
} else if ($file instanceof UploadedFileInterface) {
147158
$tmpname = tempnam(sys_get_temp_dir(), 'upload');
148159
$this->tempFiles[] = $tmpname;
149160

150-
file_put_contents($tmpname, (string)$value->getStream());
151-
$value = new \Symfony\Component\HttpFoundation\File\UploadedFile(
152-
$tmpname,
153-
$value->getClientFilename(),
154-
$value->getClientMediaType(),
155-
$value->getSize(),
156-
$value->getError(),
157-
true
158-
);
161+
if (UPLOAD_ERR_NO_FILE == $file->getError()) {
162+
$file = null;
163+
} else {
164+
if (UPLOAD_ERR_OK == $file->getError()) {
165+
file_put_contents($tmpname, (string)$file->getStream());
166+
}
167+
$file = new SymfonyFile(
168+
$tmpname,
169+
$file->getClientFilename(),
170+
$file->getClientMediaType(),
171+
$file->getSize(),
172+
$file->getError(),
173+
true
174+
);
175+
}
159176
}
160177
}
161178
};

Diff for: Laravel/SessionGuard.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace PHPPM\Laravel;
4+
5+
use Illuminate\Contracts\Auth\UserProvider;
6+
use Illuminate\Foundation\Application;
7+
use Symfony\Component\HttpFoundation\Request;
8+
use Illuminate\Contracts\Session\Session;
9+
10+
class SessionGuard extends \Illuminate\Auth\SessionGuard
11+
{
12+
13+
/**
14+
* App instance
15+
*
16+
* @var mixed|\Illuminate\Foundation\Application $app
17+
*/
18+
protected $app;
19+
20+
/**
21+
* Create a new authentication guard.
22+
*
23+
* @param string $name
24+
* @param \Illuminate\Contracts\Auth\UserProvider $provider
25+
* @param \Illuminate\Contracts\Session\Session $session
26+
* @param \Symfony\Component\HttpFoundation\Request $request
27+
* @param mixed|\Illuminate\Foundation\Application $app
28+
* @return void
29+
*/
30+
public function __construct($name,
31+
UserProvider $provider,
32+
Session $session,
33+
Request $request = null,
34+
Application $app)
35+
{
36+
$this->name = $name;
37+
$this->session = $session;
38+
$this->request = $request;
39+
$this->provider = $provider;
40+
$this->app = $app;
41+
}
42+
43+
/**
44+
* Set the current request instance.
45+
*
46+
* @param \Symfony\Component\HttpFoundation\Request $request
47+
* @return $this
48+
*/
49+
public function setRequest(Request $request)
50+
{
51+
// reset the current state
52+
$this->reset();
53+
54+
// retrieve a new session from the app
55+
$this->session = $this->app->make('session');
56+
57+
return parent::setRequest($request);
58+
}
59+
60+
/**
61+
* Reset the state of current class instance.
62+
*
63+
* @return void
64+
*/
65+
protected function reset()
66+
{
67+
$this->user = null;
68+
$this->lastAttempted = null;
69+
$this->viaRemember = false;
70+
$this->loggedOut = false;
71+
$this->tokenRetrievalAttempted = false;
72+
}
73+
}

0 commit comments

Comments
 (0)