Skip to content

Commit

Permalink
Update respone handler to always use response objects and update mini…
Browse files Browse the repository at this point in the history
…mum requirements
  • Loading branch information
pierredup committed Mar 3, 2020
1 parent 039ef3b commit f9fdbc2
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 178 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ This class needs to implement the `SolidWorx\SimpleResponseBundle\ResponseHandle

```yml
services:
some.service.name:
class: My\Custom\Handler
My\Custom\Handler:
arguments: ['@doctrine.orm.entity_manager']
tags:
- { name: 'response_handler.handler' }
tags: ['solidworx.response_handler']

```
Expand All @@ -108,16 +106,18 @@ You then need to create a class that will be used as the return value in your ac
```php
<?php
use Symfony\Component\HttpFoundation\JsonResponse;

class DoctrineEntityResponse
class DoctrineEntityResponse extends JsonResponse
{
private $entity;

public function __construct(string $entity)
{
$this->entity = $entity;
parent::__construct();
}

public function getEntity(): string
{
return $this->entity;
Expand All @@ -129,6 +129,8 @@ Your handler class will add the logic to return a response object;

```php
<?php
use SolidWorx\SimpleResponseBundle\ResponseHandlerInterface;
use Symfony\Component\HttpFoundation\Response;

class Handler implements ResponseHandlerInterface
{
Expand All @@ -139,14 +141,14 @@ class Handler implements ResponseHandlerInterface
$this->em = $entityManager;
}

public function supports($object): bool
public function supports(Response $object): bool
{
return $object instance of DoctrineEntityReponse; // Only support responses of this type
return $object instanceof DoctrineEntityReponse; // Only support responses of this type
}

public function handle($object)
public function handle(Response $object): Response
{
return new JsonResponse($this->em->getRepository($object->getEntity())->findAll()); // Return all records in the entity as a JSON response
return $object->setData($this->em->getRepository($object->getEntity())->findAll()); // Return all records in the entity as a JSON response
}
}
```
Expand All @@ -162,7 +164,7 @@ class MyAction
{
public function __invoke()
{
return new DoctrineEntityResponse('AppBundle:Order'); // Pass the Order entity which will return all orders in a JSON response
return new DoctrineEntityResponse(\App\Entity\Order::class); // Pass the Order entity which will return all orders in a JSON response
}
}

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
}
},
"require": {
"php": ">= 7.1.3",
"symfony/framework-bundle": "^3.4 | ^4.0",
"symfony/http-foundation": "^3.4 | ^4.0"
"php": ">=7.4",
"symfony/framework-bundle": "^4.0 || ^5.0",
"symfony/http-foundation": "^4.0 || ^5.0",
"twig/twig": "^2.0 || ^3.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions src/DependencyInjection/SimpleResponseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class SimpleResponseExtension extends Extension
final class SimpleResponseExtension extends Extension
{
/**
* {@inheritdoc}
Expand All @@ -27,4 +27,4 @@ public function load(array $configs, ContainerBuilder $container)

$loader->import('*.yaml');
}
}
}
24 changes: 11 additions & 13 deletions src/Handler/RouteRedirectHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,28 @@

namespace SolidWorx\SimpleResponseBundle\Handler;

use SolidWorx\SimpleResponseBundle\ResponseHandlerInterface;
use SolidWorx\SimpleResponseBundle\Response\RouteRedirectResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use SolidWorx\SimpleResponseBundle\ResponseHandlerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class RouteRedirectHandler implements ResponseHandlerInterface
final class RouteRedirectHandler implements ResponseHandlerInterface
{
/**
* @var RouterInterface
*/
private $router;
private UrlGeneratorInterface $urlGenerator;

public function __construct(RouterInterface $router)
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->router = $router;
$this->urlGenerator = $urlGenerator;
}

public function handle($object): Response
public function handle(Response $object): Response
{
return new RedirectResponse($this->router->generate($object->getRoute(), $object->getParameters()), $object->getStatusCode());
/* @var RouteRedirectResponse $object */

return $object->setTargetUrl($this->urlGenerator->generate($object->getRoute(), $object->getParameters()));
}

public function supports($object): bool
public function supports(Response $object): bool
{
return $object instanceof RouteRedirectResponse;
}
Expand Down
21 changes: 9 additions & 12 deletions src/Handler/TemplateResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,26 @@

use SolidWorx\SimpleResponseBundle\Response\TemplateResponse;
use SolidWorx\SimpleResponseBundle\ResponseHandlerInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

class TemplateResponseHandler implements ResponseHandlerInterface
final class TemplateResponseHandler implements ResponseHandlerInterface
{
/**
* @var EngineInterface
*/
private $engine;
private Environment $twig;

public function __construct(EngineInterface $engine)
public function __construct(Environment $twig)
{
$this->engine = $engine;
$this->twig = $twig;
}

public function handle($object): Response
public function handle(Response $object): Response
{
/* @var TemplateResponse $object */
return $this->engine->renderResponse($object->getTemplate(), $object->getParams(), $object->getResponse());
return $object->setContent($this->twig->render($object->getTemplate(), $object->getContext()));
}

public function supports($object): bool
public function supports(Response $object): bool
{
return $object instanceof TemplateResponse;
}
}
}
14 changes: 8 additions & 6 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ services:
public: false
autoconfigure: true

