Skip to content

Commit f9fdbc2

Browse files
committed
Update respone handler to always use response objects and update minimum requirements
1 parent 039ef3b commit f9fdbc2

13 files changed

+92
-178
lines changed

README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,9 @@ This class needs to implement the `SolidWorx\SimpleResponseBundle\ResponseHandle
9595

9696
```yml
9797
services:
98-
some.service.name:
99-
class: My\Custom\Handler
98+
My\Custom\Handler:
10099
arguments: ['@doctrine.orm.entity_manager']
101-
tags:
102-
- { name: 'response_handler.handler' }
100+
tags: ['solidworx.response_handler']
103101

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

112-
class DoctrineEntityResponse
111+
class DoctrineEntityResponse extends JsonResponse
113112
{
114113
private $entity;
115114

116115
public function __construct(string $entity)
117116
{
118117
$this->entity = $entity;
118+
parent::__construct();
119119
}
120-
120+
121121
public function getEntity(): string
122122
{
123123
return $this->entity;
@@ -129,6 +129,8 @@ Your handler class will add the logic to return a response object;
129129

130130
```php
131131
<?php
132+
use SolidWorx\SimpleResponseBundle\ResponseHandlerInterface;
133+
use Symfony\Component\HttpFoundation\Response;
132134

133135
class Handler implements ResponseHandlerInterface
134136
{
@@ -139,14 +141,14 @@ class Handler implements ResponseHandlerInterface
139141
$this->em = $entityManager;
140142
}
141143

142-
public function supports($object): bool
144+
public function supports(Response $object): bool
143145
{
144-
return $object instance of DoctrineEntityReponse; // Only support responses of this type
146+
return $object instanceof DoctrineEntityReponse; // Only support responses of this type
145147
}
146148

147-
public function handle($object)
149+
public function handle(Response $object): Response
148150
{
149-
return new JsonResponse($this->em->getRepository($object->getEntity())->findAll()); // Return all records in the entity as a JSON response
151+
return $object->setData($this->em->getRepository($object->getEntity())->findAll()); // Return all records in the entity as a JSON response
150152
}
151153
}
152154
```
@@ -162,7 +164,7 @@ class MyAction
162164
{
163165
public function __invoke()
164166
{
165-
return new DoctrineEntityResponse('AppBundle:Order'); // Pass the Order entity which will return all orders in a JSON response
167+
return new DoctrineEntityResponse(\App\Entity\Order::class); // Pass the Order entity which will return all orders in a JSON response
166168
}
167169
}
168170

composer.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
}
99
},
1010
"require": {
11-
"php": ">= 7.1.3",
12-
"symfony/framework-bundle": "^3.4 | ^4.0",
13-
"symfony/http-foundation": "^3.4 | ^4.0"
11+
"php": ">=7.4",
12+
"symfony/framework-bundle": "^4.0 || ^5.0",
13+
"symfony/http-foundation": "^4.0 || ^5.0",
14+
"twig/twig": "^2.0 || ^3.0"
1415
},
1516
"require-dev": {
1617
"phpunit/phpunit": "^7.0"

src/DependencyInjection/CompilerPass/ResponseHandlerCompilerPass.php

-40
This file was deleted.

src/DependencyInjection/SimpleResponseExtension.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Component\DependencyInjection\Extension\Extension;
1717
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1818

19-
class SimpleResponseExtension extends Extension
19+
final class SimpleResponseExtension extends Extension
2020
{
2121
/**
2222
* {@inheritdoc}
@@ -27,4 +27,4 @@ public function load(array $configs, ContainerBuilder $container)
2727

2828
$loader->import('*.yaml');
2929
}
30-
}
30+
}

src/Handler/RouteRedirectHandler.php

+11-13
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,28 @@
1111

1212
namespace SolidWorx\SimpleResponseBundle\Handler;
1313

14-
use SolidWorx\SimpleResponseBundle\ResponseHandlerInterface;
1514
use SolidWorx\SimpleResponseBundle\Response\RouteRedirectResponse;
16-
use Symfony\Component\HttpFoundation\RedirectResponse;
15+
use SolidWorx\SimpleResponseBundle\ResponseHandlerInterface;
1716
use Symfony\Component\HttpFoundation\Response;
18-
use Symfony\Component\Routing\RouterInterface;
17+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1918

20-
class RouteRedirectHandler implements ResponseHandlerInterface
19+
final class RouteRedirectHandler implements ResponseHandlerInterface
2120
{
22-
/**
23-
* @var RouterInterface
24-
*/
25-
private $router;
21+
private UrlGeneratorInterface $urlGenerator;
2622

27-
public function __construct(RouterInterface $router)
23+
public function __construct(UrlGeneratorInterface $urlGenerator)
2824
{
29-
$this->router = $router;
25+
$this->urlGenerator = $urlGenerator;
3026
}
3127

32-
public function handle($object): Response
28+
public function handle(Response $object): Response
3329
{
34-
return new RedirectResponse($this->router->generate($object->getRoute(), $object->getParameters()), $object->getStatusCode());
30+
/* @var RouteRedirectResponse $object */
31+
32+
return $object->setTargetUrl($this->urlGenerator->generate($object->getRoute(), $object->getParameters()));
3533
}
3634

37-
public function supports($object): bool
35+
public function supports(Response $object): bool
3836
{
3937
return $object instanceof RouteRedirectResponse;
4038
}

src/Handler/TemplateResponseHandler.php

+9-12
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,26 @@
1313

1414
use SolidWorx\SimpleResponseBundle\Response\TemplateResponse;
1515
use SolidWorx\SimpleResponseBundle\ResponseHandlerInterface;
16-
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
1716
use Symfony\Component\HttpFoundation\Response;
17+
use Twig\Environment;
1818

19-
class TemplateResponseHandler implements ResponseHandlerInterface
19+
final class TemplateResponseHandler implements ResponseHandlerInterface
2020
{
21-
/**
22-
* @var EngineInterface
23-
*/
24-
private $engine;
21+
private Environment $twig;
2522

26-
public function __construct(EngineInterface $engine)
23+
public function __construct(Environment $twig)
2724
{
28-
$this->engine = $engine;
25+
$this->twig = $twig;
2926
}
3027

31-
public function handle($object): Response
28+
public function handle(Response $object): Response
3229
{
3330
/* @var TemplateResponse $object */
34-
return $this->engine->renderResponse($object->getTemplate(), $object->getParams(), $object->getResponse());
31+
return $object->setContent($this->twig->render($object->getTemplate(), $object->getContext()));
3532
}
3633

37-
public function supports($object): bool
34+
public function supports(Response $object): bool
3835
{
3936
return $object instanceof TemplateResponse;
4037
}
41-
}
38+
}

src/Resources/config/services.yaml

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ services:
55
public: false
66
autoconfigure: true
77

8-
SolidWorx\SimpleResponseBundle\ResponseHandler: ~
8+
_instanceof:
9+
SolidWorx\SimpleResponseBundle\ResponseHandlerInterface:
10+
tags: ['solidworx.response_handler']
11+
12+
SolidWorx\SimpleResponseBundle\ResponseHandler:
13+
arguments: [!tagged_iterator 'solidworx.response_handler']
914

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

1318
# Core handlers
14-
SolidWorx\SimpleResponseBundle\Handler\TemplateResponseHandler:
15-
tags: ['response_handler.handler']
16-
17-
SolidWorx\SimpleResponseBundle\Handler\RouteRedirectHandler:
18-
tags: ['response_handler.handler']
19+
SolidWorx\SimpleResponseBundle\Handler\TemplateResponseHandler: ~
20+
SolidWorx\SimpleResponseBundle\Handler\RouteRedirectHandler: ~

src/Response/RouteRedirectResponse.php

+8-29
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,21 @@
1111

1212
namespace SolidWorx\SimpleResponseBundle\Response;
1313

14+
use Symfony\Component\HttpFoundation\RedirectResponse;
1415
use Symfony\Component\HttpFoundation\Response;
1516

16-
class RouteRedirectResponse
17+
final class RouteRedirectResponse extends RedirectResponse
1718
{
18-
/**
19-
* @var string
20-
*/
21-
private $route;
19+
private string $route;
2220

23-
/**
24-
* @var int
25-
*/
26-
private $statusCode;
21+
private array $parameters;
2722

28-
/**
29-
* @var array
30-
*/
31-
private $parameters;
32-
33-
public function __construct(string $route, array $parameters = [], int $statusCode = Response::HTTP_SEE_OTHER)
23+
public function __construct(string $route = '', array $parameters = [], int $status = Response::HTTP_FOUND, array $headers = [])
3424
{
3525
$this->route = $route;
36-
$this->statusCode = $statusCode;
3726
$this->parameters = $parameters;
27+
28+
parent::__construct('', $status, $headers);
3829
}
3930

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

50-
public function getStatusCode(): int
51-
{
52-
return $this->statusCode;
53-
}
54-
5541
public function setRoute(string $route): self
5642
{
5743
$this->route = $route;
@@ -65,11 +51,4 @@ public function setParameters(array $parameters): self
6551

6652
return $this;
6753
}
68-
69-
public function setStatusCode(int $statusCode): self
70-
{
71-
$this->statusCode = $statusCode;
72-
73-
return $this;
74-
}
75-
}
54+
}

