Files
cmasa/app/config/services.php
T
2026-10-29 13:34:31 +07:00

157 lines
5.1 KiB
PHP

<?php
/*******************************************************************
Desc. : Loading Service untuk Aplikasi
Created By : Siau Pheng ([email protected]).
Created Date : November 23rd, 2019.
Last Modified : September 10th, 2026.
(c) 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
use flight\Engine;
use flight\database\SimplePdo;
use flight\debug\database\PdoQueryCapture;
use flight\debug\tracy\TracyExtensionLoader;
use flight\Session;
use Dice\Dice;
use Tracy\Debugger;
use App\Utils\Config;
use App\Utils\DBFactory;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Webapp\App\User;
/**
* Services + Dice DI wiring.
*
* @var Engine $app
* @var string $projectRoot
* @var string $ds
*/
if (!isset($app) || !($app instanceof Engine) || !($config instanceof Config)) {
return;
}
/*********************************************
* Tracy Debugger Setup *
*********************************************/
$logDir = $projectRoot . $ds . 'temp' . $ds . 'log';
if (!is_dir($logDir)) {
mkdir($logDir, 0775, true);
}
if ($config->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