Skip to content

Commit 6385ad7

Browse files
committed
Added file upload support and symfony4 support
1 parent fa2b748 commit 6385ad7

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

Diff for: Bootstraps/Symfony.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@ public function getApplication()
4949
require './vendor/autoload.php';
5050
}
5151

52+
// environment loading as of Symfony 3.3
53+
if (!getenv('APP_ENV') && class_exists('Symfony\Component\Dotenv\Dotenv') && file_exists(realpath('.env'))) {
54+
(new \Symfony\Component\Dotenv\Dotenv())->load(realpath('.env'));
55+
}
56+
57+
$class = class_exists('\AppKernel') ? '\AppKernel' : '\App\Kernel';
58+
5259
//since we need to change some services, we need to manually change some services
53-
$app = new \AppKernel($this->appenv, $this->debug);
60+
$app = new $class($this->appenv, $this->debug);
61+
5462
// We need to change some services, before the boot, because they would
5563
// otherwise be instantiated and passed to other classes which makes it
5664
// impossible to replace them.
@@ -66,8 +74,7 @@ public function getApplication()
6674
//now we can modify the container
6775
$nativeStorage = new StrongerNativeSessionStorage(
6876
$app->getContainer()->getParameter('session.storage.options'),
69-
$app->getContainer()->has('session.handler') ? $app->getContainer()->get('session.handler'): null,
70-
$app->getContainer()->get('session.storage.metadata_bag')
77+
$app->getContainer()->has('session.handler') ? $app->getContainer()->get('session.handler'): null
7178
);
7279
$app->getContainer()->set('session.storage.native', $nativeStorage);
7380

Diff for: Bridges/HttpKernel.php

+38-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class HttpKernel implements BridgeInterface
3030
*/
3131
protected $bootstrap;
3232

33+
/**
34+
* @var string[]
35+
*/
36+
protected $tempFiles = [];
37+
3338
/**
3439
* Bootstrap an application implementing the HttpKernelInterface.
3540
*
@@ -82,7 +87,8 @@ public function handle(ServerRequestInterface $request)
8287
$syResponse = $this->application->handle($syRequest);
8388
} catch (\Exception $exception) {
8489
// internal server error
85-
$response = new Psr7\Response(500, ['Content-type' => 'text/plain'], $exception->getMessage());
90+
error_log((string)$exception);
91+
$response = new Psr7\Response(500, ['Content-type' => 'text/plain'], 'Unexpected error');
8692

8793
// end buffering if we need to throw
8894
@ob_end_clean();
@@ -142,8 +148,30 @@ protected function mapRequest(ServerRequestInterface $psrRequest)
142148
session_id(Utils::generateSessionId());
143149
}
144150

145-
// files
146-
$files = $psrRequest->getUploadedFiles();
151+
/** @var \React\Http\Io\UploadedFile $file */
152+
$uploadedFiles = $psrRequest->getUploadedFiles();
153+
154+
$mapFiles = function(&$files) use (&$mapFiles) {
155+
foreach ($files as &$value) {
156+
if (is_array($value)) {
157+
$mapFiles($value);
158+
} else if ($value instanceof \React\Http\Io\UploadedFile) {
159+
$tmpname = tempnam(sys_get_temp_dir(), 'upload');
160+
$this->tempFiles[] = $tmpname;
161+
162+
file_put_contents($tmpname, (string)$value->getStream());
163+
$value = new \Symfony\Component\HttpFoundation\File\UploadedFile(
164+
$tmpname,
165+
$value->getClientFilename(),
166+
$value->getClientMediaType(),
167+
$value->getSize(),
168+
$value->getError(),
169+
true
170+
);
171+
}
172+
}
173+
};
174+
$mapFiles($uploadedFiles);
147175

148176
// @todo check howto handle additional headers
149177

@@ -158,7 +186,7 @@ protected function mapRequest(ServerRequestInterface $psrRequest)
158186
}
159187

160188
/** @var SymfonyRequest $syRequest */
161-
$syRequest = new $class($query, $post, $attributes = [], $_COOKIE, $files, $_SERVER, $psrRequest->getBody());
189+
$syRequest = new $class($query, $post, $attributes = [], $_COOKIE, $uploadedFiles, $_SERVER, $psrRequest->getBody());
162190

163191
$syRequest->setMethod($method);
164192

@@ -256,6 +284,12 @@ protected function mapResponse(SymfonyResponse $syResponse)
256284

257285
$psrResponse = $psrResponse->withBody(Psr7\stream_for($content));
258286

287+
foreach ($this->tempFiles as $tmpname) {
288+
if (file_exists($tmpname)) {
289+
unlink($tmpname);
290+
}
291+
}
292+
259293
return $psrResponse;
260294
}
261295

0 commit comments

Comments
 (0)