diff --git a/src/Engine.php b/src/Engine.php index f7ad03d..8ecb042 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -89,14 +89,22 @@ class Engine ]; /** @var array */ - protected array $vars = []; + protected array $vars = [ + 'flight.base_url' => null, + 'flight.case_sensitive' => false, + 'flight.handle_errors' => true, + 'flight.log_errors' => false, + 'flight.views.path' => './views', + 'flight.views.extension' => '.php', + 'flight.content_length' => true, + ]; protected Loader $loader; protected Dispatcher $dispatcher; protected EventDispatcher $eventDispatcher; /** If the framework has been initialized or not */ - protected bool $initialized = false; + protected bool $initialized = true; /** If the request has been handled or not */ protected bool $requestHandled = false; @@ -138,20 +146,17 @@ class Engine /** Initializes the framework */ public function init(): void { - if ($this->initialized) { - $this->vars = []; - $this->loader->reset(); - $this->dispatcher->reset(); - } - - // Add this class to Dispatcher - $this->dispatcher->setEngine($this); - // Register default components $this->loader->register('eventDispatcher', EventDispatcher::class); $this->loader->register('request', Request::class); - $this->loader->register('response', Response::class); - $this->loader->register('router', Router::class); + + $this->loader->register('response', Response::class, [], function (Response $response): void { + $response->content_length = $this->get('flight.content_length'); + }); + + $this->loader->register('router', Router::class, [], function (Router $router): void { + $router->caseSensitive = $this->get('flight.case_sensitive'); + }); $this->loader->register('view', View::class, [], function (View $view): void { $view->path = $this->get('flight.views.path'); @@ -162,43 +167,22 @@ class Engine $this->dispatcher->set($name, [$this, "_$name"]); } - // Default configuration settings - $this->set('flight.base_url'); - $this->set('flight.case_sensitive', false); - $this->set('flight.handle_errors', true); - $this->set('flight.log_errors', false); - $this->set('flight.views.path', './views'); - $this->set('flight.views.extension', '.php'); - $this->set('flight.content_length', true); - - // Startup configuration - $this->before('start', function (): void { - // Enable error handling - if ($this->get('flight.handle_errors')) { - set_error_handler([$this, 'handleError']); - set_exception_handler([$this, 'handleException']); - } - - // Set case-sensitivity - $this->router()->caseSensitive = $this->get('flight.case_sensitive'); - - // Set Content-Length - $this->response()->content_length = $this->get('flight.content_length'); - }); - - $this->initialized = true; + // Enable error handling + if ($this->get('flight.handle_errors')) { + set_error_handler([$this, 'handleError']); + set_exception_handler([$this, 'handleException']); + } } /** * Custom error handler. Converts errors into exceptions. * - * @param int $errno Error number - * @param string $errstr Error string - * @param string $errfile Error file name - * @param int $errline Error file line number - * - * @return false + * @param int $errno Level of the error raised. + * @param string $errstr Error message. + * @param string $errfile Filename that the error was raised in. + * @param int $errline Line number where the error was raised. * @throws ErrorException + * @return false */ public function handleError(int $errno, string $errstr, string $errfile, int $errline): bool { @@ -209,24 +193,20 @@ class Engine return false; } - /** - * Custom exception handler. Logs exceptions. - * - * @param Throwable $e Thrown exception - */ - public function handleException(Throwable $e): void + /** Custom exception handler. Logs exceptions */ + public function handleException(Throwable $ex): void { if ($this->get('flight.log_errors')) { - error_log($e->getMessage()); // @codeCoverageIgnore + error_log($ex->getMessage()); } - $this->error($e); + $this->error($ex); } /** * Registers the container handler * @template T of object - * @param ContainerInterface|callable(class-string, array): ?T $containerHandler + * @param ContainerInterface|callable(class-string): T $containerHandler * Callback function or PSR-11 Container object that sets the container and how it will inject classes */ public function registerContainerHandler($containerHandler): void @@ -240,11 +220,17 @@ class Engine */ public function map(string $name, callable $callback): void { - if (method_exists($this, $name)) { + $this->ensureMethodNotExists($name)->dispatcher->set($name, $callback); + } + + /** @throws Exception */ + private function ensureMethodNotExists(string $method): self + { + if (method_exists($this, $method)) { throw new Exception('Cannot override an existing framework method.'); } - $this->dispatcher->set($name, $callback); + return $this; } /** @@ -265,11 +251,7 @@ class Engine */ public function register(string $name, string $class, array $params = [], ?callable $callback = null): void { - if (method_exists($this, $name)) { - throw new Exception('Cannot override an existing framework method.'); - } - - $this->loader->register($name, $class, $params, $callback); + $this->ensureMethodNotExists($name)->loader->register($name, $class, $params, $callback); } /** Unregisters a class to a framework method */ diff --git a/tests/DocExamplesTest.php b/tests/DocExamplesTest.php index 33f92bc..0bdacb0 100644 --- a/tests/DocExamplesTest.php +++ b/tests/DocExamplesTest.php @@ -62,12 +62,9 @@ class DocExamplesTest extends TestCase Flight::request()->method = 'GET'; Flight::request()->url = '/'; - $router->get( - '/', - function () { - Flight::response()->write('from resp '); - } - ); + $router->get('/', static function () { + Flight::response()->write('from resp '); + }); Flight::start(); diff --git a/tests/EngineTest.php b/tests/EngineTest.php index b94bb60..f5d8399 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -134,26 +134,6 @@ class EngineTest extends TestCase $engine->start(); } - public function testStartWithRouteButReturnedValueThrows404V2OutputBuffering(): void - { - $_SERVER['REQUEST_METHOD'] = 'GET'; - $_SERVER['REQUEST_URI'] = '/someRoute'; - - $engine = new class extends Engine { - public function getInitializedVar(): bool - { - return $this->initialized; - } - }; - $engine->set('flight.v2.output_buffering', true); - $engine->route('/someRoute', function () { - echo 'i ran'; - return true; - }, true); - $this->expectOutputString('

404 Not Found

The page you have requested could not be found.

'); - $engine->start(); - } - public function testDoubleReturnTrueRoutesContinueIteration(): void { $_SERVER['REQUEST_METHOD'] = 'GET'; diff --git a/tests/RenderTest.php b/tests/RenderTest.php index ab71965..36391f9 100644 --- a/tests/RenderTest.php +++ b/tests/RenderTest.php @@ -21,7 +21,6 @@ class RenderTest extends TestCase public function testRenderView(): void { $this->app->render('hello', ['name' => 'Bob']); - $this->expectOutputString('Hello, Bob!'); }