src/Response/TemplateResponse.php

+18-26
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,41 @@
1313

1414
use Symfony\Component\HttpFoundation\Response;
1515

16-
class TemplateResponse
16+
final class TemplateResponse extends Response
1717
{
18-
/**
19-
* @var string
20-
*/
21-
private $template;
22-
23-
/**
24-
* @var array
25-
*/
26-
private $params;
27-
28-
/**
29-
* @var Response
30-
*/
31-
private $response;
32-
33-
public function __construct(string $template = null, array $params = [], Response $response = null)
18+
private string $template;
19+
20+
private array $context;
21+
22+
public function __construct(string $template = '', array $context = [], int $status = Response::HTTP_OK, array $headers = [])
3423
{
3524
$this->template = $template;
36-
$this->params = $params;
37-
$this->response = $response ?: new Response();
25+
$this->context = $context;
26+
27+
parent::__construct('', $status, $headers);
3828
}
3929

4030
public function getTemplate(): ?string
4131
{
4232
return $this->template;
4333
}
4434

45-
public function getParams(): array
35+
public function getContext(): array
4636
{
47-
return $this->params;
37+
return $this->context;
4838
}
4939

50-
public function getResponse(): ?Response
40+
public function setTemplate(string $template): self
5141
{
52-
return $this->response;
42+
$this->template = $template;
43+
44+
return $this;
5345
}
5446

55-
public function setTemplate(string $template): self
47+
public function setContext(array $context): self
5648
{
57-
$this->template = $template;
49+
$this->context = $context;
5850

5951
return $this;
6052
}
61-
}
53+
}

0 commit comments

Comments
 (0)