* initial commit
* get list of activities * get activity by id
This commit is contained in:
92
src/App/Action/AbstractAction.php
Normal file
92
src/App/Action/AbstractAction.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Stratigility\MiddlewareInterface;
|
||||
|
||||
abstract class AbstractAction implements MiddlewareInterface
|
||||
{
|
||||
const IDENTIFIER_NAME = 'id';
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$requestMethod = strtoupper($request->getMethod());
|
||||
$id = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
|
||||
switch ($requestMethod) {
|
||||
case 'GET':
|
||||
return isset($id)
|
||||
? $this->get($request, $response, $next)
|
||||
: $this->getList($request, $response, $next);
|
||||
case 'POST':
|
||||
return $this->create($request, $response, $next);
|
||||
case 'PUT':
|
||||
return $this->update($request, $response, $next);
|
||||
case 'DELETE':
|
||||
return isset($id)
|
||||
? $this->delete($request, $response, $next)
|
||||
: $this->deleteList($request, $response, $next);
|
||||
case 'HEAD':
|
||||
return $this->head($request, $response, $next);
|
||||
case 'OPTIONS':
|
||||
return $this->options($request, $response, $next);
|
||||
case 'PATCH':
|
||||
return $this->patch($request, $response, $next);
|
||||
default:
|
||||
return $next($request, $response);
|
||||
}
|
||||
}
|
||||
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function create(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function update(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function delete(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function deleteList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function head(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function options(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function patch(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
final protected function createResponse($data, $status = 200)
|
||||
{
|
||||
return new JsonResponse($data, $status);
|
||||
}
|
||||
}
|
||||
34
src/App/Action/ActivityAction.php
Normal file
34
src/App/Action/ActivityAction.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\SkiesService;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class ActivityAction extends AbstractAction
|
||||
{
|
||||
private $skiesService;
|
||||
|
||||
public function __construct(SkiesService $skiesService)
|
||||
{
|
||||
$this->skiesService = $skiesService;
|
||||
}
|
||||
|
||||
public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return new JsonResponse($this->skiesService->getActivityList());
|
||||
}
|
||||
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute(self::IDENTIFIER_NAME);
|
||||
return new JsonResponse($this->skiesService->getActivity($id));
|
||||
}
|
||||
|
||||
public function options(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return new JsonResponse(true);
|
||||
}
|
||||
}
|
||||
15
src/App/Action/ActivityFactory.php
Normal file
15
src/App/Action/ActivityFactory.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\SkiesService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class ActivityFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$skiesService = $container->get(SkiesService::class);
|
||||
return new ActivityAction($skiesService);
|
||||
}
|
||||
}
|
||||
62
src/App/Action/HomePageAction.php
Normal file
62
src/App/Action/HomePageAction.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Expressive\Router;
|
||||
use Zend\Expressive\Template;
|
||||
use Zend\Expressive\Plates\PlatesRenderer;
|
||||
use Zend\Expressive\Twig\TwigRenderer;
|
||||
use Zend\Expressive\ZendView\ZendViewRenderer;
|
||||
|
||||
class HomePageAction
|
||||
{
|
||||
private $router;
|
||||
|
||||
private $template;
|
||||
|
||||
public function __construct(Router\RouterInterface $router, Template\TemplateRendererInterface $template = null)
|
||||
{
|
||||
$this->router = $router;
|
||||
$this->template = $template;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if ($this->router instanceof Router\AuraRouter) {
|
||||
$data['routerName'] = 'Aura.Router';
|
||||
$data['routerDocs'] = 'http://auraphp.com/packages/2.x/Router.html';
|
||||
} elseif ($this->router instanceof Router\FastRouteRouter) {
|
||||
$data['routerName'] = 'FastRoute';
|
||||
$data['routerDocs'] = 'https://github.com/nikic/FastRoute';
|
||||
} elseif ($this->router instanceof Router\ZendRouter) {
|
||||
$data['routerName'] = 'Zend Router';
|
||||
$data['routerDocs'] = 'http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html';
|
||||
}
|
||||
|
||||
if ($this->template instanceof PlatesRenderer) {
|
||||
$data['templateName'] = 'Plates';
|
||||
$data['templateDocs'] = 'http://platesphp.com/';
|
||||
} elseif ($this->template instanceof TwigRenderer) {
|
||||
$data['templateName'] = 'Twig';
|
||||
$data['templateDocs'] = 'http://twig.sensiolabs.org/documentation';
|
||||
} elseif ($this->template instanceof ZendViewRenderer) {
|
||||
$data['templateName'] = 'Zend View';
|
||||
$data['templateDocs'] = 'http://framework.zend.com/manual/current/en/modules/zend.view.quick-start.html';
|
||||
}
|
||||
|
||||
if (!$this->template) {
|
||||
return new JsonResponse([
|
||||
'welcome' => 'Congratulations! You have installed the zend-expressive skeleton application.',
|
||||
'docsUrl' => 'zend-expressive.readthedocs.org',
|
||||
]);
|
||||
}
|
||||
|
||||
return new HtmlResponse($this->template->render('app::home-page', $data));
|
||||
}
|
||||
}
|
||||
20
src/App/Action/HomePageFactory.php
Normal file
20
src/App/Action/HomePageFactory.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Zend\Expressive\Router\RouterInterface;
|
||||
use Zend\Expressive\Template\TemplateRendererInterface;
|
||||
|
||||
class HomePageFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$router = $container->get(RouterInterface::class);
|
||||
$template = ($container->has(TemplateRendererInterface::class))
|
||||
? $container->get(TemplateRendererInterface::class)
|
||||
: null;
|
||||
|
||||
return new HomePageAction($router, $template);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/PingAction.php
Normal file
17
src/App/Action/PingAction.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class PingAction
|
||||
{
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return new JsonResponse([
|
||||
'ack' => time(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user