您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

288 行
9.4KB

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