Skip to content

Commit

Permalink
Exclude json response from shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
lopes-vincent authored Aug 9, 2022
1 parent ea43e0a commit 49c8776
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions EventListener/ResponseListener.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ShortCode\EventListener;

use Maiorano\Shortcodes\Library\SimpleShortcode;
Expand All @@ -10,6 +20,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
Expand All @@ -21,9 +32,6 @@ class ResponseListener implements EventSubscriberInterface
/** @var EventDispatcherInterface */
protected $eventDispatcher;

/**
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(
EventDispatcherInterface $eventDispatcher
) {
Expand All @@ -33,19 +41,24 @@ public function __construct(
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => [['dispatchShortCodeEvents', 64]]
KernelEvents::RESPONSE => [['dispatchShortCodeEvents', 64]],
];
}

public function dispatchShortCodeEvents(ResponseEvent $event)
public function dispatchShortCodeEvents(ResponseEvent $event): void
{
if ($event->getRequest()->get('disable_shortcode', 0) == 1) {
return;
}

$response = $event->getResponse();

if ($response instanceof BinaryFileResponse || $response instanceof StreamedResponse || $response instanceof RedirectResponse) {
if (
$response instanceof BinaryFileResponse
|| $response instanceof StreamedResponse
|| $response instanceof RedirectResponse
|| $response instanceof JsonResponse
) {
return;
}

Expand All @@ -62,6 +75,7 @@ public function dispatchShortCodeEvents(ResponseEvent $event)
$simpleShortCodes[$shortCode->getTag()] = new SimpleShortcode($shortCode->getTag(), null, function ($content, $attributes) use ($shortCode, $dispatcher) {
$shortCodeEvent = new ShortCodeEvent($content, $attributes);
$dispatcher->dispatch($shortCodeEvent, $shortCode->getEvent());

return $shortCodeEvent->getResult();
});
}
Expand Down

0 comments on commit 49c8776

Please sign in to comment.