src/Core/Controller/AbstractCoreController.php line 116
<?phpnamespace App\Core\Controller;use App\Core\Contracts\TokenGeneratorInterface;use App\Core\EventListener\AppEvent;use Doctrine\ORM\EntityManagerInterface;use Psr\Log\LoggerInterface;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;use Symfony\Component\EventDispatcher\EventDispatcherInterface;use Symfony\Component\HttpFoundation\BinaryFileResponse;use Symfony\Component\HttpFoundation\JsonResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\RequestStack;use Symfony\Component\HttpFoundation\ResponseHeaderBag;use Symfony\Component\HttpFoundation\Session\SessionInterface;use Symfony\Component\Serializer\Normalizer\NormalizerInterface;use Symfony\Contracts\Translation\TranslatorInterface;/*** The core controller class.** All controllers classes must extends this class** @see http://github.com/kdms90* @since 3.0.0** @author Marius Stanislas KEMAYOU DJEUMENI <kdms1990@gmail.com>*/abstract class AbstractCoreController extends AbstractController{protected EntityManagerInterface $em;protected TranslatorInterface $text;protected SessionInterface $session;/*** Availables locales defined in parameters file.** @var array<string>*/protected array $locales;protected ?Request $request;/*** Check if is an ajax request.*/protected bool $isAjaxRequest;/*** Path to web folder.*/protected string $webFolder;/*** Define number of items to show in one page.*/protected int $nbItemsPerPage = 10;protected EventDispatcherInterface $eventDispatcher;/** Current actor ID */protected int $actor_id;/** Détermine sous quelle forme d'intervenant l'utilisateur travaille dans la compagnie actuelle */protected string $contextType = '';/** Utilisee dans l'instace encours. La dévise de la compagnie connectée */protected string $currency = 'xaf';/** La langue de la compagnie */protected string $locale = 'fr';protected ParameterBagInterface $params;protected TokenGeneratorInterface $tokenGenerator;protected LoggerInterface $logger;protected NormalizerInterface $normalizer;public function __construct(RequestStack $request,EntityManagerInterface $entityManager,TranslatorInterface $translator,ParameterBagInterface $params,EventDispatcherInterface $eventDispatcher,TokenGeneratorInterface $tokenGenerator,LoggerInterface $logger,NormalizerInterface $normalizer) {$this->params = $params;$this->request = $request->getCurrentRequest();$this->em = $entityManager;$this->text = $translator;$this->tokenGenerator = $tokenGenerator;$this->eventDispatcher = $eventDispatcher;$this->loadDependencies();$this->logger = $logger;$this->normalizer = $normalizer;}/*** We use this method like a constructor.*/protected function loadDependencies(): void{$this->locales = (array) $this->params->get('locales');$this->webFolder = __DIR__ . '/../../public/';$this->em->getConnection()->getConfiguration()->setSQLLogger(null);if ($this->request) {$this->isAjaxRequest = $this->request->isXmlHttpRequest();$this->locale = $this->request->getLocale();// Start session if is not started$this->session = $this->request->getSession();if (!$this->session->isStarted()) {$this->session->start();}}}/*** Returns a JsonResponse that uses the serializer component if enabled, or json_encode.** @param array<string> $headers* @param array<string> $context*/protected function json(mixed $data, int $status = 200, array $headers = [], array $context = []): JsonResponse{$headers['Access-Control-Allow-Origin'] = '*';if (!empty($context)) {/** @phpstan-ignore-next-line */$data = $this->normalizer->normalize($data, $context);}return parent::json($this->utf8ize($data), $status, $headers, $context);}/** Use it for json_encode some corrupt UTF-8 chars* useful for = malformed utf-8 characters possibly incorrectly encoded by json_encode.*/private function utf8ize(mixed $mixed): mixed{if (is_array($mixed)) {foreach ($mixed as $key => $value) {$mixed[$key] = $this->utf8ize($value);}} elseif (is_string($mixed)) {return mb_convert_encoding($mixed, 'UTF-8', 'UTF-8');}return $mixed;}protected function triggerEvent(string $eventType, AppEvent $event): void{$this->eventDispatcher->dispatch($event, $eventType);}protected function forceDownload(string $file, string $fileName = null, string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT): BinaryFileResponse{$response = new BinaryFileResponse($file);$response->headers->set('Access-Control-Expose-Headers', 'Content-Disposition');$response->headers->set('Access-Control-Allow-Origin', '*');return $response;}protected function getRequestContent(): array{if ($this->request != null) {$data = $this->request->request->all();if (count($data) > 0) {return $data;}return (array) json_decode($this->request->getContent(), true);}return [];}}