Recently I started playing a bit with Slim and I am really excited with possibilities that offers this solution. It’s not micro - it’s huge, it’s not "frame"work it’s "wide open gate"work. What is - sorry what was making me confused was a necessity of container and middleware hard coding ? What the heck is a separated of a controller the middleware idea ? Why the heck the request and response are so meaningfully underlined within application coding process ? Those 3 questions made my approach to Slim belated till the day before yesterday when (frankly speaking against myself) I started to look into the code and in the end I was chocked (in positive meaning of course).
But let’s get to the point. As because I am a staunch supporter of a dynamic loading and hmvc pattern, yesterday I wrote a piece of code for middleware dynamic loading that suits my preferences. Considering my programming skills limitations - I am more like a hobbyist than a professional programmer - would anybody be so kind and take a look optimize what I have done ? Thanks in advance.
settings.php
'mwGETbefore' => [
[
'cIndex' => 'auth',
'nClass' => 'paneric\\engine\\middlewares\\Auth',
'inject' => ['session','access'],
],
[
'cIndex' => 'access',
'nClass' => 'paneric\\engine\\middlewares\\Access',
'inject' => ['session','csrfForgery'],
],
[
'cIndex' => 'csrfForgery',
'nClass' => 'paneric\\engine\\middlewares\\CsrfForgery',
'inject' => ['session','xssClean'],
],
[
'cIndex' => 'xssClean',
'nClass' => 'paneric\\engine\\middlewares\\XssClean',
'inject' => [],
],
],
middleware.php
/** Middleware : */ //=================================================
//=====================================================================
$middleware = function ($request, $response, $next) use ($container){
$method = $request->getMethod();
/** Trigger "BEFORE" : */ //-------------------------------------------
//---------------------------------------------------------------------
if ($this->get('settings')['mw'.$method.'before']){
$settings = $this->get('settings')['mw'.$method.'before'];
$request = $this['middlewareTrigger']->run($this, $request, $response, $method, $settings);
}
/** Trigger "ROUTE" : */ //--------------------------------------------
//---------------------------------------------------------------------
$response = $next($request, $response);
/** Trigger "AFTER" : */ //--------------------------------------------
//---------------------------------------------------------------------
if ($this->get('settings')['mw'.$method.'after']){
$settings = $this->get('settings')['mw'.$method.'after'];
$response = $this['middlewareTrigger']->run($this, $request, $response, $method, $settings);
}
return $response;
};
MiddlewareTrigger.php
//=====================================================================
/** CLASS MiddlewareTrigger */ //======================================
//=====================================================================
class MiddlewareTrigger{
//=====================================================================
/** public function run() */
//=====================================================================
public function run($cnt, $request, $response, $method, $settings = array())
{
foreach ($settings as $mw)
$cnt[$mw['cIndex']] = function ($c) use ($mw){
return new $mw['nClass']();
};
foreach ($settings as $mw)
foreach ($mw['inject'] as $inject)
$args[$mw['cIndex']][] = $cnt[$inject];
return $cnt[$settings[0]['cIndex']]->run($request,$response,$args);
}
}
routes.php
/** Routes : */ //=====================================================
//=====================================================================
$app->get('/[{params:.*}]', function ( $request, $response, $args) use($container)
{
// trigger controller ...
return $response;
})->add($middleware);
Auth.php (example middleware “BEFORE”)
//=====================================================================
/** CLASS Auth */ //===================================================
//=====================================================================
class Auth{
private $wrapped;
//=====================================================================
/** public function run() */
//=====================================================================
public function run($request, $response, $args = array())
{
// class short name
$cName = lcfirst((new \ReflectionClass($this))->getShortName());
//pseudo injection
$this->cSession = $args[lcfirst($cName)][0];
// ...
$this->wrapped = end($args[lcfirst($cName)]);
unset($args[lcfirst($cName)]);
// manipulate the request, maybe
$request = $request->withAttribute($cName, true);
// delegate to the middleware we wrap:
return $this->wrapped->run($request, $response, $args);
}
}