Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb579b02

Browse files
fabpotnicolas-grekas
authored andcommitted
[HttpKernel] add RealHttpKernel: handle requests with HttpClientInterface
1 parent1479a26 commitb579b02

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

‎src/Symfony/Component/HttpKernel/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ CHANGELOG
2121
* renamed`GetResponseForControllerResultEvent` to`ViewEvent`
2222
* renamed`GetResponseForExceptionEvent` to`ExceptionEvent`
2323
* renamed`PostResponseEvent` to`TerminateEvent`
24+
* added`RealHttpKernel` for handling requests with an`HttpClientInterface` instance
2425

2526
4.2.0
2627
-----
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\HttpKernel;
13+
14+
usePsr\Log\LoggerInterface;
15+
usePsr\Log\NullLogger;
16+
useSymfony\Component\HttpClient\HttpClient;
17+
useSymfony\Component\HttpFoundation\Request;
18+
useSymfony\Component\HttpFoundation\Response;
19+
useSymfony\Component\Mime\Part\AbstractPart;
20+
useSymfony\Component\Mime\Part\DataPart;
21+
useSymfony\Component\Mime\Part\Multipart\FormDataPart;
22+
useSymfony\Component\Mime\Part\TextPart;
23+
useSymfony\Contracts\HttpClient\HttpClientInterface;
24+
25+
/**
26+
* An implementation of a Symfony HTTP kernel using a "real" HTTP client.
27+
*
28+
* @author Fabien Potencier <fabien@symfony.com>
29+
*/
30+
finalclass RealHttpKernelimplements HttpKernelInterface
31+
{
32+
private$client;
33+
private$logger;
34+
35+
publicfunction__construct(HttpClientInterface$client =null,LoggerInterface$logger =null)
36+
{
37+
if (!class_exists(HttpClient::class)) {
38+
thrownew \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".',__CLASS__));
39+
}
40+
41+
$this->client =$client ?? HttpClient::create();
42+
$this->logger =$logger ??newNullLogger();
43+
}
44+
45+
publicfunctionhandle(Request$request,$type = HttpKernelInterface::MASTER_REQUEST,$catch =true)
46+
{
47+
$this->logger->debug(sprintf('Request: %s %s',$request->getMethod(),$request->getUri()));
48+
49+
$headers =$this->getHeaders($request);
50+
$body ='';
51+
if (null !==$part =$this->getBody($request)) {
52+
$headers =array_merge($headers,$part->getPreparedHeaders()->toArray());
53+
$body =$part->bodyToIterable();
54+
}
55+
$response =$this->client->request($request->getMethod(),$request->getUri(), [
56+
'headers' =>$headers,
57+
'body' =>$body,
58+
'max_redirects' =>0,
59+
] +$request->attributes->get('http_client_options', []));
60+
61+
$this->logger->debug(sprintf('Response: %s %s',$response->getStatusCode(),$request->getUri()));
62+
63+
returnnewResponse($response->getContent(!$catch),$response->getStatusCode(),$response->getHeaders(!$catch));
64+
}
65+
66+
privatefunctiongetBody(Request$request): ?AbstractPart
67+
{
68+
if (\in_array($request->getMethod(), ['GET','HEAD'])) {
69+
returnnull;
70+
}
71+
72+
if (!class_exists(AbstractPart::class)) {
73+
thrownew \LogicException('You cannot pass non-empty bodies as the Mime component is not installed. Try running "composer require symfony/mime".');
74+
}
75+
76+
if ($content =$request->getContent()) {
77+
returnnewTextPart($content,'utf-8','plain','8bit');
78+
}
79+
80+
$fields =$request->request->all();
81+
foreach ($request->files->all()as$name =>$file) {
82+
$fields[$name] = DataPart::fromPath($file->getPathname(),$file->getClientOriginalName(),$file->getClientMimeType());
83+
}
84+
85+
returnnewFormDataPart($fields);
86+
}
87+
88+
privatefunctiongetHeaders(Request$request):array
89+
{
90+
$headers = [];
91+
foreach ($request->headersas$key =>$value) {
92+
$headers[$key] =$value;
93+
}
94+
$cookies = [];
95+
foreach ($request->cookies->all()as$name =>$value) {
96+
$cookies[] =$name.'='.$value;
97+
}
98+
if ($cookies) {
99+
$headers['cookie'] =implode(';',$cookies);
100+
}
101+
102+
return$headers;
103+
}
104+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp