diff --git a/app/Application.php b/app/Application.php deleted file mode 100644 index 46d8c93..0000000 --- a/app/Application.php +++ /dev/null @@ -1,14 +0,0 @@ -handle(); \ No newline at end of file +// Load routes +require '../config/routes.php'; + +// Load env +Env::load(); + +// Dispatch router +Router::dispatch(); \ No newline at end of file diff --git a/src/Bootstrap.php b/src/Bootstrap.php deleted file mode 100644 index f434d27..0000000 --- a/src/Bootstrap.php +++ /dev/null @@ -1,45 +0,0 @@ -run(); - - return Bootstrap::getInstance(); - } - - /** - * Get the instance - * - * @return \Core\Factory\BootstrapFactory - */ - public static function getInstance(): BootstrapFactory - { - return self::$instance; - } - - /** - * Create new factory instance - * - * @return \Core\Factory\BootstrapFactory - */ - protected function run(): BootstrapFactory - { - return new BootstrapFactory(); - } -} \ No newline at end of file diff --git a/src/Http/Controllers/Controller.php b/src/Controllers/Controller.php similarity index 93% rename from src/Http/Controllers/Controller.php rename to src/Controllers/Controller.php index d428fcd..e8f1cec 100644 --- a/src/Http/Controllers/Controller.php +++ b/src/Controllers/Controller.php @@ -1,6 +1,6 @@ is('post')) { + if (Env::get('debug')) { + if ($request?->is('post')) { return new JsonResponseHandler(); } @@ -58,44 +60,24 @@ class ExceptionHandler /** * Catch all exceptions * + * @param \Core\Http\Request $request * @return void */ - public static function catch(): void + public static function catch(Request $request): void { - self::instance()->register(); + self::instance($request)->register(); } /** * Catch single exception * * @param \Throwable $exception - * @return never + * @return void */ - public static function catchOne(Throwable $exception): never + public static function catchOne(Throwable $exception): void { self::instance()->handleException($exception); exit(0); } - - /** - * Make new exception - * - * @param null $abstract - * @param string|null $message - * @return never - */ - public static function make(mixed $abstract = null, string|null $message = null): never - { - if(is_string($abstract)) { - $abstract = app()->make($abstract, $message); - } - - if(is_subclass_of($abstract, 'Exception')) { - self::catchOne($abstract); - } - - exit(0); - } - } \ No newline at end of file diff --git a/src/Exceptions/Exceptions/ClassNotFoundException.php b/src/Exceptions/Exceptions/ClassNotFoundException.php deleted file mode 100644 index 494b2b4..0000000 --- a/src/Exceptions/Exceptions/ClassNotFoundException.php +++ /dev/null @@ -1,5 +0,0 @@ -request = app()->make(Request::class, [$_POST + $_FILES]); - - // Capture all exceptions - ExceptionHandler::catch(); - - // Load routes - require '../config/routes.php'; - - try { - // Boot application - app()->make(Application::class)->bootstrap(); - - // Dispatch router - app()->make(RouteDispatcher::class)->dispatch($this->request, RouteCollection::retrieve()); - } catch (\Exception $e) { - ExceptionHandler::catchOne($e); - } - } - - /** - * @param string $abstract - * @param array $arguments - * @return mixed - */ - public function make(string $abstract, mixed $arguments = []): mixed - { - return $this->resolve($abstract, $arguments); - } - - /** - * @param string $abstract - * @param array $arguments - * @return mixed|null - */ - private function resolve(string $abstract, mixed $arguments = []): mixed - { - if (class_exists($abstract)) { - try { - $reflection = new \ReflectionClass($abstract); - return $reflection->newInstanceArgs($arguments); - } catch (\ReflectionException $e) { - ExceptionHandler::catchOne($e); - } - } - - ExceptionHandler::make(ClassNotFoundException::class, sprintf("Class '%s' not found", $abstract)); - } - - /** - * Get the request instance - * - * @return \Core\Http\Request - */ - public function request(): Request - { - return $this->request; - } - - /** - * Get path to file/folder in resources folder - * - * @param string $path - * @return string - */ - public function resourcePath(string $path = ''): string - { - return "../resources/" . $path; - } -} \ No newline at end of file diff --git a/src/Helpers/helpers.php b/src/Helpers/helpers.php deleted file mode 100644 index 9d3c5c4..0000000 --- a/src/Helpers/helpers.php +++ /dev/null @@ -1,51 +0,0 @@ -make($abstract, $arguments); - } -} - -if(!function_exists('env')) -{ - /** - * Get env variable - * - * @param string $key - * @return bool - */ - function env(string $key): mixed - { - return Env::get($key); - } -} - -if (!function_exists('request')) { - /** - * Get the request instance - * - * @return \Core\Http\Request - */ - function request(): Request - { - return app()->request(); - } -} diff --git a/src/Http/Request.php b/src/Http/Request.php index 7422c90..8bbe2d9 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -2,6 +2,8 @@ namespace Core\Http; +use Core\Exceptions\Exceptions; + class Request { private array $data = []; @@ -14,6 +16,9 @@ class Request public function __construct(array $data) { $this->data = $data; + + // Capture all exceptions + Exceptions::catch($this); } /** diff --git a/src/Http/Response.php b/src/Http/Response.php index bb5d38d..99d4fcb 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -2,9 +2,9 @@ namespace Core\Http; -use Core\Http\View\Engine\HtmlEngine; -use Core\Http\View\Engine\JsonEngine; -use Core\Http\View\Render; +use Core\View\Render; +use Core\View\Render\HtmlRender; +use Core\View\Render\JsonRender; class Response { @@ -25,20 +25,20 @@ class Response * Render HTML * * @param string $view - * @return \Core\Http\View\Render + * @return \Core\View\Render */ public function view(string $view): Render { - return (new HtmlEngine())->view($view); + return (new HtmlRender())->view($view); } /** * Render JSON * - * @return \Core\Http\View\Render + * @return \Core\View\Render */ public function json(): Render { - return new JsonEngine(); + return new JsonRender(); } } \ No newline at end of file diff --git a/src/Routing/RouteDispatcher.php b/src/Routing/RouteDispatcher.php index 9106021..b700682 100644 --- a/src/Routing/RouteDispatcher.php +++ b/src/Routing/RouteDispatcher.php @@ -2,10 +2,11 @@ namespace Core\Routing; -use Core\Exceptions\ExceptionHandler; +use Core\Env\Env; +use Core\Exceptions\Exceptions; use Core\Exceptions\Exceptions\NotFoundHttpException; use Core\Http\Request; -use Core\Http\View\Engine; +use Core\View\Render; use Exception; class RouteDispatcher @@ -25,13 +26,10 @@ class RouteDispatcher private array $routeCollection; /** - * Dispatch the router dispatcher - * * @param \Core\Http\Request $request * @param array $routeCollection - * @return void */ - public function dispatch(Request $request, array $routeCollection): void + public function __construct(Request $request, array $routeCollection) { $this->request = $request; $this->routeCollection = $routeCollection; @@ -50,6 +48,18 @@ class RouteDispatcher } } + /** + * Dispatch the router dispatcher + * + * @param \Core\Http\Request $request + * @param array $routeCollection + * @return void + */ + public static function dispatch(Request $request, array $routeCollection): void + { + new self($request, $routeCollection); + } + /** * Locate a matching route for the incoming request. * @@ -143,8 +153,8 @@ class RouteDispatcher */ private function handleException(Exception $e, int $statusCode, string $message): void { - if (env('debug')) { - ExceptionHandler::catchOne($e); + if (Env::get('debug')) { + Exceptions::catchOne($e); } http_response_code($statusCode); diff --git a/src/Http/View/Render.php b/src/View/Render.php similarity index 97% rename from src/Http/View/Render.php rename to src/View/Render.php index bf61d90..fc1608a 100644 --- a/src/Http/View/Render.php +++ b/src/View/Render.php @@ -1,6 +1,6 @@ resourcePath('views/' . str_replace('.', '/', $this->view) . '.php'); + $viewsPath = $basePath . '/../resources/views/' . str_replace('.', '/', $this->view) . '.php'; if (file_exists($viewsPath)) { extract($this->data); diff --git a/src/Http/View/Engine/JsonEngine.php b/src/View/Render/JsonRender.php similarity index 70% rename from src/Http/View/Engine/JsonEngine.php rename to src/View/Render/JsonRender.php index f97d63c..3dc68b5 100644 --- a/src/Http/View/Engine/JsonEngine.php +++ b/src/View/Render/JsonRender.php @@ -1,11 +1,10 @@