diff --git a/public/index.php b/public/index.php index 78fef51..e15604b 100644 --- a/public/index.php +++ b/public/index.php @@ -8,8 +8,7 @@ error_reporting(E_ALL); use App\Kernel; -require_once dirname(__DIR__).'/vendor/autoload.php'; +require_once dirname(__DIR__) . '/vendor/autoload.php'; -$kernel = new Kernel(); - -$kernel(); \ No newline at end of file +$kernel = new Kernel(__DIR__ . '/../app'); +$kernel->run(); \ No newline at end of file diff --git a/src/AppConfig.php b/src/AppConfig.php new file mode 100644 index 0000000..bcf0ee6 --- /dev/null +++ b/src/AppConfig.php @@ -0,0 +1,17 @@ +<?php + +namespace Ardent\Undercurrent; + +class AppConfig +{ + public function __construct( + private readonly string $rootPath, + ) + { + } + + public function getRootPath(): string + { + return $this->rootPath; + } +} \ No newline at end of file diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php index 60164d8..c27f49a 100644 --- a/src/Kernel/BaseKernel.php +++ b/src/Kernel/BaseKernel.php @@ -2,6 +2,7 @@ namespace Ardent\Undercurrent\Kernel; +use Ardent\Undercurrent\AppConfig; use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Container\GenericContainer; use Ardent\Undercurrent\Http\GenericRequest; @@ -13,18 +14,26 @@ use Ardent\Undercurrent\Http\RouterInterface; use Ardent\Undercurrent\Http\StatusEnum; use Ardent\Undercurrent\Logger\LogContainer; use Ardent\Undercurrent\Logger\LoggerInterface; -use Exception; class BaseKernel { - public function __invoke(): void + public function __construct( + private readonly string $rootDirectory, + ) { + } + + public function run(): void + { + $appConfig = new AppConfig($this->rootDirectory); + $container = (new GenericContainer()); $container ->alias(RouterInterface::class, GenericRouter::class) ->alias(ContainerInterface::class, GenericContainer::class) ->alias(LoggerInterface::class, LogContainer::class) ->add(GenericContainer::class, fn($container) => $container) + ->add(AppConfig::class, fn() => $appConfig) ->add(GenericRouter::class) ->add(LogContainer::class);
92 lines
2.8 KiB
PHP
92 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Ardent\Undercurrent\Kernel;
|
|
|
|
use Ardent\Undercurrent\AppConfig;
|
|
use Ardent\Undercurrent\Container\ContainerInterface;
|
|
use Ardent\Undercurrent\Container\GenericContainer;
|
|
use Ardent\Undercurrent\Http\GenericRequest;
|
|
use Ardent\Undercurrent\Http\GenericResponse;
|
|
use Ardent\Undercurrent\Http\GenericRouter;
|
|
use Ardent\Undercurrent\Http\MethodEnum;
|
|
use Ardent\Undercurrent\Http\RouterConfig;
|
|
use Ardent\Undercurrent\Http\RouterInterface;
|
|
use Ardent\Undercurrent\Http\StatusEnum;
|
|
use Ardent\Undercurrent\Logger\LogContainer;
|
|
use Ardent\Undercurrent\Logger\LoggerInterface;
|
|
|
|
class BaseKernel
|
|
{
|
|
public function __construct(
|
|
private readonly string $rootDirectory,
|
|
)
|
|
{
|
|
}
|
|
|
|
public function run(): void
|
|
{
|
|
$appConfig = new AppConfig($this->rootDirectory);
|
|
|
|
$container = (new GenericContainer());
|
|
$container
|
|
->alias(RouterInterface::class, GenericRouter::class)
|
|
->alias(ContainerInterface::class, GenericContainer::class)
|
|
->alias(LoggerInterface::class, LogContainer::class)
|
|
->add(GenericContainer::class, fn($container) => $container)
|
|
->add(AppConfig::class, fn() => $appConfig)
|
|
->add(GenericRouter::class)
|
|
->add(LogContainer::class);
|
|
|
|
$this->dependencies($container);
|
|
|
|
$this->render($container);
|
|
}
|
|
|
|
private function render(GenericContainer $container): void
|
|
{
|
|
$request = new GenericRequest(
|
|
MethodEnum::from($_SERVER['REQUEST_METHOD']),
|
|
$_SERVER['REQUEST_URI'],
|
|
$_REQUEST,
|
|
);
|
|
$router = $container->get(RouterInterface::class);
|
|
$log = $container->get(LogContainer::class);
|
|
try {
|
|
$response = $router->dispatch($request);
|
|
} catch (\Throwable $e) {
|
|
$response = new GenericResponse(
|
|
$e->getMessage(),
|
|
StatusEnum::NOT_FOUND
|
|
);
|
|
//$response = $container->get(RouterConfig::class)->getExceptionRoute()->getController()::exception($e);
|
|
}
|
|
|
|
foreach ($log->getLogs() as $log) {
|
|
echo sprintf('<p>%s</p>', $log);
|
|
}
|
|
|
|
http_response_code($response->getStatus()->value);
|
|
|
|
foreach ($response->getHeaders() as $header) {
|
|
header($header);
|
|
}
|
|
|
|
echo $response->getBody();
|
|
}
|
|
|
|
protected function addControllers(ContainerInterface $container, array $controllers): void
|
|
{
|
|
$config = new RouterConfig();
|
|
|
|
foreach ($controllers as $controller) {
|
|
$container->add($controller);
|
|
$config->addController($controller);
|
|
}
|
|
|
|
$container->add(RouterConfig::class, fn() => $config);
|
|
}
|
|
|
|
protected function dependencies(ContainerInterface $container): void
|
|
{
|
|
}
|
|
} |