move default configurations to Engine::vars definition

v4
fadrian06 16 hours ago
parent b9b70ac2ae
commit 60dffbc6d9

@ -89,14 +89,22 @@ class Engine
]; ];
/** @var array<string, mixed> */ /** @var array<string, mixed> */
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 Loader $loader;
protected Dispatcher $dispatcher; protected Dispatcher $dispatcher;
protected EventDispatcher $eventDispatcher; protected EventDispatcher $eventDispatcher;
/** If the framework has been initialized or not */ /** If the framework has been initialized or not */
protected bool $initialized = false; protected bool $initialized = true;
/** If the request has been handled or not */ /** If the request has been handled or not */
protected bool $requestHandled = false; protected bool $requestHandled = false;
@ -138,20 +146,17 @@ class Engine
/** Initializes the framework */ /** Initializes the framework */
public function init(): void 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 // Register default components
$this->loader->register('eventDispatcher', EventDispatcher::class); $this->loader->register('eventDispatcher', EventDispatcher::class);
$this->loader->register('request', Request::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 { $this->loader->register('view', View::class, [], function (View $view): void {
$view->path = $this->get('flight.views.path'); $view->path = $this->get('flight.views.path');
@ -162,43 +167,22 @@ class Engine
$this->dispatcher->set($name, [$this, "_$name"]); $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 // Enable error handling
if ($this->get('flight.handle_errors')) { if ($this->get('flight.handle_errors')) {
set_error_handler([$this, 'handleError']); set_error_handler([$this, 'handleError']);
set_exception_handler([$this, 'handleException']); 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;
} }
/** /**
* Custom error handler. Converts errors into exceptions. * Custom error handler. Converts errors into exceptions.
* *
* @param int $errno Error number * @param int $errno Level of the error raised.
* @param string $errstr Error string * @param string $errstr Error message.
* @param string $errfile Error file name * @param string $errfile Filename that the error was raised in.
* @param int $errline Error file line number * @param int $errline Line number where the error was raised.
*
* @return false
* @throws ErrorException * @throws ErrorException
* @return false
*/ */
public function handleError(int $errno, string $errstr, string $errfile, int $errline): bool public function handleError(int $errno, string $errstr, string $errfile, int $errline): bool
{ {
@ -209,24 +193,20 @@ class Engine
return false; return false;
} }
/** /** Custom exception handler. Logs exceptions */
* Custom exception handler. Logs exceptions. public function handleException(Throwable $ex): void
*
* @param Throwable $e Thrown exception
*/
public function handleException(Throwable $e): void
{ {
if ($this->get('flight.log_errors')) { 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 * Registers the container handler
* @template T of object * @template T of object
* @param ContainerInterface|callable(class-string<T>, array<int|string, mixed>): ?T $containerHandler * @param ContainerInterface|callable(class-string<T>): T $containerHandler
* Callback function or PSR-11 Container object that sets the container and how it will inject classes * Callback function or PSR-11 Container object that sets the container and how it will inject classes
*/ */
public function registerContainerHandler($containerHandler): void public function registerContainerHandler($containerHandler): void
@ -240,11 +220,17 @@ class Engine
*/ */
public function map(string $name, callable $callback): void 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.'); 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 public function register(string $name, string $class, array $params = [], ?callable $callback = null): void
{ {
if (method_exists($this, $name)) { $this->ensureMethodNotExists($name)->loader->register($name, $class, $params, $callback);
throw new Exception('Cannot override an existing framework method.');
}
$this->loader->register($name, $class, $params, $callback);
} }
/** Unregisters a class to a framework method */ /** Unregisters a class to a framework method */

@ -62,12 +62,9 @@ class DocExamplesTest extends TestCase
Flight::request()->method = 'GET'; Flight::request()->method = 'GET';
Flight::request()->url = '/'; Flight::request()->url = '/';
$router->get( $router->get('/', static function () {
'/',
function () {
Flight::response()->write('from resp '); Flight::response()->write('from resp ');
} });
);
Flight::start(); Flight::start();

@ -134,26 +134,6 @@ class EngineTest extends TestCase
$engine->start(); $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('<h1>404 Not Found</h1><h3>The page you have requested could not be found.</h3>');
$engine->start();
}
public function testDoubleReturnTrueRoutesContinueIteration(): void public function testDoubleReturnTrueRoutesContinueIteration(): void
{ {
$_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_METHOD'] = 'GET';

@ -21,7 +21,6 @@ class RenderTest extends TestCase
public function testRenderView(): void public function testRenderView(): void
{ {
$this->app->render('hello', ['name' => 'Bob']); $this->app->render('hello', ['name' => 'Bob']);
$this->expectOutputString('Hello, Bob!'); $this->expectOutputString('Hello, Bob!');
} }

Loading…
Cancel
Save