* expressive version uplift

* loslog added
This commit is contained in:
Danyi Dávid
2018-07-09 01:01:22 +02:00
parent efad8765c8
commit 12c98c0519
18 changed files with 885 additions and 597 deletions

View File

@@ -2,17 +2,17 @@
namespace App\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Json\Json;
abstract class AbstractAction implements ServerMiddlewareInterface
abstract class AbstractAction implements RequestHandlerInterface
{
const IDENTIFIER_NAME = 'id';
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function handle(ServerRequestInterface $request): ResponseInterface
{
$requestMethod = strtoupper($request->getMethod());
$id = $request->getAttribute(static::IDENTIFIER_NAME);
@@ -20,73 +20,71 @@ abstract class AbstractAction implements ServerMiddlewareInterface
switch ($requestMethod) {
case 'GET':
return isset($id)
? $this->get($request, $delegate)
: $this->getList($request, $delegate);
? $this->get($request)
: $this->getList($request);
case 'POST':
return $this->create($request, $delegate);
return $this->create($request);
case 'PUT':
return $this->update($request, $delegate);
return $this->update($request);
case 'DELETE':
return isset($id)
? $this->delete($request, $delegate)
: $this->deleteList($request, $delegate);
? $this->delete($request)
: $this->deleteList($request);
case 'HEAD':
return $this->head($request, $delegate);
return $this->head($request);
case 'OPTIONS':
return $this->options($request, $delegate);
return $this->options($request);
case 'PATCH':
return $this->patch($request, $delegate);
default:
return $delegate->process($request);
return $this->patch($request);
}
}
public function get(ServerRequestInterface $request, DelegateInterface $delegate)
public function get(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse(['content' => 'Method not allowed'], 405);
}
public function getList(ServerRequestInterface $request, DelegateInterface $delegate)
public function getList(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse(['content' => 'Method not allowed'], 405);
}
public function create(ServerRequestInterface $request, DelegateInterface $delegate)
public function create(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse(['content' => 'Method not allowed'], 405);
}
public function update(ServerRequestInterface $request, DelegateInterface $delegate)
public function update(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse(['content' => 'Method not allowed'], 405);
}
public function delete(ServerRequestInterface $request, DelegateInterface $delegate)
public function delete(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse(['content' => 'Method not allowed'], 405);
}
public function deleteList(ServerRequestInterface $request, DelegateInterface $delegate)
public function deleteList(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse(['content' => 'Method not allowed'], 405);
}
public function head(ServerRequestInterface $request, DelegateInterface $delegate)
public function head(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse(['content' => 'Method not allowed'], 405);
}
public function options(ServerRequestInterface $request, DelegateInterface $delegate)
public function options(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse(['content' => 'Method not allowed'], 405);
}
public function patch(ServerRequestInterface $request, DelegateInterface $delegate)
public function patch(ServerRequestInterface $request): ResponseInterface
{
return $this->createResponse(['content' => 'Method not allowed'], 405);
}
final protected function createResponse($data, $status = 200)
final protected function createResponse($data, $status = 200): ResponseInterface
{
return new JsonResponse($data, $status);
}

View File

@@ -3,12 +3,12 @@
namespace App\Action;
use App\Service\KoinService;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\TextResponse;
class HomePageAction implements ServerMiddlewareInterface
class HomePageAction implements RequestHandlerInterface
{
private $tmp;
@@ -17,7 +17,12 @@ class HomePageAction implements ServerMiddlewareInterface
$this->tmp = $tmp;
}
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
/**
* Handle the request and return a response.
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new TextResponse("Nuff");
}

View File

@@ -2,14 +2,14 @@
namespace App\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ServerRequestInterface;
class PingAction implements ServerMiddlewareInterface
class PingAction implements RequestHandlerInterface
{
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new JsonResponse(['ack' => time()]);
}

View File

@@ -5,7 +5,7 @@ namespace App\Action;
use App\Entity\Sms;
use App\Response\JsonCorsResponse;
use App\Service\SmsStoreService;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class StoreAction extends AbstractAction
@@ -17,7 +17,7 @@ class StoreAction extends AbstractAction
$this->smsStore = $smsStore;
}
public function create(ServerRequestInterface $request, DelegateInterface $delegate)
public function create(ServerRequestInterface $request): ResponseInterface
{
$hashKey = $request->getAttribute('hashKey');
$direction = $request->getAttribute('direction');

View File

@@ -2,23 +2,22 @@
namespace App\Middleware;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class EventSubscriberMiddleware implements ServerMiddlewareInterface
{
/**
* This actually does nothing, just wraps the next middleware in the pipe.
*
* @param ServerRequestInterface $request
* @param DelegateInterface $delegate
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
return $delegate->process($request);
}
/**
* This actually does nothing, just wraps the next middleware in the pipe.
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return $handler->handle($request);
}
}

View File

@@ -9,19 +9,19 @@ use Zend\EventManager\LazyListener;
class EventSubscriberMiddlewareFactory
{
public function __invoke(ContainerInterface $container)
{
$eventManager = $container->get(EventManager::class);
$eventManager->setIdentifiers([
EventSubscriberMiddleware::class,
]);
public function __invoke(ContainerInterface $container)
{
$eventManager = $container->get(EventManager::class);
$eventManager->setIdentifiers([
EventSubscriberMiddleware::class,
]);
$lazyListener = new LazyListener([
'listener' => KoinService::class,
'method' => 'onReceiveSmsListener',
], $container);
$eventManager->attach('store.sms.persisted', $lazyListener);
$lazyListener = new LazyListener([
'listener' => KoinService::class,
'method' => 'onReceiveSmsListener',
], $container);
$eventManager->attach('store.sms.persisted', $lazyListener);
return new EventSubscriberMiddleware();
}
return new EventSubscriberMiddleware();
}
}

View File

@@ -2,10 +2,12 @@
namespace App\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class PreFlightMiddleware
class PreFlightMiddleware implements ServerMiddlewareInterface
{
const ALLOW_HEADERS = [
'DNT',
@@ -19,16 +21,24 @@ class PreFlightMiddleware
'Authorization',
];
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
/**
* Set CORS headers the ugly way.
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
* @todo replace with cors middleware tuupola?
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$requestMethod = strtoupper($request->getMethod());
if ($requestMethod == 'OPTIONS') {
return $response
$response = $request
->withHeader('Accept', 'OPTIONS,GET,POST,PUT,PATCH,DELETE')
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Methods', 'OPTIONS,GET,POST,PUT,PATCH,DELETE')
->withHeader('Access-Control-Allow-Headers', implode(",", self::ALLOW_HEADERS));
return $handler->handle($response);
}
return $next($request, $response);
return $handler->handle($request);
}
}