isDebug()) { Debugger::enable(Debugger::Development); Debugger::$strictMode = true; } else { Debugger::enable(Debugger::Production); Debugger::$strictMode = E_ALL & ~E_DEPRECATED; } Debugger::$logDirectory = $logDir; if (Debugger::$showBar === true && PHP_SAPI !== 'cli' && class_exists(TracyExtensionLoader::class)) { new TracyExtensionLoader($app); } /********************************************** * Flight Database Setup * **********************************************/ $db = null; if (DBFactory::isEnabled($config)) { $db = DBFactory::create($config); $app->map('db', function () use ($db) { return $db; }); } /********************************************** * Flight Session Setup * **********************************************/ $sessionOptions = [ 'prefix' => (string) $config->get('session.prefix', 'flight_sess_'), 'encryption_key' => (string) $config->get('session.encryption_key', md5($projectRoot)), 'auto_commit' => true, ]; $savePath = $config->get('session.save_path'); if ($savePath !== null && $savePath !== '') { $sessionOptions['save_path'] = (string) $savePath; } if (PHP_SAPI === 'cli') $sessionOptions['start_session'] = false; $session = new Session($sessionOptions); $app->map('session', function () use ($session) { return $session; }); /********************************************** * User Util Setup * **********************************************/ $user = new User($app); $app->map('user', function () use ($user) { return $user; }); /********************************************** * Setup Twig as Default Render * **********************************************/ if (!defined('RUNWAY_CMD')) { $viewsPath = $projectRoot . $ds . 'app' . $ds . 'views'; $twigCache = $projectRoot . $ds . 'temp' . $ds . 'twig'; if (!is_dir($twigCache)) mkdir($twigCache, 0775, true); $loader = new FilesystemLoader($viewsPath); $twig = new Environment($loader, [ 'cache' => $config->isDebug() ? false : $twigCache, 'debug' => $config->isDebug(), 'auto_reload' => $config->isDebug(), ]); $twig->addGlobal('csp_nonce', $app->get('csp_nonce')); $app->map('render', function (string $template, array $data = []) use ($app, $twig) { if (substr($template, -5) !== '.twig') $template .= '.twig'; $app->response()->write($twig->render($template, $data)); }); } /********************************************** * Dice + Engine substitutions * **********************************************/ if (!defined('RUNWAY_CMD')) { $container = new Dice(); $substitutions = [ Engine::class => $app, Config::class => $config, Session::class => $session, ]; if ($db instanceof SimplePdo) $substitutions[SimplePdo::class] = $db; $container = $container->addRule('*', [ 'substitutions' => $substitutions, ]); $container = $container->addRule(Config::class, ['shared' => true]); $container = $container->addRule(Session::class, ['shared' => true]); if ($db instanceof SimplePdo) $container = $container->addRule(SimplePdo::class, ['shared' => true]); $app->registerContainerHandler(function ($class, $params) use ($container) { return $container->create($class, $params); }); // Helper for non-route code: $app->make(SomeClass::class) $app->map('make', function ($class, $params = []) use ($container) { return $container->create($class, $params); }); } /********************************************** * Third-Party Integrations * **********************************************/ // Google OAuth Example: // $app->register('google_oauth', Google_Client::class, [ $config['google_oauth'] ]); // Redis Example: // $app->register('redis', Redis::class, [ $config['redis']['host'], $config['redis']['port'] ]); // Add more service registrations below as needed