No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

309 líneas
9.9KB

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