src/Controller/TourController.php line 137

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Category;
  4. use App\Entity\Tour;
  5. use App\Entity\POI;
  6. use App\Form\TourType;
  7. use App\Repository\TourRepository;
  8. use JMS\Serializer\Context;
  9. use JMS\Serializer\Expression\ExpressionEvaluator;
  10. use JMS\Serializer\Handler\HandlerRegistry;
  11. use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
  12. use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
  13. use JMS\Serializer\Ordering\IdenticalPropertyOrderingStrategy;
  14. use JMS\Serializer\SerializationContext;
  15. use JMS\Serializer\SerializerBuilder;
  16. use JMS\Serializer\SerializerInterface;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Doctrine\ORM\EntityManagerInterface;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  26. use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
  27. use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
  28. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  29. use Symfony\Component\Serializer\Serializer;
  30. use App\Serializer\IdOnlyRelationNormalizer;
  31. #[Route('/tour')]
  32. class TourController extends AbstractController
  33. {
  34.     /*
  35.      * @var SerializerInterface
  36.      */
  37.     private SerializerInterface $serializer;
  38.     /**
  39.      * @param SerializerInterface $serializer
  40.      */
  41.     public function __construct(SerializerInterface $serializer)
  42.     {
  43.         $this->serializer SerializerBuilder::create()
  44.             ->setPropertyNamingStrategy(
  45.                 new SerializedNameAnnotationStrategy(
  46.                     new IdenticalPropertyNamingStrategy()
  47.                 )
  48.             )->addDefaultHandlers()
  49.             ->setExpressionEvaluator(new ExpressionEvaluator(new ExpressionLanguage()))
  50.             ->build();
  51.     }
  52.     #[Route('/'name'app_tour_index'methods: ['GET'])]
  53.     public function index(TourRepository $tourRepository): Response
  54.     {
  55.         return $this->render('tour/index.html.twig', [
  56.             'tours' => $tourRepository->findAll(),
  57.         ]);
  58.     }
  59.     #[Route('/new'name'app_tour_new'methods: ['GET''POST'])]
  60.     public function new(Request $requestTourRepository $tourRepository): Response
  61.     {
  62.         $tour = new Tour();
  63.         $form $this->createForm(TourType::class, $tour);
  64.         $form->handleRequest($request);
  65.         if ($form->isSubmitted() && $form->isValid()) {
  66.             $tourRepository->save($tourtrue);
  67.             return $this->redirectToRoute('app_tour_index', [], Response::HTTP_SEE_OTHER);
  68.         }
  69.         return $this->renderForm('tour/new.html.twig', [
  70.             'tour' => $tour,
  71.             'form' => $form,
  72.         ]);
  73.     }
  74.     #[Route('/{id}'name'app_tour_show'methods: ['GET'])]
  75.     public function show(Tour $tour): Response
  76.     {
  77.         return $this->render('tour/show.html.twig', [
  78.             'tour' => $tour,
  79.         ]);
  80.     }
  81.     #[Route('/{id}/edit'name'app_tour_edit'methods: ['GET''POST'])]
  82.     public function edit(Request $requestTour $tourTourRepository $tourRepository): Response
  83.     {
  84.         $form $this->createForm(TourType::class, $tour);
  85.         $form->handleRequest($request);
  86.         if ($form->isSubmitted() && $form->isValid()) {
  87.             $tourRepository->save($tourtrue);
  88.             return $this->redirectToRoute('app_tour_index', [], Response::HTTP_SEE_OTHER);
  89.         }
  90.         return $this->renderForm('tour/edit.html.twig', [
  91.             'tour' => $tour,
  92.             'form' => $form,
  93.         ]);
  94.     }
  95.     #[Route('/{id}'name'app_tour_delete'methods: ['POST'])]
  96.     public function delete(Request $requestTour $tourTourRepository $tourRepository): Response
  97.     {
  98.         if ($this->isCsrfTokenValid('delete' $tour->getId(), $request->request->get('_token'))) {
  99.             $tourRepository->remove($tourtrue);
  100.         }
  101.         return $this->redirectToRoute('app_tour_index', [], Response::HTTP_SEE_OTHER);
  102.     }
  103.     #[Route('/api/all')]
  104.     public function getAll(EntityManagerInterface $entityManager): Response
  105.     {
  106.         $tours $entityManager->getRepository(Tour::class)->findAll();
  107.         $categories $entityManager->getRepository(Category::class)->findAll();
  108.         $pois $entityManager->getRepository(POI::class)->findAll();
  109.         $merged_entity_data $this->serializeEntityData($pois$tours$categories);
  110.         $response JsonResponse::fromJsonString($merged_entity_data);
  111.         return $response;
  112.     }
  113.     #[Route('/api/live')]
  114.     public function getLive(EntityManagerInterface $entityManager): Response
  115.     {
  116.         $tours $entityManager->getRepository(Tour::class)->findBy(array('live' => true));
  117.         $categories $entityManager->getRepository(Category::class)->findBy(array('live' => true));
  118.         $pois $entityManager->getRepository(POI::class)->findBy(array('live' => true));
  119.         $merged_entity_data $this->serializeEntityData($pois$tours$categories);
  120.         $response = new JsonResponse($merged_entity_data200, [], false);
  121.         return $response;
  122.     }
  123.     #[Route('/api/beta')]
  124.     public function getBeta(EntityManagerInterface $entityManager): Response
  125.     {
  126.         $tours $entityManager->getRepository(Tour::class)->findBy(array('live' => false));
  127.         $categories $entityManager->getRepository(Category::class)->findBy(array('live' => false));
  128.         $pois $entityManager->getRepository(POI::class)->findBy(array('live' => false));
  129.         $merged_entity_data $this->serializeEntityData($pois$tours$categories);
  130.         $response = new JsonResponse($merged_entity_data200, [], false);
  131.         return $response;
  132.     }
  133.     public function serializeEntityData($pois$tours$categories)
  134.     {
  135.         $contextTours SerializationContext::create()->enableMaxDepthChecks()->setGroups("tour");
  136.         $contextPois SerializationContext::create()->enableMaxDepthChecks()->setGroups("poi");
  137.         $contextCategories SerializationContext::create()->enableMaxDepthChecks()->setGroups("category");
  138.         $data = [   ['categories' => $this->serializer->toArray($categories$contextCategories)],
  139.             ['tours' => $this->serializer->toArray($tours$contextTours)],
  140.             ['pois' => $this->serializer->toArray($pois$contextPois)],
  141.         ];
  142.         return $data;
  143.     }
  144.     private function normalizeEntities($serializer$entities$relations)
  145.     {
  146.         $data array_map(function ($entity) use ($serializer$relations) {
  147.             $normalizedEntity json_decode($serializer->serialize($entity'json'), true);
  148.             foreach ($relations as $relation) {
  149.                 if (isset($normalizedEntity[$relation])) {
  150.                     $normalizedEntity[$relation] = array_map(function ($relatedEntity) {
  151.                         return $relatedEntity['id'];
  152.                     }, $normalizedEntity[$relation]);
  153.                 }
  154.             }
  155.             return $normalizedEntity;
  156.         }, $entities);
  157.         return $data;
  158.     }
  159.     #[Route('/api/{id}')]
  160.     public function getTour(EntityManagerInterface $entityManager$id): Response
  161.     {
  162.         $tours $entityManager->getRepository(Tour::class)->find($id);
  163.         return $this->json($tours);
  164.     }
  165. }