Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add middleware handler #39

Merged
merged 4 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}