1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\CoreBundle\Fragment; |
16
|
|
|
|
17
|
|
|
use SWP\Bundle\BridgeBundle\Client\GuzzleClient; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
22
|
|
|
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface; |
23
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
24
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class ExternalFragmentRenderer. |
28
|
|
|
* |
29
|
|
|
* Render content fetched from external uri with guzzle. |
30
|
|
|
*/ |
31
|
|
|
class ExternalFragmentRenderer implements FragmentRendererInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var HttpKernelInterface |
35
|
|
|
*/ |
36
|
|
|
private $kernel; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var EventDispatcherInterface |
40
|
|
|
*/ |
41
|
|
|
private $dispatcher; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param HttpKernelInterface $kernel A HttpKernelInterface instance |
47
|
|
|
* @param EventDispatcherInterface $dispatcher A EventDispatcherInterface instance |
48
|
|
|
*/ |
49
|
|
|
public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null) |
50
|
|
|
{ |
51
|
|
|
$this->kernel = $kernel; |
52
|
|
|
$this->dispatcher = $dispatcher; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function render($uri, Request $request, array $options = []) |
59
|
|
|
{ |
60
|
|
|
$level = ob_get_level(); |
61
|
|
|
|
62
|
|
|
try { |
63
|
|
|
return new Response($this->createExternalRequest($uri)); |
|
|
|
|
64
|
|
|
} catch (\Throwable $e) { |
65
|
|
|
// we dispatch the exception event to trigger the logging |
66
|
|
|
// the response that comes back is simply ignored |
67
|
|
|
if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) { |
68
|
|
|
$event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// let's clean up the output buffers that were created by the sub-request |
74
|
|
|
Response::closeOutputBuffers($level, false); |
75
|
|
|
|
76
|
|
|
if (isset($options['alt'])) { |
77
|
|
|
$alt = $options['alt']; |
78
|
|
|
unset($options['alt']); |
79
|
|
|
|
80
|
|
|
return $this->render($alt, $request, $options); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!isset($options['ignore_errors']) || !$options['ignore_errors']) { |
84
|
|
|
throw $e; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new Response(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function getName() |
95
|
|
|
{ |
96
|
|
|
return 'external'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $uri |
101
|
|
|
* |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
private function createExternalRequest(string $uri) |
105
|
|
|
{ |
106
|
|
|
$client = new GuzzleClient(); |
107
|
|
|
|
108
|
|
|
return $client->makeCall($uri)['body']; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.