SolidWorx\SimpleResponseBundle\ResponseHandler: ~
_instanceof:
SolidWorx\SimpleResponseBundle\ResponseHandlerInterface:
tags: ['solidworx.response_handler']

SolidWorx\SimpleResponseBundle\ResponseHandler:
arguments: [!tagged_iterator 'solidworx.response_handler']

SolidWorx\SimpleResponseBundle\ResponseHandlerListener:
tags: ['kernel.event_subscriber']

# Core handlers
SolidWorx\SimpleResponseBundle\Handler\TemplateResponseHandler:
tags: ['response_handler.handler']

SolidWorx\SimpleResponseBundle\Handler\RouteRedirectHandler:
tags: ['response_handler.handler']
SolidWorx\SimpleResponseBundle\Handler\TemplateResponseHandler: ~
SolidWorx\SimpleResponseBundle\Handler\RouteRedirectHandler: ~
37 changes: 8 additions & 29 deletions src/Response/RouteRedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,21 @@

namespace SolidWorx\SimpleResponseBundle\Response;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

class RouteRedirectResponse
final class RouteRedirectResponse extends RedirectResponse
{
/**
* @var string
*/
private $route;
private string $route;

/**
* @var int
*/
private $statusCode;
private array $parameters;

/**
* @var array
*/
private $parameters;

public function __construct(string $route, array $parameters = [], int $statusCode = Response::HTTP_SEE_OTHER)
public function __construct(string $route = '', array $parameters = [], int $status = Response::HTTP_FOUND, array $headers = [])
{
$this->route = $route;
$this->statusCode = $statusCode;
$this->parameters = $parameters;

parent::__construct('', $status, $headers);
}

public function getRoute(): string
Expand All @@ -47,11 +38,6 @@ public function getParameters(): array
return $this->parameters;
}

public function getStatusCode(): int
{
return $this->statusCode;
}

public function setRoute(string $route): self
{
$this->route = $route;
Expand All @@ -65,11 +51,4 @@ public function setParameters(array $parameters): self

return $this;
}

public function setStatusCode(int $statusCode): self
{
$this->statusCode = $statusCode;

return $this;
}
}
}
44 changes: 18 additions & 26 deletions src/Response/TemplateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,41 @@

use Symfony\Component\HttpFoundation\Response;

class TemplateResponse
final class TemplateResponse extends Response
{
/**
* @var string
*/
private $template;

/**
* @var array
*/
private $params;

/**
* @var Response
*/
private $response;

public function __construct(string $template = null, array $params = [], Response $response = null)
private string $template;

private array $context;

public function __construct(string $template = '', array $context = [], int $status = Response::HTTP_OK, array $headers = [])
{
$this->template = $template;
$this->params = $params;
$this->response = $response ?: new Response();
$this->context = $context;

parent::__construct('', $status, $headers);
}

public function getTemplate(): ?string
{
return $this->template;
}

public function getParams(): array
public function getContext(): array
{
return $this->params;
return $this->context;
}

public function getResponse(): ?Response
public function setTemplate(string $template): self
{
return $this->response;
$this->template = $template;

return $this;
}

public function setTemplate(string $template): self
public function setContext(array $context): self
{
$this->template = $template;
$this->context = $context;

return $this;
}
}
}
Loading

0 comments on commit f9fdbc2

Please sign in to comment.