From 73d214c21b95746e0b06dfe003aa46e78f84373d Mon Sep 17 00:00:00 2001 From: christianbeeznst Date: Fri, 30 Aug 2024 18:14:43 -0500 Subject: [PATCH] Internal: Refactor global.inc.php for CLI compatibility --- public/main/inc/global.inc.php | 122 ++++++++++++++++----------------- 1 file changed, 58 insertions(+), 64 deletions(-) diff --git a/public/main/inc/global.inc.php b/public/main/inc/global.inc.php index f446c1adde..67d19305c3 100644 --- a/public/main/inc/global.inc.php +++ b/public/main/inc/global.inc.php @@ -35,39 +35,52 @@ if ($debug) { Debug::enable(); } -$kernel = new Chamilo\Kernel($env, $debug); -// Loading Request from Sonata. In order to use Sonata Pages Bundle. -$request = Request::createFromGlobals(); -// This 'load_legacy' variable is needed to know that symfony is loaded using old style legacy mode, -// and not called from a symfony controller from public/ -$request->request->set('load_legacy', true); -$currentBaseUrl = $request->getBaseUrl(); - -if (empty($currentBaseUrl)) { - $currentBaseUrl = $request->getSchemeAndHttpHost() . $request->getBasePath(); -} +$isCli = php_sapi_name() === 'cli'; +if ($isCli) { -$response = $kernel->handle($request, HttpKernelInterface::MAIN_REQUEST, false); + $kernel = new Chamilo\Kernel($env, $debug); + $kernel->boot(); -$container = $kernel->getContainer(); -$router = $container->get('router'); -$context = $router->getContext(); -$router->setContext($context); + if (!$kernel->isInstalled()) { + throw new Exception('Chamilo is not installed'); + } -$context = Container::getRouter()->getContext(); + $container = $kernel->getContainer(); + $router = $container->get('router'); + $context = $router->getContext(); + $router->setContext($context); + Database::setManager($container->get('doctrine.orm.entity_manager')); -$isCli = 'cli' === php_sapi_name(); -$baseUrl = null; -if ($isCli) { $cliOptions = getopt('', ['url:']); if (!empty($cliOptions['url'])) { $baseUrl = $cliOptions['url']; + $context->setBaseUrl($baseUrl); } -} -if ($isCli && $baseUrl) { - $context->setBaseUrl($baseUrl); + echo "CLI mode: EntityManager initialized.\n"; + } else { + $kernel = new Chamilo\Kernel($env, $debug); + // Loading Request from Sonata. In order to use Sonata Pages Bundle. + $request = Request::createFromGlobals(); + // This 'load_legacy' variable is needed to know that symfony is loaded using old style legacy mode, + // and not called from a symfony controller from public/ + $request->request->set('load_legacy', true); + $currentBaseUrl = $request->getBaseUrl(); + + if (empty($currentBaseUrl)) { + $currentBaseUrl = $request->getSchemeAndHttpHost() . $request->getBasePath(); + } + + $response = $kernel->handle($request, HttpKernelInterface::MAIN_REQUEST, false); + + $container = $kernel->getContainer(); + $router = $container->get('router'); + $context = $router->getContext(); + $router->setContext($context); + + $context = Container::getRouter()->getContext(); + $currentUri = $request->getRequestUri(); $fullUrl = $currentBaseUrl . $currentUri; @@ -92,53 +105,34 @@ if ($isCli && $baseUrl) { } $context->setBaseUrl($newBaseUrl); -} -try { + try { + // Do not over-use this variable. It is only for this script's local use. + $libraryPath = __DIR__.'/lib/'; + $container = $kernel->getContainer(); - if (!$kernel->isInstalled()) { - throw new Exception('Chamilo is not installed'); - } - - // Do not over-use this variable. It is only for this script's local use. - $libraryPath = __DIR__.'/lib/'; - $container = $kernel->getContainer(); + // Symfony uses request_stack now + $container->get('request_stack')->push($request); + $container->get('translator')->setLocale($request->getLocale()); - // Symfony uses request_stack now - $container->get('request_stack')->push($request); - $container->get('translator')->setLocale($request->getLocale()); + /** @var FlashBag $flashBag */ + $flashBag = $request->getSession()->getFlashBag(); + $saveFlashBag = !empty($flashBag->keys()) ? $flashBag->all() : null; - /** @var FlashBag $flashBag */ - $flashBag = $request->getSession()->getFlashBag(); - $saveFlashBag = !empty($flashBag->keys()) ? $flashBag->all() : null; - - if (!empty($saveFlashBag)) { - foreach ($saveFlashBag as $typeMessage => $messageList) { - foreach ($messageList as $message) { - Container::getSession()->getFlashBag()->add($typeMessage, $message); + if (!empty($saveFlashBag)) { + foreach ($saveFlashBag as $typeMessage => $messageList) { + foreach ($messageList as $message) { + Container::getSession()->getFlashBag()->add($typeMessage, $message); + } } } - } - // Connect Chamilo with the Symfony container - // Container::setContainer($container); - // Container::setLegacyServices($container); - - // The code below is not needed. The connections is now made in the file: - // src/CoreBundle/EventListener/LegacyListener.php - // This is called when when doing the $kernel->handle - $charset = 'UTF-8'; - ini_set('log_errors', '1'); - $this_section = SECTION_GLOBAL; - //Default quota for the course documents folder - /*$default_quota = api_get_setting('default_document_quotum'); - //Just in case the setting is not correctly set - if (empty($default_quota)) { - $default_quota = 100000000; + $charset = 'UTF-8'; + ini_set('log_errors', '1'); + $this_section = SECTION_GLOBAL; + define('DEFAULT_DOCUMENT_QUOTA', 100000000); + } catch (Exception $e) { + $controller = new ExceptionController(); + $controller->show($e); } - define('DEFAULT_DOCUMENT_QUOTA', $default_quota);*/ - define('DEFAULT_DOCUMENT_QUOTA', 100000000); -} catch (Exception $e) { - $controller = new ExceptionController(); - $controller->show($e); }