forked from GNUsocial/gnu-social
[CORE][Controller] Switch order for content negotiation: allow events to take precedence. Bring back default JSON response
This commit is contained in:
parent
bc5ddc52ea
commit
e8ae0b74e0
@ -37,6 +37,7 @@ use App\Util\Exception\RedirectException;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
@ -112,22 +113,26 @@ class Controller extends AbstractController implements EventSubscriberInterface
|
|||||||
// Respond in the most preferred acceptable content type
|
// Respond in the most preferred acceptable content type
|
||||||
$accept = $request->getAcceptableContentTypes() ?: ['text/html'];
|
$accept = $request->getAcceptableContentTypes() ?: ['text/html'];
|
||||||
$format = $request->getFormat($accept[0]);
|
$format = $request->getFormat($accept[0]);
|
||||||
switch ($format) {
|
|
||||||
case 'html':
|
$potential_response = null;
|
||||||
$event->setResponse($this->render($template, $this->vars));
|
if (Event::handle('ControllerResponseInFormat', [
|
||||||
break;
|
'route' => $request->get('_route'),
|
||||||
default:
|
'accept_header' => $accept,
|
||||||
$potential_response = null;
|
'vars' => $this->vars,
|
||||||
if (Event::handle('ControllerResponseInFormat', [
|
'response' => &$potential_response,
|
||||||
'route' => $request->get('_route'),
|
]) === Event::next) {
|
||||||
'accept_header' => $accept,
|
switch ($format) {
|
||||||
'vars' => $this->vars,
|
case 'html':
|
||||||
'response' => &$potential_response,
|
$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
|
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');
|
Event::handle('CleanupModule');
|
||||||
|
@ -38,7 +38,7 @@ use DateTimeInterface;
|
|||||||
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
||||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
*/
|
*/
|
||||||
class Note extends Entity
|
class Note extends Entity implements \JsonSerializable
|
||||||
{
|
{
|
||||||
// {{{ Autocode
|
// {{{ Autocode
|
||||||
// @codeCoverageIgnoreStart
|
// @codeCoverageIgnoreStart
|
||||||
|
Loading…
Reference in New Issue
Block a user