|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webfactory\ShortcodeBundle\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use Psr\Log\NullLogger; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerReference; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Fragment\FragmentHandler; |
|
10
|
|
|
use Thunder\Shortcode\Shortcode\ShortcodeInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Handler for thunderer\Shortcode that embeds the configured renderer for a shortcode. |
|
14
|
|
|
* |
|
15
|
|
|
* The attributes of the shortcode will be passed on as parameters to the controller. |
|
16
|
|
|
*/ |
|
17
|
|
|
class EmbeddedShortcodeHandler |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var FragmentHandler */ |
|
20
|
|
|
private $fragmentHandler; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $controllerName; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $renderer; |
|
27
|
|
|
|
|
28
|
|
|
/** @var LoggerInterface */ |
|
29
|
|
|
private $logger; |
|
30
|
|
|
|
|
31
|
|
|
/** @var RequestStack */ |
|
32
|
|
|
private $requestStack; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param FragmentHandler $fragmentHandler |
|
36
|
|
|
* @param string $controllerName |
|
37
|
|
|
* @param string $renderer |
|
38
|
|
|
* @param LoggerInterface $logger |
|
39
|
|
|
* @param RequestStack $requestStack |
|
40
|
|
|
*/ |
|
41
|
4 |
|
public function __construct( |
|
42
|
|
|
FragmentHandler $fragmentHandler, |
|
43
|
|
|
$controllerName, |
|
44
|
|
|
$renderer, |
|
45
|
|
|
RequestStack $requestStack, |
|
46
|
|
|
LoggerInterface $logger = null |
|
47
|
|
|
) { |
|
48
|
4 |
|
$this->fragmentHandler = $fragmentHandler; |
|
49
|
4 |
|
$this->controllerName = $controllerName; |
|
50
|
4 |
|
$this->renderer = $renderer; |
|
51
|
4 |
|
$this->requestStack = $requestStack; |
|
52
|
4 |
|
$this->logger = $logger ?: new NullLogger(); |
|
53
|
4 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param ShortcodeInterface $shortcode |
|
57
|
|
|
* |
|
58
|
|
|
* @return string|null |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function __invoke(ShortcodeInterface $shortcode) |
|
61
|
|
|
{ |
|
62
|
2 |
|
$this->logger->notice( |
|
63
|
2 |
|
'Request {controllerName} with parameters {parameters} and renderer {renderer} to resolve shortcode {shortcode}, triggered by a request to {url}.', |
|
64
|
|
|
[ |
|
65
|
2 |
|
'controllerName' => $this->controllerName, |
|
66
|
2 |
|
'parameters' => json_encode($shortcode->getParameters()), |
|
67
|
2 |
|
'renderer' => $this->renderer, |
|
68
|
2 |
|
'shortcode' => $shortcode->getName(), |
|
69
|
2 |
|
'url' => $this->requestStack->getMasterRequest()->getRequestUri(), |
|
70
|
|
|
] |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
try { |
|
74
|
2 |
|
return $this->fragmentHandler->render( |
|
75
|
2 |
|
new ControllerReference( |
|
76
|
2 |
|
$this->controllerName, |
|
77
|
2 |
|
array_merge(['request' => $this->requestStack->getCurrentRequest()], $shortcode->getParameters()) |
|
78
|
|
|
), |
|
79
|
2 |
|
$this->renderer |
|
80
|
|
|
); |
|
81
|
|
|
} catch (\InvalidArgumentException $exception) { |
|
82
|
|
|
if ('esi' === $this->renderer) { |
|
83
|
|
|
throw new \InvalidArgumentException( |
|
84
|
|
|
'An InvalidArgumentException occured while trying to render the shortcode ' |
|
85
|
|
|
.$shortcode->getShortcodeText().'. You\'ve probably tried to use the ESI rendering strategy for ' |
|
|
|
|
|
|
86
|
|
|
.'your shortcodes while handling a request that contained non-scalar values as part of URI ' |
|
87
|
|
|
.'attributes. This can happen e.g. when using Param Converters for your original controller ' |
|
88
|
|
|
.'action, as the request (containing the conversion result) is automatically passed to the call of ' |
|
89
|
|
|
.'the shortcode controller to allow context sensitive shortcodes. You could use ' |
|
90
|
|
|
.'Webfactory\ShortcodeBundle\Handler\EmbeddedShortcodeHandler.inline as parent in your ' |
|
91
|
|
|
.'shortcode\'s service defintion, so that the inline instead of ESI rendering strategy will be ' |
|
92
|
|
|
.'used.', |
|
93
|
|
|
0, |
|
94
|
|
|
$exception |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
throw $exception; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|