|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the SF Forward Bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) DAOUDI Soufian <[email protected]> |
|
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
|
|
|
namespace SfForward\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use SfForward\Manager\ResponseManager; |
|
17
|
|
|
use SfForward\Util\ParamsListService; |
|
18
|
|
|
use SfForward\Util\RequestMethodMapping; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
22
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
23
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class SfForwardController. |
|
27
|
|
|
*/ |
|
28
|
|
|
class SfForwardController extends Controller |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @Route("/sfForwardFront/{paramsList}", name="sf_forward_front") |
|
32
|
|
|
* |
|
33
|
|
|
* @param Request $request |
|
34
|
|
|
* @param string $paramsList |
|
35
|
|
|
* |
|
36
|
|
|
* @return Response |
|
37
|
|
|
*/ |
|
38
|
|
|
public function sfForwardFront(Request $request, $paramsList) |
|
39
|
|
|
{ |
|
40
|
|
|
$params = new ParamsListService($paramsList); |
|
41
|
|
|
$guzzleServiceName = sprintf('eight_points_guzzle.client.%s', $params->getServiceName()); |
|
42
|
|
|
$method = strtolower($request->getMethod()); |
|
43
|
|
|
|
|
44
|
|
|
if (RequestMethodMapping::isValidMethod($method) && $this->has($guzzleServiceName)) { |
|
45
|
|
|
/** @var Client $client */ |
|
46
|
|
|
$client = $this->get($guzzleServiceName); |
|
47
|
|
|
|
|
48
|
|
|
$contentType = RequestMethodMapping::$contentTypes[$method]; |
|
49
|
|
|
$methodName = RequestMethodMapping::$methodsMapping[$method]; |
|
50
|
|
|
|
|
51
|
|
|
/** @var ResponseInterface $guzzleResponse */ |
|
52
|
|
|
$guzzleResponse = $client->{$method}( |
|
53
|
|
|
$params->getRouteId(), |
|
54
|
|
|
[ |
|
55
|
|
|
$contentType => $request->{$methodName}->all(), |
|
56
|
|
|
] |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
return ( |
|
60
|
|
|
new ResponseManager($guzzleResponse, $this->container->getParameter('kernel.project_dir')) |
|
61
|
|
|
)->getResponse(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
throw new NotFoundHttpException(sprintf('Service "%s" not found', $params->getServiceName())); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|