src/Kernel.php line 153

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