Skip to content

Commit

Permalink
Make handle xml http request response protected
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen van den Nieuwenhuisen committed Jul 21, 2020
1 parent d002ef5 commit e07a76e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ This file will get merged with CHANGELOG.md when releasing 4.0.0
- `AbstractAdmin::configureActionButtons` method signature has changed
- Moved default buttons from `AbstractAdmin::configureActionButtons` to `AbstractAdmin::getActionButtons`
- `AbstractAdmin::getBatchActions` is now final
- `CRUDController::handleXmlHttpRequestSuccessResponse` method is now protected
- `CRUDController::handleXmlHttpRequestErrorResponse` method is now protected

### Removed
- Removed BC handler for deprecated `view` `_action`
Expand Down
5 changes: 5 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ Method `SimplePager::getResults` is always returning an array

## LockInterface
`LockInterface` extends from `ModelManagerInterface`.

## CRUDController
The following methods changed their visiblity to protected:
* `handleXmlHttpRequestSuccessResponse`
* `handleXmlHttpRequestErrorResponse`
68 changes: 35 additions & 33 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,41 @@ final protected function trans($id, array $parameters = [], $domain = null, $loc
return $this->get('translator')->trans($id, $parameters, $domain, $locale);
}

protected function handleXmlHttpRequestErrorResponse(Request $request, FormInterface $form): ?JsonResponse
{
if (!\in_array('application/json', $request->getAcceptableContentTypes(), true)) {
@trigger_error('In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json`', E_USER_DEPRECATED);

return null;
}

$errors = [];
foreach ($form->getErrors(true) as $error) {
$errors[] = $error->getMessage();
}

return $this->renderJson([
'result' => 'error',
'errors' => $errors,
], Response::HTTP_BAD_REQUEST);
}

/**
* @param object $object
*/
protected function handleXmlHttpRequestSuccessResponse(Request $request, $object): JsonResponse
{
if (!\in_array('application/json', $request->getAcceptableContentTypes(), true)) {
@trigger_error('In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json`', E_USER_DEPRECATED);
}

return $this->renderJson([
'result' => 'ok',
'objectId' => $this->admin->getNormalizedIdentifier($object),
'objectName' => $this->escapeHtml($this->admin->toString($object)),
], Response::HTTP_OK);
}

private function getSelectedTab(Request $request): array
{
return array_filter(['_tab' => $request->request->get('_tab')]);
Expand Down Expand Up @@ -1416,37 +1451,4 @@ private function setFormTheme(FormView $formView, ?array $theme = null): void

$twig->getRuntime(FormRenderer::class)->setTheme($formView, $theme);
}

private function handleXmlHttpRequestErrorResponse(Request $request, FormInterface $form): JsonResponse
{
if (!\in_array('application/json', $request->getAcceptableContentTypes(), true)) {
return $this->renderJson([], Response::HTTP_NOT_ACCEPTABLE);
}

$errors = [];
foreach ($form->getErrors(true) as $error) {
$errors[] = $error->getMessage();
}

return $this->renderJson([
'result' => 'error',
'errors' => $errors,
], Response::HTTP_BAD_REQUEST);
}

/**
* @param object $object
*/
private function handleXmlHttpRequestSuccessResponse(Request $request, $object): JsonResponse
{
if (!\in_array('application/json', $request->getAcceptableContentTypes(), true)) {
return $this->renderJson([], Response::HTTP_NOT_ACCEPTABLE);
}

return $this->renderJson([
'result' => 'ok',
'objectId' => $this->admin->getNormalizedIdentifier($object),
'objectName' => $this->escapeHtml($this->admin->toString($object)),
], Response::HTTP_OK);
}
}

0 comments on commit e07a76e

Please sign in to comment.