[CORE][Controller] Switch order for content negotiation: allow events to take precedence. Bring back default JSON response

This commit is contained in:
Hugo Sales 2021-09-06 14:29:07 +01:00
parent bc5ddc52ea
commit e8ae0b74e0
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 20 additions and 15 deletions

View File

@ -37,6 +37,7 @@ use App\Util\Exception\RedirectException;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
@ -112,22 +113,26 @@ class Controller extends AbstractController implements EventSubscriberInterface
// Respond in the most preferred acceptable content type
$accept = $request->getAcceptableContentTypes() ?: ['text/html'];
$format = $request->getFormat($accept[0]);
switch ($format) {
case 'html':
$event->setResponse($this->render($template, $this->vars));
break;
default:
$potential_response = null;
if (Event::handle('ControllerResponseInFormat', [
'route' => $request->get('_route'),
'accept_header' => $accept,
'vars' => $this->vars,
'response' => &$potential_response,
])) {
$potential_response = null;
if (Event::handle('ControllerResponseInFormat', [
'route' => $request->get('_route'),
'accept_header' => $accept,
'vars' => $this->vars,
'response' => &$potential_response,
]) === Event::next) {
switch ($format) {
case 'html':
$event->setResponse($this->render($template, $this->vars));
break;
case 'json':
$event->setResponse(new JsonResponse($response));
break;
default:
throw new ClientException(_m('Unsupported format: {format}', ['format' => $format]), 406); // 406 Not Acceptable
} else {
$event->setResponse($potential_response);
}
} else {
$event->setResponse($potential_response);
}
Event::handle('CleanupModule');

View File

@ -38,7 +38,7 @@ use DateTimeInterface;
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class Note extends Entity
class Note extends Entity implements \JsonSerializable
{
// {{{ Autocode
// @codeCoverageIgnoreStart