projectPath = $projectPath; $this->setEnvironment(); $this->initializeEngine(); $this->setupContainer(); $this->serviceContainer(); $this->handleRequest(); } public static function putenv($var, $value) { putenv("$var=$value"); $_ENV[$var] = $value; } protected function setEnvironment() { static::putenv('PROJECT_PATH', $this->projectPath); // Environment vars (accessible from \DI\env(), getenv(), $_ENV and $_SERVER) Dotenv::create(getenv("PROJECT_PATH"))->load(); // Override using headers if ( ( $keys = getenv('keys') ) && ( $auth = $_SERVER["HTTP_X_DEV_AUTH"] ) && in_array($auth, explode(',', getenv('keys'))) ) { foreach (['APP_ENV', 'DEBUG',] as $env) { if (null !== $value = $_SERVER["HTTP_X_$env"] ?? null) { static::putenv($env, $value); } } } // Paths and directories foreach($this->paths as $name => $envkey) { if ( ! getenv($name) ) { $path = getenv("PROJECT_PATH") . DIRECTORY_SEPARATOR . getenv($envkey); if (getenv('DEBUG') && ! file_exists($path)) { mkdir($path, 0755, true); } static::putenv($name, realpath($path)); } } } protected function initializeEngine() : self { # Defining default locale and timezone date_default_timezone_set(getenv("DEFAULT_TIMEZONE")); setlocale(LC_ALL, $this->locale = getenv("DEFAULT_LOCAL")); setlocale(LC_TIME, getenv("DEFAULT_TIME"), getenv("DEFAULT_TIME_FALLBACK")); if ( class_exists('Locale') ) { \Locale::setDefault($this->locale); } ini_set("log_errors", "1"); ini_set("error_log", $this->errorLogPath); ini_set('display_errors', getenv("DEBUG") ? 'on' : 'on'); error_reporting($this->errorReporting); return $this; } protected function setupContainer() : self { $containerBuilder = new ContainerBuilder(); if (getenv("APP_ENV") === "prod") { if (getenv("CACHE_PATH")) { # $containerBuilder->enableCompilation(getenv("CACHE_PATH") . "/di/"); # $containerBuilder->writeProxiesToFile(true); } } $containerBuilder->useAnnotations(false); if ($this->definitionFilePath ?? false) { $containerBuilder->addDefinitions(require($this->definitionFilePath)); } $this->container = $containerBuilder->build(); return $this; } protected function serviceContainer() : self { $this->container->has(AdapterProxy::class) and $this->container->get(AdapterProxy::class); $this->container->has(Lean::class) and $this->container->get(Lean::class); if ($this->container->has(I18n::class)) { $i18n = $this->container->get(I18n::class); $i18n->locale($this->locale); $i18n->initialize(!getenv("DEBUG")); } return $this; } protected function handleRequest() : self { ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES); // Router $routeFactory = $this->container->get($this->routeDefinitionList); $router = $routeFactory($this->container); $router->setStrategy($this->container->get(ApplicationStrategy::class)); // Handle requests $response = $router->dispatch($this->container->get(ServerRequestInterface::class)); // Reply to clients $this->container->get(EmitterInterface::class)->emit($response); return $this; } };