<?php
namespace App;
use Exception;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use function dirname;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
/**
* Chaque platform a son propre cache
*
* @return string
*
* @throws Exception
*/
public function getCacheDir(): string
{
return dirname( __DIR__ ) . '/var/cache/' . $this->environment . '/' . $this->getDomain();
}
/**
* Chaque platform a ses propres logs
*
* @return string
* @throws Exception
*/
public function getLogDir(): string
{
return dirname( __DIR__ ) . '/var/log/' . $this->getDomain();
}
public function boot()
{
parent::boot();
StaticContainer::init($this->getContainer());
}
/**
* @param ContainerConfigurator $container
*
* @return void
* @throws Exception
*/
protected function configureContainer( ContainerConfigurator $container ): void
{
$container->import( dirname( __DIR__ ) . '/config/{packages}/*.yaml' );
$container->import( dirname( __DIR__ ) . '/config/{packages}/' . $this->environment . '/*.yaml' );
if ( is_file( dirname( __DIR__ ) . '/config/services.yaml' ) ) {
$folder = 'platform';
// Utilise le dossier ".loc" si on est en local
$data = explode( '.', $this->getDomain() );
if ( end( $data ) == 'loc' ) {
$folder = 'platform.loc';
}
elseif ( end( $data ) == '__test' ) {
$folder = 'tests/platform';
}
// fin
$container->import( dirname( __DIR__ ) . '/config/services.yaml' );
// Importe les réglages d'une platform depuis l'url déduite
$configFile = dirname( __DIR__ ) . "/config/$folder/" . $this->getDomain() . ".yaml";
if ( !is_file( $configFile ) ) {
$src = dirname( __DIR__ ) . "/config/_platform_dtv/default.yaml";
copy( $src, $configFile );
}
$container->import( $configFile );
$container->import( dirname( __DIR__ ) . '/config/{services}_' . $this->environment . '.yaml' );
}
elseif ( is_file( $path = dirname( __DIR__ ) . '/config/services.php' ) ) {
( require $path )( $container->withPath( $path ), $this );
}
}
/**
* Déduit le domain depuis l'url
*
* @return mixed|string
* @throws Exception
*/
private function getDomain()
{
// On check si on est dans une commande
if ( !isset( $_SERVER[ 'HTTP_HOST' ] ) && count( $_SERVER[ 'argv' ] ) > 0 ) {
// Check que l'on est dans une commande bin/console
if ( str_contains( $_SERVER[ 'argv' ][ 0 ], 'bin/console' ) && array_key_exists( 1, $_SERVER[ 'argv' ] ) ) {
$re = '/^dtv:.*$/';
preg_match( $re, $_SERVER[ 'argv' ][ 1 ], $matches, PREG_OFFSET_CAPTURE );
// Check que l'on est dans une commande DTV
if ( count( $matches ) > 0 ) {
if ( !array_key_exists( 2, $_SERVER[ 'argv' ] ) ) {
throw new Exception( 'Aucun subdomain trouvé dans la commande' );
}
return $_SERVER[ 'argv' ][ 2 ];
}
}
// La clé DTV_PLATFORM est utilisé pour les tests unitaires
// Check que l'on lance notre commande de tests unitaires
elseif ( array_key_exists( 'DTV_PLATFORM', $_SERVER ) ) {
return $_SERVER[ 'DTV_PLATFORM' ];
}
// @TODO Erreur pour les migrations d'acl en local,
// @TODO il prend le mauvais yaml : celui dans platform au lieu de platform.loc
return 'developpetesventes.com';
} //Si il n'y as pas de HOST
elseif ( !isset( $_SERVER[ 'HTTP_HOST' ] ) ) {
return 'developpetesventes.com';
}
// On retire le www.
if ( strpos( $_SERVER[ 'HTTP_HOST' ], 'www.' ) === 0 ) {
$subdomain = substr( $_SERVER[ 'HTTP_HOST' ], 4 );
}
else {
$subdomain = $_SERVER[ 'HTTP_HOST' ];
}
return $subdomain;
}
/**
* @param RoutingConfigurator $routes
*
* @return void
*/
protected function configureRoutes( RoutingConfigurator $routes ): void
{
$routes->import( dirname( __DIR__ ) . '/config/{routes}/' . $this->environment . '/*.yaml' );
$routes->import( dirname( __DIR__ ) . '/config/{routes}/*.yaml' );
if ( is_file( dirname( __DIR__ ) . '/config/routes.yaml' ) ) {
$routes->import( dirname( __DIR__ ) . '/config/routes.yaml' );
}
elseif ( is_file( $path = dirname( __DIR__ ) . '/config/routes.php' ) ) {
( require $path )( $routes->withPath( $path ), $this );
}
}
}