Skip to content

Commit

Permalink
Merge pull request #157 from leafsphp/v3.x-dev
Browse files Browse the repository at this point in the history
V3.x dev
  • Loading branch information
mychidarko authored Oct 3, 2022
2 parents 762dfd3 + 2430bb5 commit 38dc1dc
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
root = true

[*]
indent_style = tab
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
109 changes: 56 additions & 53 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -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,
],
]);
}

Expand Down Expand Up @@ -251,6 +254,7 @@ public function config($name, $value = null)
}

Config::set($name, $value);
$this->loadConfig();
$this->setupErrorHandler();
}

Expand All @@ -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();
});
}

Expand Down Expand Up @@ -327,7 +331,6 @@ public function response()
return $this->response;
}


/**
* Create mode-specific code
*
Expand Down Expand Up @@ -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(), '/') . '/';
}

/**
Expand Down
19 changes: 9 additions & 10 deletions tests/app.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -64,25 +63,25 @@ public function call()
});
app()->run();

expect(app()->config('app.down'))->toBe(true);
expect(app()->config('anotherKey'))->toBe(true);
});

test('in-route middleware', function () {
$_SERVER['REQUEST_METHOD'] = 'GET';
$_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 () {
Expand Down
20 changes: 10 additions & 10 deletions tests/config.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 38dc1dc

Please sign in to comment.