diff --git a/src/AbstractKernel.php b/src/AbstractKernel.php index 5e5ebda..332e6b2 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,14 +43,16 @@ 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::setDefaultTimezone($this->config['configs']['timezone']); + if(App::get()->getApp('locale') !== null) { + Clock::setDefaultLocale(App::get()->getApp('locale')); + } + if(App::get()->getApp('timezone') !== null) { + Clock::setDefaultTimezone(App::get()->getApp('timezone')); + } } /** @@ -62,39 +65,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..9be4499 100644 --- a/src/App.php +++ b/src/App.php @@ -14,11 +14,14 @@ 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; + // Renamed configs to app + $this->app = $this->config['app'] ?? ($this->config['configs'] ?? []); } /** @@ -58,7 +61,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 +71,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 +81,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 +91,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 +101,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 +133,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/CliKernel.php b/src/CliKernel.php index 623fd71..f18da6f 100644 --- a/src/CliKernel.php +++ b/src/CliKernel.php @@ -43,7 +43,7 @@ public function __construct(string $dir) { parent::__construct($dir); // Default config - Kernel::setRouterFilePath($dir . "/routers/console.php"); + Kernel::setRouterFilePath(App::get()->dir()->routes() . "/console.php"); $this->stream = new Stream(Stream::STDERR); } diff --git a/src/HttpKernel.php b/src/HttpKernel.php index 7fcaac4..558f1eb 100644 --- a/src/HttpKernel.php +++ b/src/HttpKernel.php @@ -23,12 +23,12 @@ final class HttpKernel extends AbstractKernel implements KernelLoadInterface public function __construct(string $dir) { parent::__construct($dir); - Kernel::setRouterFilePath($dir . "/routers/web.php"); + Kernel::setRouterFilePath(App::get()->dir()->routes() . "/web.php"); $this->stream = new Stream(Stream::TEMP); } /** - * Initialize the HTTP kernel with default framework configuration. + * Initialize the HTTP kernel with the default framework configuration. * * This method loads HTTP-related configuration from `/configs/http.php` * and registers globally configured middleware. It also attaches the 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