From 4539881d2ffbfef6dd5ddc8b346ba3453f5ccc72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Mon, 16 May 2022 11:33:46 +0200 Subject: [PATCH] chore: Refactor `Bootstrap` module initialization and `Config` class wiring. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- api.php | 6 +- cli.php | 10 +- index.php | 6 +- lib/Base.php | 87 +++++---- lib/Definitions.php | 36 ++-- lib/SP/Bootstrap.php | 417 +++++++++++++++++++++++-------------------- 6 files changed, 315 insertions(+), 247 deletions(-) diff --git a/api.php b/api.php index 80988ac6..25b6cd5c 100644 --- a/api.php +++ b/api.php @@ -22,7 +22,11 @@ * along with sysPass. If not, see . */ +use SP\Bootstrap; + const APP_ROOT = __DIR__; const APP_MODULE = 'api'; -require APP_ROOT . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'Base.php'; +$dic = require APP_ROOT.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Base.php'; + +Bootstrap::runApi($dic); diff --git a/cli.php b/cli.php index bf470b50..68891d34 100644 --- a/cli.php +++ b/cli.php @@ -22,7 +22,15 @@ * along with sysPass. If not, see . */ +use SP\Modules\Cli\Init as InitCli; + const APP_ROOT = __DIR__; const APP_MODULE = 'cli'; -require APP_ROOT . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'Base.php'; +$dic = require APP_ROOT.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Base.php'; + +logger('------------'); +logger('Boostrap:cli'); + +$cli = $dic->get(InitCli::class); +$cli->initialize(''); \ No newline at end of file diff --git a/index.php b/index.php index c0fb08fb..ce7db44c 100644 --- a/index.php +++ b/index.php @@ -22,7 +22,11 @@ * along with sysPass. If not, see . */ +use SP\Bootstrap; + const APP_ROOT = __DIR__; const APP_MODULE = 'web'; -require APP_ROOT . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'Base.php'; \ No newline at end of file +$dic = require APP_ROOT.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Base.php'; + +Bootstrap::runWeb($dic); diff --git a/lib/Base.php b/lib/Base.php index 69b9fad2..7d9f8213 100644 --- a/lib/Base.php +++ b/lib/Base.php @@ -24,82 +24,97 @@ use DI\ContainerBuilder; use Dotenv\Dotenv; -use SP\Bootstrap; defined('APP_ROOT') || die(); // Core PATHS const DS = DIRECTORY_SEPARATOR; const BASE_PATH = __DIR__; -const APP_PATH = APP_ROOT . DS . 'app'; -const VENDOR_PATH = APP_ROOT . DS . 'vendor'; -const SQL_PATH = APP_ROOT . DS . 'schemas'; -const PUBLIC_PATH = APP_ROOT . DS . 'public'; -const XML_SCHEMA = SQL_PATH . DS . 'syspass.xsd'; -const RESOURCES_PATH = APP_PATH . DS . 'resources'; -const MODULES_PATH = APP_PATH . DS . 'modules'; -const LOCALES_PATH = APP_PATH . DS . 'locales'; +const APP_PATH = APP_ROOT.DS.'app'; +const VENDOR_PATH = APP_ROOT.DS.'vendor'; +const SQL_PATH = APP_ROOT.DS.'schemas'; +const PUBLIC_PATH = APP_ROOT.DS.'public'; +const XML_SCHEMA = SQL_PATH.DS.'syspass.xsd'; +const RESOURCES_PATH = APP_PATH.DS.'resources'; +const MODULES_PATH = APP_PATH.DS.'modules'; +const LOCALES_PATH = APP_PATH.DS.'locales'; // Start tracking the memory used $memInit = memory_get_usage(); -require __DIR__ . DS . 'BaseFunctions.php'; -require VENDOR_PATH . DS . 'autoload.php'; +require __DIR__.DS.'BaseFunctions.php'; +require VENDOR_PATH.DS.'autoload.php'; $dotenv = Dotenv::createImmutable(APP_ROOT); $dotenv->load(); defined('APP_MODULE') || define('APP_MODULE', 'web'); define('DEBUG', getenv('DEBUG')); -define('IS_TESTING', +define( + 'IS_TESTING', getenv('IS_TESTING') - ?: defined('TEST_ROOT')); + ?: defined('TEST_ROOT') +); -define('CONFIG_PATH', +define( + 'CONFIG_PATH', getenv('CONFIG_PATH') - ?: APP_PATH . DS . 'config'); + ?: APP_PATH.DS.'config' +); // Setup config files -const OLD_CONFIG_FILE = CONFIG_PATH . DS . 'config.php'; +const OLD_CONFIG_FILE = CONFIG_PATH.DS.'config.php'; -define('CONFIG_FILE', +define( + 'CONFIG_FILE', getenv('CONFIG_FILE') - ?: CONFIG_PATH . DS . 'config.xml'); -define('ACTIONS_FILE', + ?: CONFIG_PATH.DS.'config.xml' +); +define( + 'ACTIONS_FILE', getenv('ACTIONS_FILE') - ?: RESOURCES_PATH . DS . 'actions.xml'); -define('MIMETYPES_FILE', + ?: RESOURCES_PATH.DS.'actions.xml' +); +define( + 'MIMETYPES_FILE', getenv('MIMETYPES_FILE') - ?: RESOURCES_PATH . DS . 'mime.xml'); -define('LOG_FILE', + ?: RESOURCES_PATH.DS.'mime.xml' +); +define( + 'LOG_FILE', getenv('LOG_FILE') - ?: CONFIG_PATH . DS . 'syspass.log'); + ?: CONFIG_PATH.DS.'syspass.log' +); -const LOCK_FILE = CONFIG_PATH . DS . '.lock'; +const LOCK_FILE = CONFIG_PATH.DS.'.lock'; // Setup application paths -define('BACKUP_PATH', +define( + 'BACKUP_PATH', getenv('BACKUP_PATH') - ?: APP_PATH . DS . 'backup'); -define('CACHE_PATH', + ?: APP_PATH.DS.'backup' +); +define( + 'CACHE_PATH', getenv('CACHE_PATH') - ?: APP_PATH . DS . 'cache'); -define('TMP_PATH', + ?: APP_PATH.DS.'cache' +); +define( + 'TMP_PATH', getenv('TMP_PATH') - ?: APP_PATH . DS . 'temp'); + ?: APP_PATH.DS.'temp' +); try { $moduleDefinitions = initModule(APP_MODULE); - $dic = (new ContainerBuilder) - ->writeProxiesToFile(true, CACHE_PATH . DS . 'proxies') + return (new ContainerBuilder) + ->writeProxiesToFile(true, CACHE_PATH.DS.'proxies') ->addDefinitions( - BASE_PATH . DS . 'Definitions.php', + BASE_PATH.DS.'Definitions.php', $moduleDefinitions ) ->build(); - - Bootstrap::run($dic); } catch (Exception $e) { processException($e); diff --git a/lib/Definitions.php b/lib/Definitions.php index a5777f47..5d39039d 100644 --- a/lib/Definitions.php +++ b/lib/Definitions.php @@ -24,7 +24,6 @@ use Monolog\Logger; use PHPMailer\PHPMailer\PHPMailer; -use Psr\Container\ContainerInterface; use SP\Config\Config; use SP\Config\ConfigDataInterface; use SP\Core\Acl\Acl; @@ -39,6 +38,7 @@ use SP\Http\Client; use SP\Http\Request; use SP\Providers\Auth\AuthProvider; use SP\Services\Account\AccountAclService; +use SP\Services\Config\ConfigBackupService; use SP\Storage\Database\DatabaseConnectionData; use SP\Storage\Database\DBStorageInterface; use SP\Storage\Database\MySQLHandler; @@ -51,51 +51,51 @@ use function DI\factory; use function DI\get; return [ - Request::class => create(Request::class) + Request::class => create(Request::class) ->constructor(\Klein\Request::createFromGlobals()), - ContextInterface::class => + ContextInterface::class => static fn() => ContextFactory::getForModule(APP_MODULE), - Config::class => - static fn(ContainerInterface $c) => new Config( + Config::class => create(Config::class) + ->constructor( new XmlHandler(new FileHandler(CONFIG_FILE)), new FileCache(Config::CONFIG_CACHE_FILE), - $c->get(ContextInterface::class), - $c + get(ContextInterface::class), + create(ConfigBackupService::class)->lazy() ), ConfigDataInterface::class => static fn(Config $config) => $config->getConfigData(), - DBStorageInterface::class => create(MySQLHandler::class) + DBStorageInterface::class => create(MySQLHandler::class) ->constructor( factory([DatabaseConnectionData::class, 'getFromConfig']) ), - Actions::class => + Actions::class => static fn() => new Actions( new FileCache(Actions::ACTIONS_CACHE_FILE), new XmlHandler(new FileHandler(ACTIONS_FILE)) ), - MimeTypes::class => + MimeTypes::class => static fn() => new MimeTypes( new FileCache(MimeTypes::MIME_CACHE_FILE), new XmlHandler(new FileHandler(MIMETYPES_FILE)) ), - Acl::class => autowire(Acl::class) + Acl::class => autowire(Acl::class) ->constructorParameter( 'action', get(Actions::class) ), - ThemeInterface::class => autowire(Theme::class) + ThemeInterface::class => autowire(Theme::class) ->constructorParameter('module', APP_MODULE) ->constructorParameter( 'fileCache', new FileCache(Theme::ICONS_CACHE_FILE) ), - PHPMailer::class => create(PHPMailer::class) + PHPMailer::class => create(PHPMailer::class) ->constructor(true), - Logger::class => create(Logger::class) + Logger::class => create(Logger::class) ->constructor('syspass'), - AccountAclService::class => autowire(AccountAclService::class), - \GuzzleHttp\Client::class => create(GuzzleHttp\Client::class) + AccountAclService::class => autowire(AccountAclService::class), + \GuzzleHttp\Client::class => create(GuzzleHttp\Client::class) ->constructor(factory([Client::class, 'getOptions'])), - CSRF::class => autowire(CSRF::class), - AuthProvider::class => autowire(AuthProvider::class)->lazy() + CSRF::class => autowire(CSRF::class), + AuthProvider::class => autowire(AuthProvider::class)->lazy(), ]; \ No newline at end of file diff --git a/lib/SP/Bootstrap.php b/lib/SP/Bootstrap.php index b236aa36..bae6aa4e 100644 --- a/lib/SP/Bootstrap.php +++ b/lib/SP/Bootstrap.php @@ -25,9 +25,6 @@ namespace SP; use Closure; -use DI\Container; -use DI\DependencyException; -use DI\NotFoundException; use Klein\Klein; use Klein\Response; use PHPMailer\PHPMailer\Exception; @@ -38,10 +35,10 @@ use SP\Config\ConfigUtil; use SP\Core\Exceptions\InitializationException; use SP\Core\Exceptions\SessionTimeout; use SP\Core\Language; +use SP\Core\ModuleBase; use SP\Core\PhpExtensionChecker; use SP\Http\Request; use SP\Modules\Api\Init as InitApi; -use SP\Modules\Cli\Init as InitCli; use SP\Modules\Web\Init as InitWeb; use SP\Plugin\PluginManager; use SP\Services\Api\ApiRequest; @@ -83,36 +80,35 @@ final class Bootstrap /** * @var bool Indica si la versión de PHP es correcta */ - public static bool $checkPhpVersion = false; + public static bool $checkPhpVersion = false; private static ContainerInterface $container; - private Klein $router; - private Request $request; - private ConfigDataInterface $configData; + private Klein $router; + private Request $request; + private ConfigDataInterface $configData; + private ModuleBase $module; /** * Bootstrap constructor. */ - public function __construct(ContainerInterface $container) + public function __construct(ConfigDataInterface $configData, Klein $router, Request $request) { - self::$container = $container; - // Set the default language Language::setLocales('en_US'); - $this->configData = $container->get(ConfigDataInterface::class); - $this->router = $container->get(Klein::class); - $this->request = $container->get(Request::class); + $this->configData = $configData; + $this->router = $router; + $this->request = $request; $this->initRouter(); } - protected function initRouter(): void + private function initRouter(): void { $this->router->onError(function ($router, $err_msg, $type, $err) { - logger('Routing error: ' . $err_msg); + logger('Routing error: '.$err_msg); /** @var Exception|Throwable $err */ - logger('Routing error: ' . $err->getTraceAsString()); + logger('Routing error: '.$err->getTraceAsString()); /** @var Klein $router */ $router->response()->body(__($err_msg)); @@ -124,20 +120,6 @@ final class Bootstrap null, $this->manageCorsRequest() ); - - // Manage requests for api module - $this->router->respond( - 'POST', - '@/api\.php', - $this->manageApiRequest() - ); - - // Manage requests for web module - $this->router->respond( - ['GET', 'POST'], - '@(?!/api\.php)', - $this->manageWebRequest() - ); } private function manageCorsRequest(): Closure @@ -164,61 +146,132 @@ final class Bootstrap $response->header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); } - private function manageApiRequest(): Closure + public static function getContainer(): ContainerInterface + { + return self::$container; + } + + /** + * @param \Psr\Container\ContainerInterface $container + * + * @return \SP\Bootstrap + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface + * + * TODO: Inject needed classes + */ + public static function runWeb(ContainerInterface $container): Bootstrap + { + logger('------------'); + logger('Boostrap:web'); + + // TODO: remove + self::$container = $container; + + /** @noinspection SelfClassReferencingInspection */ + $bs = $container->get(Bootstrap::class); + $bs->module = $container->get(InitWeb::class); + $bs->configureRouterForWeb(); + $bs->handleRequest(); + + return $bs; + } + + private function configureRouterForWeb(): void + { + // Manage requests for web module + $this->router->respond( + ['GET', 'POST'], + '@(?!/api\.php)', + $this->manageWebRequest() + ); + } + + private function manageWebRequest(): Closure { return function ($request, $response, $service) { + /** @var \Klein\Request $request */ + /** @var \Klein\Response $response */ + try { - logger('API route'); + logger('WEB route'); - $apiRequest = self::$container->get(ApiRequest::class); + /** @var \Klein\Request $request */ + $route = Filter::getString($request->param('r', 'index/index')); - [$controllerName, $action] = explode('/', $apiRequest->getMethod()); + if (!preg_match_all( + '#(?P[a-zA-Z]+)(?:/(?P[a-zA-Z]+))?(?P(/[a-zA-Z\d.]+)+)?#', + $route, + $matches + )) { + throw new RuntimeException(self::OOPS_MESSAGE); + } - $controllerClass = self::getControllerClass($controllerName); - - $method = $action . 'Action'; - - if (!method_exists($controllerClass, $method)) { - logger($controllerClass . '::' . $method); - - /** @var Response $response */ - $response->headers() - ->set( - 'Content-type', - 'application/json; charset=utf-8' - ); - - return $response->body( - JsonRpcResponse::getResponseError( - self::OOPS_MESSAGE, - JsonRpcResponse::METHOD_NOT_FOUND, - $apiRequest->getId() + $controllerName = $matches['controller'][0]; + $methodName = empty($matches['action'][0]) + ? 'indexAction' + : $matches['action'][0].'Action'; + $methodParams = empty($matches['params'][0]) + ? [] + : Filter::getArray( + explode( + '/', + trim( + $matches['params'][0], + '/' + ) ) ); + + $controllerClass = self::getClassFor($controllerName); + + $this->initializePluginClasses(); + + if (!method_exists($controllerClass, $methodName)) { + logger($controllerClass.'::'.$methodName); + + $response->code(404); + + throw new RuntimeException(self::OOPS_MESSAGE); } + $this->setCors($response); + $this->initializeCommon(); - self::$container->get(InitApi::class) - ->initialize($controllerName); + // TODO: remove?? + if (APP_MODULE === 'web') { + $this->module->initialize($controllerName); + } - logger('Routing call: ' . $controllerClass . '::' . $method); + logger( + sprintf( + 'Routing call: %s::%s::%s', + $controllerClass, + $methodName, + print_r($methodParams, true) + ) + ); - return call_user_func([new $controllerClass(self::$container, $method), $method]); + $controller = self::$container->get($controllerClass); + + return call_user_func_array([$controller, $methodName], $methodParams); + } catch (SessionTimeout $sessionTimeout) { + logger('Session timeout'); } catch (\Exception $e) { processException($e); /** @var Response $response */ - $response->headers()->set('Content-type', 'application/json; charset=utf-8'); + if ($response->status()->getCode() !== 404) { + $response->code(503); + } - return $response->body(JsonRpcResponse::getResponseException($e, 0)); - } finally { - $this->router->skipRemaining(); + return __($e->getMessage()); } }; } - private static function getControllerClass(string $controllerName): string + private static function getClassFor(string $controllerName): string { return sprintf( 'SP\Modules\%s\Controllers\%sController', @@ -227,6 +280,11 @@ final class Bootstrap ); } + protected function initializePluginClasses(): void + { + PluginManager::getPlugins(); + } + /** * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface @@ -280,19 +338,23 @@ final class Bootstrap // Establecer las cabeceras de autentificación para apache+php-cgi // Establecer las cabeceras de autentificación para que apache+php-cgi funcione si la variable es renombrada por apache if (($server->get('HTTP_AUTHORIZATION') !== null - && preg_match( - '/Basic\s+(.*)$/i', - $server->get('HTTP_AUTHORIZATION'), - $matches)) + && preg_match( + '/Basic\s+(.*)$/i', + $server->get('HTTP_AUTHORIZATION'), + $matches + )) || ($server->get('REDIRECT_HTTP_AUTHORIZATION') !== null && preg_match( '/Basic\s+(.*)$/i', $server->get('REDIRECT_HTTP_AUTHORIZATION'), - $matches)) + $matches + )) ) { - [$name, $password] = explode(':', + [$name, $password] = explode( + ':', base64_decode($matches[1]), - 2); + 2 + ); $server->set('PHP_AUTH_USER', strip_tags($name)); $server->set('PHP_AUTH_PW', strip_tags($password)); @@ -305,21 +367,23 @@ final class Bootstrap public function initPHPVars(): void { if (defined('DEBUG') && DEBUG) { - /** @noinspection ForgottenDebugOutputInspection */ - Debug::enable(); - } else if (!defined('DEBUG') - && ($this->router->request()->cookies()->get('XDEBUG_SESSION') - || $this->configData->isDebug()) - ) { - define('DEBUG', true); - /** @noinspection ForgottenDebugOutputInspection */ Debug::enable(); } else { - error_reporting(E_ALL & ~(E_DEPRECATED | E_STRICT | E_NOTICE)); + if (!defined('DEBUG') + && ($this->router->request()->cookies()->get('XDEBUG_SESSION') + || $this->configData->isDebug()) + ) { + define('DEBUG', true); - if (!headers_sent()) { - ini_set('display_errors', 0); + /** @noinspection ForgottenDebugOutputInspection */ + Debug::enable(); + } else { + error_reporting(E_ALL & ~(E_DEPRECATED | E_STRICT | E_NOTICE)); + + if (!headers_sent()) { + ini_set('display_errors', 0); + } } } @@ -327,7 +391,7 @@ final class Bootstrap && touch(LOG_FILE) && chmod(LOG_FILE, 0600) ) { - logger('Setup log file: ' . LOG_FILE); + logger('Setup log file: '.LOG_FILE); } if (date_default_timezone_get() === 'UTC') { @@ -348,7 +412,7 @@ final class Bootstrap */ private function initPaths(): void { - self::$SUBURI = '/' . basename($this->request->getServer('SCRIPT_FILENAME')); + self::$SUBURI = '/'.basename($this->request->getServer('SCRIPT_FILENAME')); $uri = $this->request->getServer('REQUEST_URI'); @@ -358,7 +422,7 @@ final class Bootstrap self::$WEBROOT = substr($uri, 0, $pos); } - self::$WEBURI = $this->request->getHttpHost() . self::$WEBROOT; + self::$WEBURI = $this->request->getHttpHost().self::$WEBROOT; } /** @@ -406,132 +470,105 @@ final class Bootstrap } } - private function manageWebRequest(): Closure + /** + * Handle the request through the router + * + * @return void + */ + private function handleRequest(): void + { + $this->router->dispatch($this->request->getRequest()); + } + + /** + * @param \Psr\Container\ContainerInterface $container + * + * @return \SP\Bootstrap + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface + * + * TODO: Inject needed classes + */ + public static function runApi(ContainerInterface $container): Bootstrap + { + logger('------------'); + logger('Boostrap:api'); + + // TODO: remove + self::$container = $container; + + /** @noinspection SelfClassReferencingInspection */ + $bs = $container->get(Bootstrap::class); + $bs->module = $container->get(InitApi::class); + $bs->configureRouterForApi(); + $bs->handleRequest(); + + return $bs; + } + + private function configureRouterForApi() + { + // Manage requests for api module + $this->router->respond( + 'POST', + '@/api\.php', + $this->manageApiRequest() + ); + } + + private function manageApiRequest(): Closure { return function ($request, $response, $service) { - /** @var \Klein\Request $request */ - /** @var \Klein\Response $response */ - try { - logger('WEB route'); + logger('API route'); - /** @var \Klein\Request $request */ - $route = Filter::getString($request->param('r', 'index/index')); + $apiRequest = self::$container->get(ApiRequest::class); - if (!preg_match_all( - '#(?P[a-zA-Z]+)(?:/(?P[a-zA-Z]+))?(?P(/[a-zA-Z\d.]+)+)?#', - $route, - $matches - )) { - throw new RuntimeException(self::OOPS_MESSAGE); + [$controllerName, $action] = explode('/', $apiRequest->getMethod()); + + $controllerClass = self::getClassFor($controllerName); + + $method = $action.'Action'; + + if (!method_exists($controllerClass, $method)) { + logger($controllerClass.'::'.$method); + + /** @var Response $response */ + $response->headers() + ->set( + 'Content-type', + 'application/json; charset=utf-8' + ); + + return $response->body( + JsonRpcResponse::getResponseError( + self::OOPS_MESSAGE, + JsonRpcResponse::METHOD_NOT_FOUND, + $apiRequest->getId() + ) + ); } - $controllerName = $matches['controller'][0]; - $methodName = empty($matches['action'][0]) - ? 'indexAction' - : $matches['action'][0] . 'Action'; - $methodParams = empty($matches['params'][0]) - ? [] - : Filter::getArray(explode( - '/', - trim($matches['params'][0], - '/') - )); - - $controllerClass = self::getControllerClass($controllerName); - - $this->initializePluginClasses(); - - if (!method_exists($controllerClass, $methodName)) { - logger($controllerClass . '::' . $methodName); - - $response->code(404); - - throw new RuntimeException(self::OOPS_MESSAGE); - } - - $this->setCors($response); - $this->initializeCommon(); - if (APP_MODULE === 'web') { - self::$container->get(InitWeb::class) - ->initialize($controllerName); - } + $this->module->initialize($controllerName); - logger(sprintf( - 'Routing call: %s::%s::%s', - $controllerClass, - $methodName, - print_r($methodParams, true) - )); + logger('Routing call: '.$controllerClass.'::'.$method); - $controller = new $controllerClass(self::$container, $methodName); - - return call_user_func_array([$controller, $methodName], $methodParams); - } catch (SessionTimeout $sessionTimeout) { - logger('Session timeout'); + return call_user_func([new $controllerClass(self::$container, $method), $method]); } catch (\Exception $e) { processException($e); /** @var Response $response */ - if ($response->status()->getCode() !== 404) { - $response->code(503); - } + $response->headers()->set('Content-type', 'application/json; charset=utf-8'); - return __($e->getMessage()); + return $response->body(JsonRpcResponse::getResponseException($e, 0)); + } finally { + $this->router->skipRemaining(); } }; } - protected function initializePluginClasses(): void - { - PluginManager::getPlugins(); - } - - public static function getContainer(): ContainerInterface - { - return self::$container; - } - - /** - * @throws InitializationException - * @throws DependencyException - * @throws NotFoundException - * @throws Core\Context\ContextException - */ - public static function run( - Container $container, - string $module = APP_MODULE - ): void - { - switch ($module) { - case 'web': - logger('------------'); - logger('Boostrap:web'); - - $bs = new Bootstrap($container); - $bs->router->dispatch($bs->request->getRequest()); - break; - case 'api': - logger('------------'); - logger('Boostrap:api'); - - $bs = new Bootstrap($container); - $bs->router->dispatch($bs->request->getRequest()); - break; - case 'cli': - logger('------------'); - logger('Boostrap:cli'); - - $cli = $container->get(InitCli::class); - $cli->initialize(''); - break; - default; - throw new InitializationException('Unknown module'); - } - } - public function getRouter(): Klein { return $this->router;