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

Convert Radar to PSR-15 compatible middleware #38

Open
wants to merge 9 commits into
base: 2.x
Choose a base branch
from
26 changes: 10 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
{
"name": "radar/adr",
"description": "The Action-Domain-Responder core library for Radar.",
"name": "radar/middleware",
"description": "A PSR-15 compatible Action-Domain-Responder route handler.",
"type": "library",
"homepage": "https://github.com/radarphp/Radar.Adr",
"homepage": "https://github.com/radarphp/Radar.Middleware",
"license": "MIT",
"require": {
"aura/di": "~3.0",
"aura/payload": "~3.0",
"aura/router": "~3.0",
"relay/relay": "~1.0",
"relay/middleware": "~1.0",
"arbiter/arbiter": "~1.0"
"aura/payload-interface": "~3.0",
"arbiter/arbiter": "~1.0",
"psr/http-message": "~1.0",
"psr/http-server-middleware": "^1.0",
"PHP-DI/invoker": "^2.0",
"psr/container": "^1.0"
},
"require-dev": {
"zendframework/zend-diactoros": "~1.0"
},
"autoload": {
"psr-4": {
"Radar\\Adr\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Radar\\Adr\\": "tests/",
"Aura\\Di\\": "vendor/aura/di/tests/"
"Radar\\Middleware\\": "src/"
}
}
}
100 changes: 100 additions & 0 deletions src/ActionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
*
* This file is part of Radar for PHP.
*
* @license http://opensource.org/licenses/MIT MIT
*
*/
namespace Radar\Middleware;

use Arbiter\ActionFactory;
use Arbiter\ActionHandler as Arbiter;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response;

/**
*
* Dispatches to the Action stored in the `radar/adr:action` Request attribute.
*
* @package radar/middleware
*
*/
class ActionHandler extends Arbiter implements MiddlewareInterface
{
/**
* @var string Attribute name for handler reference
*/
private $handlerAttribute = 'request-handler';
/**
* @var ActionSpecsFactory
*/
private $actionSpecsFactory;
/**
* @var ActionFactory
*/
private $actionFactory;

/**
* ActionHandler constructor.
*
* @param ActionSpecsFactory $actionSpecsFactory
* @param ActionFactory $actionFactory
* @param callable|null $resolver
*/
public function __construct(
ActionSpecsFactory $actionSpecsFactory,
ActionFactory $actionFactory,
callable $resolver = null
)
{
parent::__construct($resolver);
$this->actionSpecsFactory = $actionSpecsFactory;
$this->actionFactory = $actionFactory;
}

/**
* Set the attribute name to store handler reference.
*
* @param string $handlerAttribute
* @return ActionHandler
*/
public function handlerAttribute(string $handlerAttribute): self
{
$this->handlerAttribute = $handlerAttribute;
return $this;
}

/**
*
* Dispatches to the Action stored in the `radar/adr:action` Request
* attribute.
*
* @param ServerRequestInterface $request The HTTP request object.
*
* @param RequestHandlerInterface $handler The handler middleware decorator.
*
* @return ResponseInterface
*
* @throws \InvalidArgumentException
* @throws \Arbiter\Exception
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$requestHandler = $request->getAttribute($this->handlerAttribute);
$request = $request->withoutAttribute($this->handlerAttribute);

$specs = $this->actionSpecsFactory->newInstance($requestHandler);

$action = $this->actionFactory->newInstance(
$specs->input,
$specs->domain,
$specs->responder
);

return $this->handle($action, $request, new Response());
}
}
110 changes: 110 additions & 0 deletions src/ActionSpecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
*
* This file is part of Radar for PHP.
*
* @license http://opensource.org/licenses/MIT MIT
*
*/

namespace Radar\Middleware;

/**
* Auto-discovery of custom Action specs or fallback to the defaults.
*
* @package radar\middleware
*/
class ActionSpecs
{
/**
*
* The input spec to use with the action.
*
* @var string
*
*/
public $input = 'Radar\Middleware\Input';

/**
*
* The domain spec to use with the action.
*
* @var string
*
*/
public $domain;

/**
*
* The responder spec to use with the action.
*
* @var string
*
*/
public $responder = 'Radar\Middleware\Responder';

/**
* ActionSpecs constructor.
*
* @param string $handler
*/
public function __construct(string $handler)
{
$input = $handler . '\\Input';
if (class_exists($input)) {
$this->input($input);
}

$this->domain($handler);

$responder = $handler . '\\Responder';
if (class_exists($responder)) {
$this->responder($responder);
}
}

/**
*
* Sets the input specification.
*
* @param string $input The input specification.
*
* @return self
*
*/
protected function input($input): self
{
$this->input = $input;
return $this;
}

/**
*
* Sets the domain specification.
*
* @param string $domain The domain specification.
*
* @return self
*
*/
protected function domain($domain): self
{
$this->domain = $domain;
return $this;
}

/**
*
* Sets the responder specification.
*
* @param string $responder The responder specification.
*
* @return self
*
*/
protected function responder($responder): self
{
$this->responder = $responder;
return $this;
}
}
28 changes: 28 additions & 0 deletions src/ActionSpecsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
*
* This file is part of Radar for PHP.
*
* @license http://opensource.org/licenses/MIT MIT
*
*/
namespace Radar\Middleware;

/**
*
*
* @package Radar\Middleware
*/
class ActionSpecsFactory
{
/**
* Returns a new ActionSpecs instance.
*
* @param string $handler
* @return ActionSpecs
*/
public function newInstance(string $handler): ActionSpecs
{
return new ActionSpecs($handler);
}
}
Loading