Skip to content

Commit

Permalink
[RFC Implementation] Merge Router into Core (#254)
Browse files Browse the repository at this point in the history
* chore: merge router into core

* feat: use env as default mode

* chore: remove unused files

* feat: update router internals

* chore: update middleware internals

* chore: fix styling

* feat: update middleware to run on all http methods

* feat: add csrf method

* chore: fix styling
  • Loading branch information
mychidarko authored Oct 2, 2024
1 parent ddad1e3 commit b887f2c
Show file tree
Hide file tree
Showing 7 changed files with 880 additions and 235 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leafs/leaf",
"description": "Simple, performant and powerful PHP micro-framework for rapid web app & API development",
"description": "Elegant PHP for modern developers",
"keywords": [
"microframework",
"rest",
Expand Down Expand Up @@ -33,7 +33,6 @@
"require": {
"php": "^7.4|^8.0",
"leafs/http": "*",
"leafs/router": "*",
"leafs/anchor": "*",
"leafs/exception": "*"
},
Expand Down
42 changes: 24 additions & 18 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ public function __construct(array $userSettings = [])
protected function loadConfig(array $userSettings = [])
{
if (!empty($userSettings)) {
Config::set($userSettings);
Config::set(array_merge($userSettings, [
'mode' => _env('APP_ENV', Config::getStatic('mode')),
]));
}

$this->setupDefaultContainer();
$this->loadViewEngines();
}

protected function setupErrorHandler()
Expand Down Expand Up @@ -90,22 +91,6 @@ public function register($name, $value)
Config::singleton($name, $value);
}

/**
* This method loads all added view engines
*/
public function loadViewEngines()
{
$views = View::$engines;

if (!empty($views)) {
foreach ($views as $key => $value) {
Config::singleton($key, function () use ($value) {
return $value;
});
}
}
}

private function setupDefaultContainer()
{
Config::singleton('request', function () {
Expand Down Expand Up @@ -202,6 +187,27 @@ public function cors($options = [])
}
}

/**
* Add CSRF protection to your app
*
* @param array $options Config for csrf
*/
public function csrf($options = [])
{
if (!\class_exists('Leaf\Anchor\CSRF')) {
\trigger_error('CSRF module not found! Run `leaf install csrf` or `composer require leafs/csrf` to install the CSRF module. This is required to configure CSRF.');
}

if (!Anchor\CSRF::token()) {
Anchor\CSRF::init();
Anchor\CSRF::config($options);
}

$this->use(function () {
Anchor\CSRF::validate();
});
}

/**
* Create a route handled by websocket (requires Eien module)
*
Expand Down
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Config
'log.level' => null,
'log.enabled' => false,
'log.dir' => __DIR__ . '/../../../../storage/logs/',
'log.file' => 'log.txt',
'log.file' => 'app.log',
'log.open' => true,
'mode' => 'development',
'scripts' => [],
Expand Down
80 changes: 0 additions & 80 deletions src/Middleware.php

This file was deleted.

Loading

0 comments on commit b887f2c

Please sign in to comment.