diff --git a/.editorconfig b/.editorconfig index 0f4bf43..3cc3219 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,7 +4,7 @@ root = true [*] -indent_style = tab +indent_style = space indent_size = 4 end_of_line = lf charset = utf-8 diff --git a/composer.json b/composer.json index 0f4857e..51d6fe9 100644 --- a/composer.json +++ b/composer.json @@ -31,10 +31,10 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "leafs/http": "^2.1", - "leafs/router": "^0.1.7", - "leafs/anchor": "^1.2", - "leafs/exception": "^3.0" + "leafs/http": "*", + "leafs/router": "*", + "leafs/anchor": "*", + "leafs/exception": "*" }, "require-dev": { "pestphp/pest": "^1.21", diff --git a/src/App.php b/src/App.php index d4f2923..e754104 100755 --- a/src/App.php +++ b/src/App.php @@ -38,12 +38,14 @@ class App extends Router */ public function __construct(array $userSettings = []) { - $this->setupErrorHandler(); - - if (count($userSettings) > 0) { - Config::set($userSettings); + if (class_exists('\Leaf\BareUI')) { + View::attach(\Leaf\BareUI::class, 'template'); } + $this->setupErrorHandler(); + $this->container = new \Leaf\Helpers\Container(); + $this->loadConfig($userSettings); + if (class_exists('\Leaf\Anchor\CSRF')) { if (!Anchor\CSRF::token()) { Anchor\CSRF::init(); @@ -56,21 +58,21 @@ public function __construct(array $userSettings = []) exit(); } } + } - $this->container = new \Leaf\Helpers\Container(); - - $this->setupDefaultContainer(); - - if (class_exists('\Leaf\BareUI')) { - View::attach(\Leaf\BareUI::class, 'template'); + protected function loadConfig(array $userSettings = []) + { + if (count($userSettings) > 0) { + Config::set($userSettings); } + $this->setupDefaultContainer(); $this->loadViewEngines(); } protected function setupErrorHandler() { - if ($this->config('debug') === true) { + if (Anchor::toBool($this->config('debug')) === true) { $debugConfig = [E_ALL, 1]; $this->errorHandler = (new \Leaf\Exception\Run()); $this->errorHandler->register(); @@ -90,27 +92,29 @@ protected function setupErrorHandler() */ public function setErrorHandler($handler, bool $wrapper = true) { - $errorHandler = $handler; + if (Anchor::toBool($this->config('debug')) === false) { + $errorHandler = $handler; - if ($this->errorHandler instanceof \Leaf\Exception\Run) { - $this->errorHandler->unregister(); - } + if ($this->errorHandler instanceof \Leaf\Exception\Run) { + $this->errorHandler->unregister(); + } - if ($handler instanceof \Leaf\Exception\Handler\Handler) { - $this->errorHandler = new \Leaf\Exception\Run(); - $this->errorHandler->pushHandler($handler)->register(); - } + if ($handler instanceof \Leaf\Exception\Handler\Handler) { + $this->errorHandler = new \Leaf\Exception\Run(); + $this->errorHandler->pushHandler($handler)->register(); + } - if ($wrapper) { - $errorHandler = function ($errno, $errstr = '', $errfile = '', $errline = '') use ($handler) { - $exception = Exception\General::toException($errno, $errstr, $errfile, $errline); - Http\Response::status(500); - call_user_func_array($handler, [$exception]); - exit(); - }; - } + if ($wrapper) { + $errorHandler = function ($errno, $errstr = '', $errfile = '', $errline = '') use ($handler) { + $exception = Exception\General::toException($errno, $errstr, $errfile, $errline); + Http\Headers::resetStatus(500); + call_user_func_array($handler, [$exception]); + exit(); + }; + } - set_error_handler($errorHandler); + set_error_handler($errorHandler); + } } /** @@ -175,33 +179,32 @@ private function setupDefaultContainer() } // Default mode - (function () { - $mode = $this->config('mode'); + $mode = $this->config('mode'); - if (_env('APP_ENV')) { - $mode = _env('APP_ENV'); - } + if (_env('APP_ENV')) { + $mode = _env('APP_ENV'); + } - if (_env('LEAF_MODE')) { - $mode = _env('LEAF_MODE'); - } + if (_env('LEAF_MODE')) { + $mode = _env('LEAF_MODE'); + } - if (isset($_ENV['LEAF_MODE'])) { - $mode = $_ENV['LEAF_MODE']; - } else { - $envMode = getenv('LEAF_MODE'); + if (isset($_ENV['LEAF_MODE'])) { + $mode = $_ENV['LEAF_MODE']; + } else { + $envMode = getenv('LEAF_MODE'); - if ($envMode !== false) { - $mode = $envMode; - } + if ($envMode !== false) { + $mode = $envMode; } + } - $this->config('mode', $mode); - })(); - - Config::set('app', [ - 'instance' => $this, - 'container' => $this->container, + Config::set([ + 'mode' => $mode, + 'app' => [ + 'instance' => $this, + 'container' => $this->container, + ], ]); } @@ -251,6 +254,7 @@ public function config($name, $value = null) } Config::set($name, $value); + $this->loadConfig(); $this->setupErrorHandler(); } @@ -261,8 +265,8 @@ public function config($name, $value = null) */ public function setRequestClass($class) { - $this->container->singleton('request', function () { - return new \Leaf\Http\Request(); + $this->container->singleton('request', function () use ($class) { + return new $class(); }); } @@ -327,7 +331,6 @@ public function response() return $this->response; } - /** * Create mode-specific code * @@ -361,7 +364,7 @@ public static function script($mode, $callback) */ public function root() { - return rtrim($_SERVER['DOCUMENT_ROOT'], '/') . rtrim($this->request->getRootUri(), '/') . '/'; + return rtrim($_SERVER['DOCUMENT_ROOT'], '/') . rtrim($this->request->getScriptName(), '/') . '/'; } /** diff --git a/tests/app.test.php b/tests/app.test.php index 3e76c88..0d15310 100644 --- a/tests/app.test.php +++ b/tests/app.test.php @@ -29,29 +29,28 @@ }); test('set 404', function () { - app()->config('app.down', false); + app()->config('testKey.one', 'ooooo'); $_SERVER['REQUEST_METHOD'] = 'POST'; $_SERVER['REQUEST_URI'] = '/home'; app()->set404(function () { - app()->config('app.down', true); + app()->config('testKey.one', true); }); app()->run(); - expect(app()->config('app.down'))->toBe(true); + expect(app()->config('testKey.one'))->toBe(true); }); test('leaf middleware', function () { - $app = new Leaf\App(); - app()->config('app.down', false); + app()->config('anotherKey', false); class AppMid extends \Leaf\Middleware { public function call() { - app()->config('app.down', true); + app()->config('anotherKey', true); $this->next(); } } @@ -64,7 +63,7 @@ public function call() }); app()->run(); - expect(app()->config('app.down'))->toBe(true); + expect(app()->config('anotherKey'))->toBe(true); }); test('in-route middleware', function () { @@ -72,17 +71,17 @@ public function call() $_SERVER['REQUEST_URI'] = '/'; $app = new Leaf\App(); - $app->config('app.down', false); + $app->config('useMiddleware', false); $m = function () use ($app) { - $app->config('app.down', true); + $app->config('useMiddleware', true); }; $app->get('/', ['middleware' => $m, function () { }]); $app->run(); - expect($app->config('app.down'))->toBe(true); + expect($app->config('useMiddleware'))->toBe(true); }); test('before route middleware', function () { diff --git a/tests/config.test.php b/tests/config.test.php index 78a22c0..fe29c86 100644 --- a/tests/config.test.php +++ b/tests/config.test.php @@ -21,23 +21,23 @@ }); test('nested config', function () { - app()->config('app.key', '2'); + app()->config('randomKey.number', '2'); - $appConfig = app()->config('app'); + $randomKey = app()->config('randomKey'); - expect(isset($appConfig['key']))->toBeTrue(); - expect($appConfig['key'])->toBe('2'); - expect(app()->config('app.key'))->toBe('2'); + expect(isset($randomKey['number']))->toBeTrue(); + expect($randomKey['number'])->toBe('2'); + expect(app()->config('randomKey.number'))->toBe('2'); }); test('nested config (array)', function () { - app()->config(['app.key' => '2']); + app()->config(['nestedKey.number' => '2']); - $appConfig = app()->config('app'); + $nestedKey = app()->config('nestedKey'); - expect(isset($appConfig['key']))->toBeTrue(); - expect($appConfig['key'])->toBe('2'); - expect(app()->config('app.key'))->toBe('2'); + expect(isset($nestedKey['number']))->toBeTrue(); + expect($nestedKey['number'])->toBe('2'); + expect(app()->config('nestedKey.number'))->toBe('2'); }); test('nested config (custom group)', function () {