src/Kernel.php line 156

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. {
  51. $container->import(dirname(__DIR__) . '/config/saml_config/load_saml.php');
  52. $container->import(dirname(__DIR__) . '/config/saml_config/security_saml.yaml');
  53. }
  54. if(is_file(dirname(__DIR__) . '/config/services.yaml'))
  55. {
  56. $folder = 'platform';
  57. // Utilise le dossier ".loc" si on est en local
  58. $data = explode('.', $this->getDomain());
  59. if(end($data) == 'loc')
  60. {
  61. $folder = 'platform.loc';
  62. }
  63. elseif(end($data) == '__test')
  64. {
  65. $folder = 'tests/platform';
  66. }
  67. // fin
  68. $container->import(dirname(__DIR__) . '/config/services.yaml');
  69. // Importe les réglages d'une platform depuis l'url déduite
  70. $configFile = dirname(__DIR__) . "/config/$folder/" . $this->getDomain() . ".yaml";
  71. if(!is_file($configFile))
  72. {
  73. $src = dirname(__DIR__) . "/config/_platform_dtv/default.yaml";
  74. copy($src, $configFile);
  75. }
  76. $container->import($configFile);
  77. $container->import(dirname(__DIR__) . '/config/{services}_' . $this->environment . '.yaml');
  78. }
  79. elseif(is_file($path = dirname(__DIR__) . '/config/services.php'))
  80. {
  81. (require $path)($container->withPath($path), $this);
  82. }
  83. }
  84. /**
  85. * Déduit le domain depuis l'url
  86. *
  87. * @return mixed|string
  88. * @throws Exception
  89. */
  90. private function getDomain(): mixed
  91. {
  92. // On check si on est dans une commande
  93. if(!isset($_SERVER['HTTP_HOST']) && count($_SERVER['argv']) > 0)
  94. {
  95. // Check que l'on est dans une commande bin/console
  96. if(str_contains($_SERVER['argv'][0], 'bin/console') && array_key_exists(1, $_SERVER['argv']))
  97. {
  98. $re = '/^dtv:.*$/';
  99. preg_match($re, $_SERVER['argv'][1], $matches, PREG_OFFSET_CAPTURE);
  100. // Check que l'on est dans une commande DTV
  101. if(count($matches) > 0)
  102. {
  103. if(!array_key_exists(2, $_SERVER['argv']))
  104. {
  105. throw new Exception('Aucun subdomain trouvé dans la commande');
  106. }
  107. return $_SERVER['argv'][2];
  108. }
  109. }
  110. // La clé DTV_PLATFORM est utilisé pour les tests unitaires
  111. // Check que l'on lance notre commande de tests unitaires
  112. elseif(array_key_exists('DTV_PLATFORM', $_SERVER))
  113. {
  114. return $_SERVER['DTV_PLATFORM'];
  115. }
  116. // @TODO Erreur pour les migrations d'acl en local,
  117. // @TODO il prend le mauvais yaml : celui dans platform au lieu de platform.loc
  118. return 'developpetesventes.com';
  119. } //Si il n'y as pas de HOST
  120. elseif(!isset($_SERVER['HTTP_HOST']))
  121. {
  122. return 'developpetesventes.com';
  123. }
  124. // On retire le www.
  125. if(str_starts_with($_SERVER['HTTP_HOST'], 'www.'))
  126. {
  127. $subdomain = substr($_SERVER['HTTP_HOST'], 4);
  128. }
  129. else
  130. {
  131. $subdomain = $_SERVER['HTTP_HOST'];
  132. }
  133. return $subdomain;
  134. }
  135. /**
  136. * @param RoutingConfigurator $routes
  137. *
  138. * @return void
  139. * @throws Exception
  140. */
  141. protected function configureRoutes(RoutingConfigurator $routes): void
  142. {
  143. $routes->import(dirname(__DIR__) . '/config/{routes}/' . $this->environment . '/*.yaml');
  144. $routes->import(dirname(__DIR__) . '/config/{routes}/*.yaml');
  145. if(is_file(dirname(__DIR__) . '/config/routes.yaml'))
  146. {
  147. $routes->import(dirname(__DIR__) . '/config/routes.yaml');
  148. }
  149. elseif(is_file($path = dirname(__DIR__) . '/config/routes.php'))
  150. {
  151. (require $path)($routes->withPath($path), $this);
  152. }
  153. // Import conditionnel des routes SAML
  154. if($_ENV['SAML_ENABLED'] ?? false)
  155. {
  156. $routes->import(dirname(__DIR__) . '/config/saml_config/routes_saml.yaml');
  157. }
  158. }
  159. }