Skip to content

Commit 99f6da7

Browse files
committed
Adds a readme
0 parents  commit 99f6da7

15 files changed

+396
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Dittto\CustomRequestBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
class Configuration implements ConfigurationInterface
9+
{
10+
public function getConfigTreeBuilder()
11+
{
12+
$treeBuilder = new TreeBuilder();
13+
$treeBuilder->root('dittto_custom_request');
14+
15+
return $treeBuilder;
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Dittto\CustomRequestBundle\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\Config\FileLocator;
7+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8+
use Symfony\Component\DependencyInjection\Loader;
9+
10+
class DitttoCustomRequestExtension extends Extension
11+
{
12+
public function load(array $configs, ContainerBuilder $container)
13+
{
14+
$configuration = new Configuration();
15+
$this->processConfiguration($configuration, $configs);
16+
17+
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
18+
$loader->load('services.yml');
19+
}
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace Dittto\CustomRequestBundle\DependencyInjection;
3+
4+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\DependencyInjection\Reference;
7+
8+
class DitttoRequestFilterPass implements CompilerPassInterface
9+
{
10+
const REQUEST_SERVICE_NAME = 'dittto_custom_request.requests';
11+
12+
const TAG_NAME = 'dittto.request_filter';
13+
14+
const SLOT_ATTRIBUTE_NAME = 'slot';
15+
16+
public function process(ContainerBuilder $container):void
17+
{
18+
// drop out if for some reason the request class has gone missing
19+
if (!$container->has(self::REQUEST_SERVICE_NAME)) {
20+
return;
21+
}
22+
23+
$definition = $container->findDefinition(self::REQUEST_SERVICE_NAME);
24+
$taggedServices = $container->findTaggedServiceIds(self::TAG_NAME);
25+
26+
foreach ($taggedServices as $id => $tags) {
27+
foreach ($tags as $attributes) {
28+
$classReference = new Reference($id);
29+
$slot = $attributes[self::SLOT_ATTRIBUTE_NAME] ?? null;
30+
$definition->addMethodCall('addFilter', [$classReference, $slot]);
31+
}
32+
}
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
namespace Dittto\CustomRequestBundle\DependencyInjection;
3+
4+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\DependencyInjection\Reference;
7+
8+
class DitttoRequestPass implements CompilerPassInterface
9+
{
10+
const REQUEST_SERVICE_NAME = 'dittto_custom_request.requests';
11+
12+
const TAG_NAME = 'dittto.request';
13+
14+
public function process(ContainerBuilder $container):void
15+
{
16+
// drop out if for some reason the request class has gone missing
17+
if (!$container->has(self::REQUEST_SERVICE_NAME)) {
18+
return;
19+
}
20+
21+
$definition = $container->findDefinition(self::REQUEST_SERVICE_NAME);
22+
$taggedServices = $container->findTaggedServiceIds(self::TAG_NAME);
23+
24+
foreach ($taggedServices as $id => $tags) {
25+
$definition->addMethodCall('addSupportedClass', [new Reference($id)]);
26+
}
27+
}
28+
}

DitttoCustomRequestBundle.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Dittto\CustomRequestBundle;
3+
4+
use Dittto\CustomRequestBundle\DependencyInjection\{DitttoRequestPass, DitttoRequestFilterPass};
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\HttpKernel\Bundle\Bundle;
7+
8+
class DitttoCustomRequestBundle extends Bundle
9+
{
10+
public function build(ContainerBuilder $container)
11+
{
12+
$container->addCompilerPass(new DitttoRequestPass());
13+
$container->addCompilerPass(new DitttoRequestFilterPass());
14+
}
15+
}

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Symfony custom requests
2+
3+
## What is it?
4+
5+
To help accomplish the "Thin controllers, fat models" ideal, this bundle allows you to create custom request objects that you can use for validating your input before it gets to the controller.
6+
7+
You can use these to make sure certain fields are set with certain dynamic values, or validate form data without having to add additional logic to your controller.
8+
9+
The aim is that your controller knows that it has the correct data and therefore can start working with it immediately, instead of having to double-check it's input is valid.
10+
11+
The new custom request object are defined via services, so can have any other services passed to them. This means your validation steps can use data-sources such as other APIs, databases, Elasticsearch, etc.
12+
13+
## How to use it
14+
15+
The first step is to update your `composer.json` with `dittto/symfony-custom-request`. You may need to also specify the an entry in the `repositories` for the github repository.
16+
17+
After you've added and installed this bundle, you'll need to add it to the `AppKernel.php` file:
18+
19+
```php
20+
class AppKernel extends Kernel {
21+
public function registerBundles() {
22+
$bundles = [
23+
...
24+
new \Dittto\CustomRequestBundle\DitttoCustomRequestBundle(),
25+
```
26+
27+
Next we'll create our custom request. This is going to be a simple check that looks for a query string containing `token=allowed`. The test below is stored at `AppBundle/Request/TestRequest.php`.
28+
29+
```php
30+
use Dittto\CustomRequestBundle\Request\AbstractRequestType;
31+
32+
class TestRequest extends AbstractRequestType {
33+
public function validate():bool {
34+
return $this->getOriginalRequest()->query->get('token') === 'allowed';
35+
}
36+
}
37+
```
38+
39+
Update the services for our new TestRequest. The `tag` is important as this enables us to know this is a custom request and can be used as a controller parameter.
40+
41+
```yaml
42+
services:
43+
test_request:
44+
class: AppBundle\Request\TestRequest
45+
tags: [ { name: dittto.request } ]
46+
```
47+
48+
Lastly, we need to tell our controller to use our custom request using it's parameters.
49+
50+
```php
51+
class DefaultController extends Controller {
52+
public function indexAction(TestRequest $request):Response {
53+
...
54+
}
55+
}
56+
```
57+
58+
## Filters
59+
60+
There is a filter chain available for altering how this code handles itself after the `validate()` method has been run. By default there is a filter in place that if a `GET` request fails, then a 400 exception is thrown.
61+
62+
You can add as many additional filters as you like to the chain to create automated responses based on certain request types.
63+
64+
New filters are added by creating a compatible filter (`RequestFilterInterface`) and adding a tag of `dittto.request_filter` when defining it as a service.
65+
66+
To make the filters run in a particular order, add a `slot` as a tag with a positive integer as it's value. These run in the order of lowest number first. If you assign 2 or more filters to the same slot, the first-assigned filter will take the slot and block any others from taking it's place.
67+
68+
### How to override default filters
69+
70+
Slots in local configs seem to always install before vendor-defined configs, so you can use the following to replace the default filter.
71+
72+
```yaml
73+
services:
74+
override_exception_on_failed_get:
75+
class: Ditto\CustomRequestBundle\Request\Filter\NullFilterRequest
76+
tags: [ { name: dittto.request_filter, slot: 10 } ]
77+
```
78+
79+
## TODO
80+
81+
- tests

Request/AbstractRequestType.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace Dittto\CustomRequestBundle\Request;
3+
4+
use Symfony\Component\HttpFoundation\Request;
5+
6+
abstract class AbstractRequestType implements RequestTypeInterface
7+
{
8+
private $request;
9+
10+
public function setOriginalRequest(Request $request):RequestTypeInterface
11+
{
12+
$this->request = $request;
13+
14+
return clone $this;
15+
}
16+
17+
public function getOriginalRequest():Request
18+
{
19+
return $this->request;
20+
}
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace Dittto\CustomRequestBundle\Request\Filter;
3+
4+
use Dittto\CustomRequestBundle\Request\RequestTypeInterface;
5+
use Symfony\Component\HttpKernel\Exception\HttpException;
6+
7+
class ExceptionOnFailedGetRequest implements RequestFilterInterface
8+
{
9+
public function filterRequest(RequestTypeInterface $request, bool $isValid):bool
10+
{
11+
if ($request->getOriginalRequest()->isMethod('GET') && !$isValid) {
12+
throw new HttpException(400, 'Failed validation');
13+
}
14+
15+
return $isValid;
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace Dittto\CustomRequestBundle\Request\Filter;
3+
4+
use Dittto\CustomRequestBundle\Request\RequestTypeInterface;
5+
6+
class NullFilterRequest implements RequestFilterInterface
7+
{
8+
public function filterRequest(RequestTypeInterface $request, bool $isValid):bool
9+
{
10+
return $isValid;
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace Dittto\CustomRequestBundle\Request\Filter;
3+
4+
use Dittto\CustomRequestBundle\Request\RequestTypeInterface;
5+
6+
interface RequestFilterInterface
7+
{
8+
public function filterRequest(RequestTypeInterface $request, bool $isValid):bool;
9+
}

0 commit comments

Comments
 (0)