app/Kernel.php line 35

Open in your IDE?
  1. <?php
  2. namespace IcebirdCMS\App;
  3. use PDOException;
  4. use Spoon;
  5. use SpoonDatabaseException;
  6. use Symfony\Component\DependencyInjection\ContainerBuilder;
  7. use Symfony\Component\Filesystem\Filesystem;
  8. use Symfony\Component\Finder\Finder;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
  11. use Symfony\Component\HttpKernel\Kernel as BaseKernel;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\HttpKernelInterface;
  14. use Application\DependencyInjection\ApplicationExtension;
  15. abstract class Kernel extends BaseKernel
  16. {
  17.     private $request;
  18.     public function __construct(string $environmentbool $enableDebug)
  19.     {
  20.         $this->request Request::createFromGlobals();
  21.         parent::__construct($environment$enableDebug);
  22.         $this->boot();
  23.     }
  24.     public function handle(Request $request$type HttpKernelInterface::MASTER_REQUEST$catch true): Response
  25.     {
  26.         $this->boot();
  27.         return $this->getHttpKernel()->handle($request$type$catch);
  28.     }
  29.     public function boot(): void
  30.     {
  31.         if ($this->booted) {
  32.             return;
  33.         }
  34.         parent::boot();
  35.         $this->defineIcebirdConstants();
  36.     }
  37.     public function defineIcebirdConstants(): void
  38.     {
  39.         $container $this->getContainer();
  40.         Spoon::setDebug($container->getParameter('kernel.debug'));
  41.         Spoon::setDebugEmail($container->getParameter('icebird.debug_email'));
  42.         Spoon::setDebugMessage($container->getParameter('icebird.debug_message'));
  43.         Spoon::setCharset($container->getParameter('kernel.charset'));
  44.         defined('PATH_WWW') || define('PATH_WWW'realpath($container->getParameter('site.path_www')));
  45.         defined('SITE_DEFAULT_LANGUAGE') || define('SITE_DEFAULT_LANGUAGE'$container->getParameter('site.default_language'));
  46.         defined('SITE_DEFAULT_TITLE') || define('SITE_DEFAULT_TITLE'$container->getParameter('site.default_title'));
  47.         defined('SITE_MULTILANGUAGE') || define('SITE_MULTILANGUAGE'$container->getParameter('site.multilanguage'));
  48.         defined('SITE_DOMAIN') || define('SITE_DOMAIN'$container->getParameter('site.domain'));
  49.         defined('SITE_PROTOCOL') || define('SITE_PROTOCOL'$container->getParameter('site.protocol'));
  50.         defined('SITE_URL') || define('SITE_URL'SITE_PROTOCOL '://' SITE_DOMAIN);
  51.         defined('ICEBIRD_VERSION') || define('ICEBIRD_VERSION'$container->getParameter('icebird.version'));
  52.         defined('ACTION_GROUP_TAG') || define('ACTION_GROUP_TAG'$container->getParameter('action.group_tag'));
  53.         defined('ACTION_RIGHTS_LEVEL') || define('ACTION_RIGHTS_LEVEL'$container->getParameter('action.rights_level'));
  54.         defined('APPLICATION_PATH') || define('APPLICATION_PATH'PATH_WWW '/src/Application');
  55.         defined('APPLICATION_CACHE_PATH') || define('APPLICATION_CACHE_PATH'APPLICATION_PATH '/Cache');
  56.         defined('APPLICATION_CORE_PATH') || define('APPLICATION_CORE_PATH'APPLICATION_PATH '/Core');
  57.         defined('APPLICATION_MODULES_PATH') || define('APPLICATION_MODULES_PATH'APPLICATION_PATH '/Modules');
  58.         defined('APPLICATION_CORE_URL') || define('APPLICATION_CORE_URL''/src/Application/Core');
  59.         defined('APPLICATION_CACHE_URL') || define('APPLICATION_CACHE_URL''/src/Application/Cache');
  60.         defined('WEB_PATH') || define('WEB_PATH'PATH_WWW '/src/Web');
  61.         defined('WEB_CACHE_PATH') || define('WEB_CACHE_PATH'WEB_PATH '/Cache');
  62.         defined('WEB_THEMES_PATH') || define('WEB_THEMES_PATH'WEB_PATH '/Themes');
  63.         defined('WEB_CORE_PATH') || define('WEB_CORE_PATH'WEB_PATH '/Core');
  64.         defined('WEB_MODULES_PATH') || define('WEB_MODULES_PATH'WEB_PATH '/Modules');
  65.         defined('WEB_FILES_PATH') || define('WEB_FILES_PATH'WEB_PATH '/Files');
  66.         defined('WEB_FILES_URL') || define('WEB_FILES_URL''/src/Web/Files');
  67.         defined('WEB_CORE_URL') || define('WEB_CORE_URL''/src/Web/Core');
  68.         defined('WEB_CACHE_URL') || define('WEB_CACHE_URL''/src/Web/Cache');
  69.     }
  70.     protected function buildContainer()
  71.     {
  72.         $container parent::buildContainer();
  73.         $installedModules $this->getInstalledModules($container);
  74.         $container->setParameter('installed_modules'$installedModules);
  75.         foreach ($installedModules as $module) {
  76.             $class 'Application\\Modules\\' $module '\\DependencyInjection\\' $module 'Extension';
  77.             if (class_exists($class)) {
  78.                 $container->registerExtension(new $class());
  79.             }
  80.         }
  81.         $container->registerExtension(new ApplicationExtension());
  82.         $container->getCompilerPassConfig()->setMergePass(
  83.             new MergeExtensionConfigurationPass(array_keys($container->getExtensions()))
  84.         );
  85.         return $container;
  86.     }
  87.     private function getInstalledModules(ContainerBuilder $containerBuilder): array
  88.     {
  89.         if ($this->environment === 'install' || $this->environment === 'test') {
  90.             return $this->getAllPossibleModuleNames();
  91.         }
  92.         $moduleNames = [];
  93.         if ($this->isInstallingModule()) {
  94.             $moduleNames[] = $this->request->query->get('module');
  95.         }
  96.         try {
  97.             $moduleNames array_merge(
  98.                 $moduleNames,
  99.                 (array) $containerBuilder->get('database')->getColumn(
  100.                     'SELECT name FROM modules'
  101.                 )
  102.             );
  103.         } catch (SpoonDatabaseException $e) {
  104.             $moduleNames = [];
  105.         } catch (PDOException $e) {
  106.             $moduleNames = [];
  107.         }
  108.         if (empty($moduleNames)) {
  109.             return $this->getAllPossibleModuleNames();
  110.         }
  111.         return $moduleNames;
  112.     }
  113.     private function isInstallingModule(): bool
  114.     {
  115.         return preg_match('/\/private(\/\w\w)?\/extensions\/install_module\?/'$this->request->getRequestUri())
  116.                && $this->request->query->has('module')
  117.                && in_array($this->request->query->get('module'), $this->getAllPossibleModuleNames());
  118.     }
  119.     private function getAllPossibleModuleNames(): array
  120.     {
  121.         $moduleNames = [];
  122.         $finder = new Finder();
  123.         $directories $finder->directories()->in(__DIR__ '/../src/Application/Modules')->depth(0);
  124.         foreach ($directories->getIterator() as $directory) {
  125.             $moduleNames[] = $directory->getFilename();
  126.         }
  127.         return $moduleNames;
  128.     }
  129.     protected function initializeContainer(): void
  130.     {
  131.         if ($this->isInstallingModule()) {
  132.             $fileSystem = new Filesystem();
  133.             $fileSystem->remove($this->getCacheDir().'/'.$this->getContainerClass().'.php');
  134.         }
  135.         parent::initializeContainer();
  136.     }
  137.     public function getLogDir(): string
  138.     {
  139.         return dirname(__DIR__).'/var/logs/' $this->environment;
  140.     }
  141.     public function getCacheDir(): string
  142.     {
  143.         return dirname(__DIR__) . '/var/cache/' $this->environment;
  144.     }
  145. }