Skip to content

Commit

Permalink
Add middleware handler (#39)
Browse files Browse the repository at this point in the history
* Middleware

* Run within cubex

* Remove middleware from cubex core

* 7.3 not supported
  • Loading branch information
bajb authored Nov 4, 2023
1 parent 6d52250 commit b7142fe
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
5 changes: 0 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ defaults: &defaults

version: 2
jobs:
build-php73:
<<: *defaults
docker:
- image: php:7.3-alpine
build-php74:
<<: *defaults
docker:
Expand All @@ -39,6 +35,5 @@ workflows:
version: 2
build:
jobs:
- build-php73
- build-php74
- build-php80
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"optimize-autoloader": true
},
"require": {
"php": ">=7.3",
"php": ">=7.4",
"ext-json": "*",
"packaged/config": "~1.5",
"packaged/context": "~1.4",
Expand Down
23 changes: 23 additions & 0 deletions src/Middleware/Middleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Cubex\Middleware;

use Packaged\Context\Context;
use Packaged\Routing\Handler\Handler;
use Symfony\Component\HttpFoundation\Response;

abstract class Middleware implements MiddlewareInterface
{
protected Handler $_next;

public function setNext(Handler $handler): Handler
{
$this->_next = $handler;
return $this;
}

protected function next(Context $c): Response
{
return $this->_next->handle($c);
}
}
41 changes: 41 additions & 0 deletions src/Middleware/MiddlewareHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace Cubex\Middleware;

use Packaged\Context\Context;
use Packaged\Routing\Handler\Handler;
use Symfony\Component\HttpFoundation\Response;

class MiddlewareHandler implements Handler
{
/** @var \Cubex\Middleware\MiddlewareInterface[] */
protected array $_middlewares = [];
/**
* @var \Packaged\Routing\Handler\Handler
*/
protected Handler $_handler;

public function __construct(Handler $handler)
{
$this->_handler = $handler;
}

public function add(MiddlewareInterface $middleware)
{
$this->_middlewares[] = $middleware;
return $this;
}

public function handle(Context $c): Response
{
$handler = $this->_handler;
if($this->_middlewares)
{
foreach($this->_middlewares as $middleware)
{
$handler = $middleware->setNext($handler);
}
}

return $handler->handle($c);
}
}
10 changes: 10 additions & 0 deletions src/Middleware/MiddlewareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Cubex\Middleware;

use Packaged\Routing\Handler\Handler;

interface MiddlewareInterface extends Handler
{
public function setNext(Handler $handler): Handler;
}

0 comments on commit b7142fe

Please sign in to comment.