src/Kernel.php line 150

Open in your IDE?
  1. <?php
  2. namespace App;
  3. use Exception;
  4. use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
  5. use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
  6. use Symfony\Component\HttpKernel\Kernel as BaseKernel;
  7. use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
  8. use function dirname;
  9. class Kernel extends BaseKernel
  10. {
  11. use MicroKernelTrait;
  12. /**
  13. * Chaque platform a son propre cache
  14. *
  15. * @return string
  16. *
  17. * @throws Exception
  18. */
  19. public function getCacheDir(): string
  20. {
  21. return dirname(__DIR__) . '/var/cache/' . $this->environment . '/' . $this->getDomain();
  22. }
  23. /**
  24. * Chaque platform a ses propres logs
  25. *
  26. * @return string
  27. * @throws Exception
  28. */
  29. public function getLogDir(): string
  30. {
  31. return dirname(__DIR__) . '/var/log/' . $this->getDomain();
  32. }
  33. public function boot()
  34. {
  35. parent::boot();
  36. StaticContainer::init($this->getContainer());
  37. }
  38. /**
  39. * @param ContainerConfigurator $container
  40. *
  41. * @return void
  42. * @throws Exception
  43. */
  44. protected function configureContainer(ContainerConfigurator $container): void
  45. {
  46. $container->import(dirname(__DIR__) . '/config/{packages}/*.yaml');
  47. $container->import(dirname(__DIR__) . '/config/{packages}/' . $this->environment . '/*.yaml');
  48. // Import conditionnel des configs SAML
  49. if ($_ENV['SAML_ENABLED'] ?? false) {
  50. $container->import(dirname(__DIR__) . '/config/saml_config/load_saml.php');
  51. $container->import(dirname(__DIR__) . '/config/saml_config/security_saml.yaml');
  52. }
  53. if (is_file(dirname(__DIR__) . '/config/services.yaml')) {
  54. $folder = 'platform';
  55. // Utilise le dossier ".loc" si on est en local
  56. $data = explode('.', $this->getDomain());
  57. if (end($data) == 'loc') {
  58. $folder = 'platform.loc';
  59. } elseif (end($data) == '__test') {
  60. $folder = 'tests/platform';
  61. }
  62. // fin
  63. $container->import(dirname(__DIR__) . '/config/services.yaml');
  64. // Importe les réglages d'une platform depuis l'url déduite
  65. $configFile = dirname(__DIR__) . "/config/$folder/" . $this->getDomain() . ".yaml";
  66. if (!is_file($configFile)) {
  67. $src = dirname(__DIR__) . "/config/_platform_dtv/default.yaml";
  68. copy($src, $configFile);
  69. }
  70. $container->import($configFile);
  71. $container->import(dirname(__DIR__) . '/config/{services}_' . $this->environment . '.yaml');
  72. } elseif (is_file($path = dirname(__DIR__) . '/config/services.php')) {
  73. (require $path)($container->withPath($path), $this);
  74. }
  75. }
  76. /**
  77. * Déduit le domain depuis l'url
  78. *
  79. * @return mixed|string
  80. * @throws Exception
  81. */
  82. private function getDomain(): mixed
  83. {
  84. // On check si on est dans une commande
  85. if (!isset($_SERVER['HTTP_HOST']) && count($_SERVER['argv']) > 0) {
  86. // Check que l'on est dans une commande bin/console
  87. if (str_contains($_SERVER['argv'][0], 'bin/console') && array_key_exists(1, $_SERVER['argv'])) {
  88. $re = '/^dtv:.*$/';
  89. preg_match($re, $_SERVER['argv'][1], $matches, PREG_OFFSET_CAPTURE);
  90. // Check que l'on est dans une commande DTV
  91. if (count($matches) > 0) {
  92. if (!array_key_exists(2, $_SERVER['argv'])) {
  93. throw new Exception('Aucun subdomain trouvé dans la commande');
  94. }
  95. return $_SERVER['argv'][2];
  96. }
  97. }
  98. // La clé DTV_PLATFORM est utilisé pour les tests unitaires
  99. // Check que l'on lance notre commande de tests unitaires
  100. elseif (array_key_exists('DTV_PLATFORM', $_SERVER)) {
  101. return $_SERVER['DTV_PLATFORM'];
  102. }
  103. // @TODO Erreur pour les migrations d'acl en local,
  104. // @TODO il prend le mauvais yaml : celui dans platform au lieu de platform.loc
  105. return 'developpetesventes.com';
  106. } //Si il n'y as pas de HOST
  107. elseif (!isset($_SERVER['HTTP_HOST'])) {
  108. return 'developpetesventes.com';
  109. }
  110. // On retire le www.
  111. if (str_starts_with($_SERVER['HTTP_HOST'], 'www.')) {
  112. $subdomain = substr($_SERVER['HTTP_HOST'], 4);
  113. } else {
  114. $subdomain = $_SERVER['HTTP_HOST'];
  115. }
  116. return $subdomain;
  117. }
  118. /**
  119. * @param RoutingConfigurator $routes
  120. *
  121. * @return void
  122. * @throws Exception
  123. */
  124. protected function configureRoutes(RoutingConfigurator $routes): void
  125. {
  126. $routes->import(dirname(__DIR__) . '/config/{routes}/' . $this->environment . '/*.yaml');
  127. $routes->import(dirname(__DIR__) . '/config/{routes}/*.yaml');
  128. if (is_file(dirname(__DIR__) . '/config/routes.yaml')) {
  129. $routes->import(dirname(__DIR__) . '/config/routes.yaml');
  130. } elseif (is_file($path = dirname(__DIR__) . '/config/routes.php')) {
  131. (require $path)($routes->withPath($path), $this);
  132. }
  133. // Import conditionnel des routes SAML
  134. if ($_ENV['SAML_ENABLED'] ?? false) {
  135. $routes->import(dirname(__DIR__) . '/config/saml_config/routes_saml.yaml');
  136. }
  137. }
  138. }