Skip to content

Commit 786a0f1

Browse files
committed
Initial import
0 parents  commit 786a0f1

13 files changed

+2459
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
.idea

.scrutinizer.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
build:
2+
tests:
3+
override:
4+
-
5+
command: 'vendor/bin/phpunit tests --configuration phpunit.xml.dist --coverage-clover=code-coverage'
6+
coverage:
7+
file: 'code-coverage'
8+
format: 'clover'
9+
nodes:
10+
tests: true
11+
analysis:
12+
tests:
13+
override:
14+
- php-scrutinizer-run
15+
-
16+
command: phpcs-run
17+
use_website_config: true
18+
19+
filter:
20+
excluded_paths:
21+
- 'tests/*'
22+
coding_style:
23+
php:
24+
spaces:
25+
around_operators:
26+
concatenation: true
27+
checks:
28+
php: true

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: php
2+
php:
3+
- '7.1'
4+
- '7.2'
5+
- hhvm
6+
- nightly
7+
install: composer install
8+
script: ./vendor/bin/phpunit --configuration phpunit.xml.dist

LICENSE.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# The MIT License (MIT)
3+
4+
Copyright (c) 2018 DMT-software <bas@dmt-software.nl>
5+
6+
> Permission is hereby granted, free of charge, to any person obtaining a copy
7+
> of this software and associated documentation files (the "Software"), to deal
8+
> in the Software without restriction, including without limitation the rights
9+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
> copies of the Software, and to permit persons to whom the Software is
11+
> furnished to do so, subject to the following conditions:
12+
>
13+
> The above copyright notice and this permission notice shall be included in
14+
> all copies or substantial portions of the Software.
15+
>
16+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
> THE SOFTWARE.

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Command-Bus-Validator
2+
3+
## Install
4+
`composer require dmt-software/command-bus-validator`
5+
6+
## Usage
7+
8+
### Default usage
9+
10+
By default this middleware uses the StaticMethodLoader of the [Symfony Validator](https://symfony.com/doc/current/components/validator.html) component.
11+
If you have installed both `doctrine/annotations` and `doctrine/cache`, this default behaviour is extended with the AnnotationLoader.
12+
13+
Configure and adding this middleware to the commandBus:
14+
```php
15+
<?php // src/CommandBus/builder.php
16+
17+
use DMT\CommandBus\Validator\ValidationMiddleware;
18+
use League\Tactician\CommandBus;
19+
use League\Tactician\Handler\CommandHandlerMiddleware;
20+
21+
/** @var CommandHandlerMiddleware $commandHandlerMiddleware */
22+
$commandBus = new CommandBus(
23+
[
24+
new ValidationMiddleware(),
25+
$commandHandlerMiddleware
26+
]
27+
);
28+
```
29+
After the CommandBus is added, the commands it receives will be validated when the `handle` method is called:
30+
```php
31+
<?php
32+
33+
use DMT\CommandBus\Validator\ValidationException;
34+
use League\Tactician\CommandBus;
35+
36+
try {
37+
/** @var object $command */
38+
/** @var CommandBus $commandBus */
39+
$result = $commandBus->handle($command);
40+
} catch (ValidationException $exception) {
41+
$violations = $exception->getViolations();
42+
foreach ($violations as $violation) {
43+
echo $violation->getMessage(); // outputs: the violation message(s)
44+
}
45+
}
46+
```
47+
48+
### Using custom configured validator
49+
50+
The validator can also be plugged unto the middleware by providing it to the middleware constructor.
51+
52+
This example uses a FileLoader to determine the constraints for a command.
53+
```php
54+
<?php // src/CommandBus/builder.php
55+
56+
use DMT\CommandBus\Validator\ValidationMiddleware;
57+
use League\Tactician\CommandBus;
58+
use League\Tactician\Handler\CommandHandlerMiddleware;
59+
use Symfony\Component\Validator\ValidatorBuilder;
60+
61+
$validator = (new ValidatorBuilder())
62+
->addYamlMapping('config/validation.yaml')
63+
->getValidator();
64+
65+
/** @var CommandHandlerMiddleware $commandHandlerMiddleware */
66+
$commandBus = new CommandBus(
67+
[
68+
new ValidationMiddleware($validator),
69+
$commandHandlerMiddleware
70+
]
71+
);
72+
73+
```
74+
75+
## Further reading
76+
77+
- [Tactician CommandBus](http://tactician.thephpleague.com/)
78+
- [Validator Loaders](https://symfony.com/doc/current/components/validator/resources.html)
79+

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "dmt-software/command-bus-validator",
3+
"description": "A CommandBus middleware to validate commands using the Symfony Validator",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Bas de Mes",
9+
"email": "bas@dmt-software.nl"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.1",
14+
"league/tactician": "^1.0",
15+
"symfony/validator": "^4.0"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"DMT\\CommandBus\\": "src/"
20+
}
21+
},
22+
"suggest": {
23+
"doctrine/annotations": "Enables adding constraints using annotations",
24+
"doctrine/cache": "Enables adding constraints using annotations"
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "^7.1",
28+
"doctrine/annotations": "^1.6",
29+
"doctrine/cache": "^1.7"
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"DMT\\Test\\CommandBus\\": "tests/"
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)