Skip to content

Commit b887f2c

Browse files
authored
[RFC Implementation] Merge Router into Core (#254)
* 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
1 parent ddad1e3 commit b887f2c

File tree

7 files changed

+880
-235
lines changed

7 files changed

+880
-235
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "leafs/leaf",
3-
"description": "Simple, performant and powerful PHP micro-framework for rapid web app & API development",
3+
"description": "Elegant PHP for modern developers",
44
"keywords": [
55
"microframework",
66
"rest",
@@ -33,7 +33,6 @@
3333
"require": {
3434
"php": "^7.4|^8.0",
3535
"leafs/http": "*",
36-
"leafs/router": "*",
3736
"leafs/anchor": "*",
3837
"leafs/exception": "*"
3938
},

src/App.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ public function __construct(array $userSettings = [])
4949
protected function loadConfig(array $userSettings = [])
5050
{
5151
if (!empty($userSettings)) {
52-
Config::set($userSettings);
52+
Config::set(array_merge($userSettings, [
53+
'mode' => _env('APP_ENV', Config::getStatic('mode')),
54+
]));
5355
}
5456

5557
$this->setupDefaultContainer();
56-
$this->loadViewEngines();
5758
}
5859

5960
protected function setupErrorHandler()
@@ -90,22 +91,6 @@ public function register($name, $value)
9091
Config::singleton($name, $value);
9192
}
9293

93-
/**
94-
* This method loads all added view engines
95-
*/
96-
public function loadViewEngines()
97-
{
98-
$views = View::$engines;
99-
100-
if (!empty($views)) {
101-
foreach ($views as $key => $value) {
102-
Config::singleton($key, function () use ($value) {
103-
return $value;
104-
});
105-
}
106-
}
107-
}
108-
10994
private function setupDefaultContainer()
11095
{
11196
Config::singleton('request', function () {
@@ -202,6 +187,27 @@ public function cors($options = [])
202187
}
203188
}
204189

190+
/**
191+
* Add CSRF protection to your app
192+
*
193+
* @param array $options Config for csrf
194+
*/
195+
public function csrf($options = [])
196+
{
197+
if (!\class_exists('Leaf\Anchor\CSRF')) {
198+
\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.');
199+
}
200+
201+
if (!Anchor\CSRF::token()) {
202+
Anchor\CSRF::init();
203+
Anchor\CSRF::config($options);
204+
}
205+
206+
$this->use(function () {
207+
Anchor\CSRF::validate();
208+
});
209+
}
210+
205211
/**
206212
* Create a route handled by websocket (requires Eien module)
207213
*

src/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Config
2525
'log.level' => null,
2626
'log.enabled' => false,
2727
'log.dir' => __DIR__ . '/../../../../storage/logs/',
28-
'log.file' => 'log.txt',
28+
'log.file' => 'app.log',
2929
'log.open' => true,
3030
'mode' => 'development',
3131
'scripts' => [],

src/Middleware.php

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)