PreviewToolbarListener.php (contao-4.9.11) | : | PreviewToolbarListener.php (contao-4.9.12) | ||
---|---|---|---|---|
skipping to change at line 31 | skipping to change at line 31 | |||
use Twig\Error\Error as TwigError; | use Twig\Error\Error as TwigError; | |||
/** | /** | |||
* Injects the back end preview toolbar on any response within the /preview.php | * Injects the back end preview toolbar on any response within the /preview.php | |||
* entry point. | * entry point. | |||
* | * | |||
* The onKernelResponse method must be connected to the kernel.response event. | * The onKernelResponse method must be connected to the kernel.response event. | |||
* | * | |||
* The toolbar is only injected on well-formed HTML with a proper </body> tag, | * The toolbar is only injected on well-formed HTML with a proper </body> tag, | |||
* so is never included in sub-requests or ESI requests. | * so is never included in sub-requests or ESI requests. | |||
* | ||||
* @internal | ||||
*/ | */ | |||
class PreviewToolbarListener | class PreviewToolbarListener | |||
{ | { | |||
/** | /** | |||
* @var ScopeMatcher | * @var ScopeMatcher | |||
*/ | */ | |||
private $scopeMatcher; | private $scopeMatcher; | |||
/** | /** | |||
* @var string | ||||
*/ | ||||
private $previewScript; | ||||
/** | ||||
* @var TwigEnvironment | * @var TwigEnvironment | |||
*/ | */ | |||
private $twig; | private $twig; | |||
/** | /** | |||
* @var RouterInterface | * @var RouterInterface | |||
*/ | */ | |||
private $router; | private $router; | |||
public function __construct(string $previewScript, ScopeMatcher $scopeMatche r, TwigEnvironment $twig, RouterInterface $router) | public function __construct(ScopeMatcher $scopeMatcher, TwigEnvironment $twi g, RouterInterface $router) | |||
{ | { | |||
$this->previewScript = $previewScript; | ||||
$this->scopeMatcher = $scopeMatcher; | $this->scopeMatcher = $scopeMatcher; | |||
$this->twig = $twig; | $this->twig = $twig; | |||
$this->router = $router; | $this->router = $router; | |||
} | } | |||
public function __invoke(ResponseEvent $event): void | public function __invoke(ResponseEvent $event): void | |||
{ | { | |||
if (!$this->scopeMatcher->isFrontendMasterRequest($event)) { | if ($this->scopeMatcher->isBackendMasterRequest($event)) { | |||
return; | return; | |||
} | } | |||
$request = $event->getRequest(); | $request = $event->getRequest(); | |||
if ('' === $this->previewScript || $request->getScriptName() !== $this-> | ||||
previewScript) { | ||||
return; | ||||
} | ||||
$response = $event->getResponse(); | $response = $event->getResponse(); | |||
// Do not capture redirects, errors, or modify XML HTTP Requests | // Do not capture redirects, errors, or modify XML HTTP Requests | |||
if ($request->isXmlHttpRequest() || !($response->isSuccessful() || $resp | if ( | |||
onse->isClientError())) { | !$request->attributes->get('_preview', false) | |||
|| $request->isXmlHttpRequest() | ||||
|| !($response->isSuccessful() || $response->isClientError()) | ||||
) { | ||||
return; | return; | |||
} | } | |||
// Only inject the toolbar into HTML responses | // Only inject the toolbar into HTML responses | |||
if ( | if ( | |||
'html' !== $request->getRequestFormat() | 'html' !== $request->getRequestFormat() | |||
|| false === strpos((string) $response->headers->get('Content-Type') , 'text/html') | || false === strpos((string) $response->headers->get('Content-Type') , 'text/html') | |||
|| false !== stripos((string) $response->headers->get('Content-Dispo sition'), 'attachment;') | || false !== stripos((string) $response->headers->get('Content-Dispo sition'), 'attachment;') | |||
) { | ) { | |||
return; | return; | |||
End of changes. 7 change blocks. | ||||
16 lines changed or deleted | 9 lines changed or added |