From c826d6b15a71b405e0078e99de7df9f0a98c0d57 Mon Sep 17 00:00:00 2001 From: Daniel Ronkainen Date: Sat, 28 Mar 2026 15:08:12 +0100 Subject: [PATCH 1/3] feat: add interface wiring and consistent app config --- src/AbstractKernel.php | 63 ++++++++++++++++++++--------- src/App.php | 33 ++++++++++++--- src/Providers/DatabaseProvider.php | 3 +- src/Render/Errors/TestPage.php | 27 +++++++++++++ src/Render/Errors/TwigErrorPage.php | 5 +-- src/Render/StaticRenderer.php | 3 +- src/Render/Templates/welcome.php | 8 ++-- src/Routing/DefaultController.php | 16 ++++---- src/Support/Twig.php | 6 +-- 9 files changed, 114 insertions(+), 50 deletions(-) create mode 100644 src/Render/Errors/TestPage.php diff --git a/src/AbstractKernel.php b/src/AbstractKernel.php index 5e5ebda..5fbe95d 100644 --- a/src/AbstractKernel.php +++ b/src/AbstractKernel.php @@ -4,6 +4,7 @@ namespace MaplePHP\Core; +use MaplePHP\Container\Autowire; use MaplePHP\Core\Configs\LoadConfigFiles; use MaplePHP\Core\Support\ServiceProvider; use MaplePHP\DTO\Format\Clock; @@ -42,13 +43,11 @@ public function __construct(string $dir) ->loadFiles($this->dir . "/configs/"); $this->config = $config->fetch(); - $app = App::boot(new Dir($this->dir), $this->config); $this->container = new Container(); - $this->container->set("config", $this->config); $this->container->set("app", $app); - Clock::setDefaultLocale($this->config['configs']['locale']); + Clock::setDefaultLocale(App::get()->getApp('locale')); Clock::setDefaultTimezone($this->config['configs']['timezone']); } @@ -62,39 +61,63 @@ public function __construct(string $dir) */ protected function load(ServerRequestInterface $request, ?DispatchConfigInterface $config = null): KernelInterface { - $this->bootServiceProviders(); + + if (isset($this->config['providers']) && is_array($this->config['providers'])) { + $this->bootServiceProviders($this->config['providers']); + } + + if (isset($this->config['services']['providers']) && is_array($this->config['services']['providers'])) { + $this->bootServiceProviders($this->config['services']['providers']); + } + + if (isset($this->config['services']['bindings']) && is_array($this->config['services']['bindings'])) { + $this->bootBindings($this->config['services']['bindings']); + } + return new Kernel($this->container, $this->middlewares, $config); } + + public function bootBindings(array $bindings): void + { + Autowire::interfaceWiring($bindings); + } + /** * Boot service providers * * @return void */ - protected function bootServiceProviders() + protected function bootServiceProviders(array $providers) { - if (isset($this->config['providers'])) { - $providers = []; - - // We want to register first, that way the providers could talk to eachother - // through the container or event listners if you want. - foreach ($this->config['providers'] as $providerClass) { - $provider = new $providerClass(); - if (!($provider instanceof ServiceProvider)) { - throw new \RuntimeException( - "$providerClass is not an instance of " . ServiceProvider::class . "!" - ); - } - $provider->register($this->container); - $providers[] = $provider; + if ($providers !== []) { + $set = []; + + // We want to register first, that way the providers could talk to each other + // through the container or event listeners if you want. + foreach ($providers as $providerClass) { + $this->registerProvider($providerClass, $set); } - foreach ($providers as $provider) { + foreach ($set as $provider) { $provider->boot(); } } } + + private function registerProvider(string $providerClass, array &$providers): void + { + $provider = new $providerClass(); + if (!($provider instanceof ServiceProvider)) { + throw new \RuntimeException( + "$providerClass is not an instance of " . ServiceProvider::class . "!" + ); + } + $provider->register($this->container); + $providers[] = $provider; + } + /** * @param Stream $stream * @return $this diff --git a/src/App.php b/src/App.php index 3568a59..083d923 100644 --- a/src/App.php +++ b/src/App.php @@ -14,11 +14,13 @@ final class App implements AppInterface private Dir $dir; private string $coreDir; private array $config; + private array $app; private function __construct(Dir $dir, array $config = []) { $this->dir = $dir; $this->coreDir = __DIR__; $this->config = $config; + $this->app = $this->config['configs'] ?? []; } /** @@ -58,7 +60,7 @@ public static function get(): self */ public function isProd(): bool { - return ($this->config['configs']['env'] ?? "development") === Environment::PROD->name(); + return ($this->app['env'] ?? "development") === Environment::PROD->name(); } /** @@ -68,7 +70,7 @@ public function isProd(): bool */ public function isStage(): bool { - return ($this->config['configs']['env'] ?? "development") === Environment::STAGE->name(); + return ($this->app['env'] ?? "development") === Environment::STAGE->name(); } /** @@ -78,7 +80,7 @@ public function isStage(): bool */ public function isTest(): bool { - return ($this->config['configs']['env'] ?? "development") === Environment::TEST->name(); + return ($this->app['env'] ?? "development") === Environment::TEST->name(); } /** @@ -88,7 +90,7 @@ public function isTest(): bool */ public function isDev(): bool { - return ($this->config['configs']['env'] ?? "development") === Environment::DEV->name(); + return ($this->app['env'] ?? "development") === Environment::DEV->name(); } /** @@ -98,7 +100,7 @@ public function isDev(): bool */ public function env(): string { - return ($this->config['configs']['env'] ?? "development") ?? Environment::PROD->name(); + return ($this->app['env'] ?? "development") ?? Environment::PROD->name(); } /** @@ -130,4 +132,25 @@ public function configs(): array { return $this->config; } + + /** + * Get the app core configs + * + * @return array + */ + public function app(): array + { + return $this->app; + } + + /** + * Get the app core configs prop + * + * @param string $key + * @return mixed + */ + public function getApp(string $key): mixed + { + return $this->app[$key] ?? null; + } } \ No newline at end of file diff --git a/src/Providers/DatabaseProvider.php b/src/Providers/DatabaseProvider.php index ae3277f..35154dd 100644 --- a/src/Providers/DatabaseProvider.php +++ b/src/Providers/DatabaseProvider.php @@ -4,6 +4,7 @@ namespace MaplePHP\Core\Providers; +use MaplePHP\Core\App; use Psr\Container\ContainerInterface; use Doctrine\DBAL\DriverManager; use MaplePHP\Core\Support\ServiceProvider; @@ -20,7 +21,7 @@ class DatabaseProvider extends ServiceProvider public function register(ContainerInterface $container): void { $this->container = $container; - $config = $container->get('config'); + $config = App::get()->configs(); $default = ($config['database']['default'] ?? null); if (!empty($default)) { $dbConfig = $this->resolveConnection($config); diff --git a/src/Render/Errors/TestPage.php b/src/Render/Errors/TestPage.php new file mode 100644 index 0000000..6441300 --- /dev/null +++ b/src/Render/Errors/TestPage.php @@ -0,0 +1,27 @@ + false, ]); - $configs = App::get()->configs(); - $configs = isset($configs['configs']) ? $configs['configs'] : ['app_title' => "MaplePHP"]; - - $twig->addGlobal('app', $configs); + $twig->addGlobal('app', App::get()->app()); return $twig->render('error.twig', [ 'code' => $response->getStatusCode(), diff --git a/src/Render/StaticRenderer.php b/src/Render/StaticRenderer.php index d974785..de9ff69 100644 --- a/src/Render/StaticRenderer.php +++ b/src/Render/StaticRenderer.php @@ -26,8 +26,7 @@ function welcome(): string private function renderFile(string $fileName, array $data = []) { $response = $this->response; - $configs = App::get()->configs(); - $configs = isset($configs['configs']) ? $configs['configs'] : ['app_title' => "MaplePHP"]; + $app = App::get()->app(); extract($data); diff --git a/src/Render/Templates/welcome.php b/src/Render/Templates/welcome.php index a8561e1..2154d5c 100644 --- a/src/Render/Templates/welcome.php +++ b/src/Render/Templates/welcome.php @@ -3,7 +3,7 @@ - Welcome — MaplePHP + Welcome to MaplePHP