src/Core/Controller/AbstractCoreController.php line 116

  1. <?php
  2. namespace App\Core\Controller;
  3. use App\Core\Contracts\TokenGeneratorInterface;
  4. use App\Core\EventListener\AppEvent;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. /**
  19.  * The core controller class.
  20.  *
  21.  * All controllers classes must extends this class
  22.  *
  23.  * @see       http://github.com/kdms90
  24.  * @since      3.0.0
  25.  *
  26.  * @author     Marius Stanislas KEMAYOU DJEUMENI <kdms1990@gmail.com>
  27.  */
  28. abstract class AbstractCoreController extends AbstractController
  29. {
  30.     protected EntityManagerInterface $em;
  31.     protected TranslatorInterface $text;
  32.     protected SessionInterface $session;
  33.     /**
  34.      * Availables locales defined in parameters file.
  35.      *
  36.      * @var array<string>
  37.      */
  38.     protected array $locales;
  39.     protected ?Request $request;
  40.     /**
  41.      * Check if is an ajax request.
  42.      */
  43.     protected bool $isAjaxRequest;
  44.     /**
  45.      * Path to web folder.
  46.      */
  47.     protected string $webFolder;
  48.     /**
  49.      * Define number of items to show in one page.
  50.      */
  51.     protected int $nbItemsPerPage 10;
  52.     protected EventDispatcherInterface $eventDispatcher;
  53.     /** Current actor ID */
  54.     protected int $actor_id;
  55.     /** Détermine sous quelle forme d'intervenant l'utilisateur travaille dans la compagnie actuelle */
  56.     protected string $contextType '';
  57.     /** Utilisee dans l'instace encours. La dévise de la compagnie connectée */
  58.     protected string $currency 'xaf';
  59.     /** La langue de la compagnie */
  60.     protected string $locale 'fr';
  61.     protected ParameterBagInterface $params;
  62.     protected TokenGeneratorInterface $tokenGenerator;
  63.     protected LoggerInterface $logger;
  64.     protected NormalizerInterface $normalizer;
  65.     public function __construct(
  66.         RequestStack $request,
  67.         EntityManagerInterface $entityManager,
  68.         TranslatorInterface $translator,
  69.         ParameterBagInterface $params,
  70.         EventDispatcherInterface $eventDispatcher,
  71.         TokenGeneratorInterface $tokenGenerator,
  72.         LoggerInterface $logger,
  73.         NormalizerInterface $normalizer
  74.     ) {
  75.         $this->params $params;
  76.         $this->request $request->getCurrentRequest();
  77.         $this->em $entityManager;
  78.         $this->text $translator;
  79.         $this->tokenGenerator $tokenGenerator;
  80.         $this->eventDispatcher $eventDispatcher;
  81.         $this->loadDependencies();
  82.         $this->logger $logger;
  83.         $this->normalizer $normalizer;
  84.     }
  85.     /**
  86.      * We use this method like a constructor.
  87.      */
  88.     protected function loadDependencies(): void
  89.     {
  90.         $this->locales = (array) $this->params->get('locales');
  91.         $this->webFolder __DIR__ '/../../public/';
  92.         $this->em->getConnection()->getConfiguration()->setSQLLogger(null);
  93.         if ($this->request) {
  94.             $this->isAjaxRequest $this->request->isXmlHttpRequest();
  95.             $this->locale $this->request->getLocale();
  96.             // Start session if is not started
  97.             $this->session $this->request->getSession();
  98.             if (!$this->session->isStarted()) {
  99.                 $this->session->start();
  100.             }
  101.         }
  102.     }
  103.     /**
  104.      * Returns a JsonResponse that uses the serializer component if enabled, or json_encode.
  105.      *
  106.      * @param array<string> $headers
  107.      * @param array<string> $context
  108.      */
  109.     protected function json(mixed $dataint $status 200, array $headers = [], array $context = []): JsonResponse
  110.     {
  111.         $headers['Access-Control-Allow-Origin'] = '*';
  112.         if (!empty($context)) {
  113.             /** @phpstan-ignore-next-line */
  114.             $data $this->normalizer->normalize($data$context);
  115.         }
  116.         return parent::json($this->utf8ize($data), $status$headers$context);
  117.     }
  118.     /** Use it for json_encode some corrupt UTF-8 chars
  119.      * useful for = malformed utf-8 characters possibly incorrectly encoded by json_encode.
  120.      */
  121.     private function utf8ize(mixed $mixed): mixed
  122.     {
  123.         if (is_array($mixed)) {
  124.             foreach ($mixed as $key => $value) {
  125.                 $mixed[$key] = $this->utf8ize($value);
  126.             }
  127.         } elseif (is_string($mixed)) {
  128.             return mb_convert_encoding($mixed'UTF-8''UTF-8');
  129.         }
  130.         return $mixed;
  131.     }
  132.     protected function triggerEvent(string $eventTypeAppEvent $event): void
  133.     {
  134.         $this->eventDispatcher->dispatch($event$eventType);
  135.     }
  136.     protected function forceDownload(string $filestring $fileName nullstring $disposition ResponseHeaderBag::DISPOSITION_ATTACHMENT): BinaryFileResponse
  137.     {
  138.         $response = new BinaryFileResponse($file);
  139.         $response->headers->set('Access-Control-Expose-Headers''Content-Disposition');
  140.         $response->headers->set('Access-Control-Allow-Origin''*');
  141.         return $response;
  142.     }
  143.     protected function getRequestContent(): array
  144.     {
  145.         if ($this->request != null) {
  146.             $data $this->request->request->all();
  147.             if (count($data) > 0) {
  148.                 return $data;
  149.             }
  150.             return (array) json_decode($this->request->getContent(), true);
  151.         }
  152.         return [];
  153.     }
  154. }