vendor/symfony/ux-autocomplete/src/Controller/EntityAutocompleteController.php line 41

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\UX\Autocomplete\Controller;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\UX\Autocomplete\AutocompleteResultsExecutor;
  16. use Symfony\UX\Autocomplete\AutocompleterRegistry;
  17. /**
  18.  * @author Ryan Weaver <ryan@symfonycasts.com>
  19.  *
  20.  * @experimental
  21.  */
  22. final class EntityAutocompleteController
  23. {
  24.     public function __construct(
  25.         private AutocompleterRegistry $autocompleteFieldRegistry,
  26.         private AutocompleteResultsExecutor $autocompleteResultsExecutor
  27.     ) {
  28.     }
  29.     public function __invoke(string $aliasRequest $request): Response
  30.     {
  31.         $autocompleter $this->autocompleteFieldRegistry->getAutocompleter($alias);
  32.         if (!$autocompleter) {
  33.             throw new NotFoundHttpException(sprintf('No autocompleter found for "%s". Available autocompleters are: (%s)'$aliasimplode(', '$this->autocompleteFieldRegistry->getAutocompleterNames())));
  34.         }
  35.         $results $this->autocompleteResultsExecutor->fetchResults($autocompleter$request->query->get('query'''));
  36.         return new JsonResponse([
  37.             'results' => $results,
  38.         ]);
  39.     }
  40. }