Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ControllerTrait.php 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace Lc\SovBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  5. use Knp\Component\Pager\PaginatorInterface;
  6. use Lc\SovBundle\Component\FileComponent;
  7. use Lc\SovBundle\Container\ComponentContainer;
  8. use Lc\SovBundle\Container\File\FileContainer;
  9. use Lc\SovBundle\Container\Newsletter\NewsletterContainer;
  10. use Lc\SovBundle\Container\Reminder\ReminderContainer;
  11. use Lc\SovBundle\Container\Setting\SiteSettingContainer;
  12. use Lc\SovBundle\Container\Site\NewsContainer;
  13. use Lc\SovBundle\Container\Site\PageContainer;
  14. use Lc\SovBundle\Container\Site\SiteContainer;
  15. use Lc\SovBundle\Container\Ticket\TicketContainer;
  16. use Lc\SovBundle\Container\Ticket\TicketMessageContainer;
  17. use Lc\SovBundle\Container\User\GroupUserContainer;
  18. use Lc\SovBundle\Container\User\UserContainer;
  19. use Lc\SovBundle\Field\Filter\FilterManager;
  20. use Lc\SovBundle\Generator\PdfGenerator;
  21. use Lc\SovBundle\Repository\EntityRepository;
  22. use Lc\SovBundle\Solver\Setting\SettingSolver;
  23. use Lc\SovBundle\Translation\FlashBagTranslator;
  24. use Lc\SovBundle\Translation\TranslatorAdmin;
  25. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  26. use Psr\Log\LoggerInterface;
  27. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  28. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpFoundation\RequestStack;
  31. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  32. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  33. use Symfony\Component\Security\Core\Security;
  34. use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
  35. use Symfony\Contracts\Translation\TranslatorInterface;
  36. use Twig\Environment;
  37. trait ControllerTrait
  38. {
  39. //TODO si vous avez une erreur avec le :array c'est qu'il faut passer à symfony 5.4.
  40. //Suivez la procédure suivante : https://symfony.com/doc/current/setup/unstable_versions.html#upgrading-your-project-to-an-unstable-symfony-version
  41. //En gros il faut changer tout les dépendances symfony/ qui sont en 5.3 par 5.4 dans composer.json
  42. public static function getSubscribedServices() :array
  43. {
  44. return array_merge(
  45. parent::getSubscribedServices(),
  46. [
  47. Environment::class => Environment::class,
  48. Security::class => Security::class,
  49. EntityManagerInterface::class => EntityManagerInterface::class,
  50. UrlGeneratorInterface::class => UrlGeneratorInterface::class,
  51. SessionInterface::class => SessionInterface::class,
  52. PaginatorInterface::class => PaginatorInterface::class,
  53. RequestStack::class => RequestStack::class,
  54. EventDispatcherInterface::class => EventDispatcherInterface::class,
  55. LoggerInterface::class => LoggerInterface::class,
  56. ParameterBagInterface::class => ParameterBagInterface::class,
  57. TranslatorInterface::class => TranslatorInterface::class,
  58. PdfGenerator::class => PdfGenerator::class,
  59. TranslatorAdmin::class => TranslatorAdmin::class,
  60. FilterManager::class => FilterManager::class,
  61. FlashBagTranslator::class => FlashBagTranslator::class,
  62. MailjetTransport::class => MailjetTransport::class,
  63. AdminUrlGenerator::class => AdminUrlGenerator::class,
  64. SettingSolver::class => SettingSolver::class,
  65. ComponentContainer::class => ComponentContainer::class,
  66. FileContainer::class => FileContainer::class,
  67. NewsletterContainer::class => NewsletterContainer::class,
  68. ReminderContainer::class => ReminderContainer::class,
  69. NewsContainer::class => NewsContainer::class,
  70. PageContainer::class => PageContainer::class,
  71. SiteContainer::class => SiteContainer::class,
  72. TicketContainer::class => TicketContainer::class,
  73. TicketMessageContainer::class => TicketMessageContainer::class,
  74. GroupUserContainer::class => GroupUserContainer::class,
  75. UserContainer::class => UserContainer::class,
  76. SiteSettingContainer::class => SiteSettingContainer::class,
  77. EntityRepository::class => EntityRepository::class,
  78. FileComponent::class => FileComponent::class,
  79. ]
  80. );
  81. }
  82. public function addFlashTranslator(
  83. string $type,
  84. $translationKeyName,
  85. $translationEntityName = null,
  86. $translationParam = array()
  87. ): void
  88. {
  89. if ($translationEntityName === null && method_exists($this, 'getTranslationEntityName')) {
  90. $translationEntityName = $this->getTranslationEntityName();
  91. }
  92. $this->get(FlashBagTranslator::class)->add(
  93. $type,
  94. $translationKeyName,
  95. $translationEntityName,
  96. $translationParam
  97. );
  98. }
  99. public function isInstanceOf(string $interfaceName): bool
  100. {
  101. return in_array($interfaceName, class_implements($this->getEntityFqcn()));
  102. }
  103. public function generateEaUrl(string $controller = null, string $action = null, int $entityId = null, array $extraParam = array()): string
  104. {
  105. $adminUrlGenerator = $this->get(AdminUrlGenerator::class);
  106. if ($controller) {
  107. $adminUrlGenerator->setController($controller);
  108. }
  109. if ($action) {
  110. $adminUrlGenerator->setAction($action);
  111. }
  112. if ($entityId) {
  113. $adminUrlGenerator->setEntityId($entityId);
  114. }
  115. if ($extraParam) {
  116. foreach ($extraParam as $key => $value) {
  117. $adminUrlGenerator->set($key, $value);
  118. }
  119. }
  120. return $adminUrlGenerator->generateUrl();
  121. }
  122. public function getReferer(Request $request): ?string
  123. {
  124. return $request->headers->get('referer');
  125. }
  126. public function getTemplating(): Environment
  127. {
  128. return $this->get(Environment::class);
  129. }
  130. public function getEntityManager(): EntityManagerInterface
  131. {
  132. return $this->get(EntityManagerInterface::class);
  133. }
  134. public function getRouter(): UrlGeneratorInterface
  135. {
  136. return $this->get(UrlGeneratorInterface::class);
  137. }
  138. public function getSession(): SessionInterface
  139. {
  140. return $this->get(SessionInterface::class);
  141. }
  142. public function getSecurity(): Security
  143. {
  144. return $this->get(Security::class);
  145. }
  146. public function getPaginator(): PaginatorInterface
  147. {
  148. return $this->get(PaginatorInterface::class);
  149. }
  150. public function getRequestStack(): RequestStack
  151. {
  152. return $this->get(RequestStack::class);
  153. }
  154. public function getEventDispatcher(): EventDispatcherInterface
  155. {
  156. return $this->get(EventDispatcherInterface::class);
  157. }
  158. public function getParameterBag(): ParameterBagInterface
  159. {
  160. return $this->get(ParameterBagInterface::class);
  161. }
  162. public function getTranslator(): TranslatorInterface
  163. {
  164. return $this->get(TranslatorInterface::class);
  165. }
  166. public function getPdfGenerator(): PdfGenerator
  167. {
  168. return $this->get(PdfGenerator::class);
  169. }
  170. public function getTranslatorAdmin(): TranslatorAdmin
  171. {
  172. return $this->get(TranslatorAdmin::class);
  173. }
  174. public function getLogger(): LoggerInterface
  175. {
  176. return $this->get(LoggerInterface::class);
  177. }
  178. public function getMailjetTransport(): MailjetTransport
  179. {
  180. return $this->get(MailjetTransport::class);
  181. }
  182. public function getAdminUrlGenerator(): AdminUrlGenerator
  183. {
  184. return $this->get(AdminUrlGenerator::class);
  185. }
  186. public function getSettingSolver(): SettingSolver
  187. {
  188. return $this->get(SettingSolver::class);
  189. }
  190. public function getSettingValue($entity, $settingName)
  191. {
  192. return $this->getSettingSolver()->getSettingValue($entity, $settingName);
  193. }
  194. public function getComponentContainer(): ComponentContainer
  195. {
  196. return $this->get(ComponentContainer::class);
  197. }
  198. public function getFileContainer(): FileContainer
  199. {
  200. return $this->get(FileContainer::class);
  201. }
  202. public function getNewsletterContainer(): NewsletterContainer
  203. {
  204. return $this->get(NewsletterContainer::class);
  205. }
  206. public function getReminderContainer(): ReminderContainer
  207. {
  208. return $this->get(ReminderContainer::class);
  209. }
  210. public function getNewsContainer(): NewsContainer
  211. {
  212. return $this->get(NewsContainer::class);
  213. }
  214. public function getPageContainer(): PageContainer
  215. {
  216. return $this->get(PageContainer::class);
  217. }
  218. public function getSiteContainer(): SiteContainer
  219. {
  220. return $this->get(SiteContainer::class);
  221. }
  222. public function getTicketContainer(): TicketContainer
  223. {
  224. return $this->get(TicketContainer::class);
  225. }
  226. public function getTicketMessageContainer(): TicketMessageContainer
  227. {
  228. return $this->get(TicketMessageContainer::class);
  229. }
  230. public function getGroupUserContainer(): GroupUserContainer
  231. {
  232. return $this->get(GroupUserContainer::class);
  233. }
  234. public function getUserContainer(): UserContainer
  235. {
  236. return $this->get(UserContainer::class);
  237. }
  238. public function getSiteSettingContainer(): SiteSettingContainer
  239. {
  240. return $this->get(SiteSettingContainer::class);
  241. }
  242. public function setNoMemoryAndTimeLimit(): void
  243. {
  244. ini_set('memory_limit', '-1');
  245. set_time_limit(0);
  246. }
  247. }