Skip to content

Commit 7cdc45b

Browse files
committed
feat: Add initial additions
1 parent 64be2f7 commit 7cdc45b

File tree

18 files changed

+5667
-51
lines changed

18 files changed

+5667
-51
lines changed

.editorconfig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
indent_size = 4
8+
indent_style = space
9+
insert_final_newline = true
10+
max_line_length = 120
11+
tab_width = 4
12+
trim_trailing_whitespace = true
13+
14+
[{Dockerfile,Dockerfile.*}]
15+
indent_size = 2
16+
17+
[.github/**.yaml]
18+
indent_size = 2
19+
20+
[*.md]
21+
trim_trailing_whitespace = false

.gitignore

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,6 @@
1-
# Cache and logs (Symfony2)
2-
/app/cache/*
3-
/app/logs/*
4-
!app/cache/.gitkeep
5-
!app/logs/.gitkeep
6-
7-
# Email spool folder
8-
/app/spool/*
9-
10-
# Cache, session files and logs (Symfony3)
11-
/var/cache/*
12-
/var/logs/*
13-
/var/sessions/*
14-
!var/cache/.gitkeep
15-
!var/logs/.gitkeep
16-
!var/sessions/.gitkeep
17-
18-
# Logs (Symfony4)
19-
/var/log/*
20-
!var/log/.gitkeep
21-
22-
# Parameters
23-
/app/config/parameters.yml
24-
/app/config/parameters.ini
25-
26-
# Managed by Composer
27-
/app/bootstrap.php.cache
28-
/var/bootstrap.php.cache
29-
/bin/*
30-
!bin/console
31-
!bin/symfony_requirements
1+
/.phpunit.cache
322
/vendor/
333

34-
# Assets and user uploads
35-
/web/bundles/
36-
/web/uploads/
37-
38-
# PHPUnit
39-
/app/phpunit.xml
40-
/phpunit.xml
41-
42-
# Build data
43-
/build/
44-
45-
# Composer PHAR
4+
/.php-cs-fixer.cache
465
/composer.phar
47-
48-
# Backup entities generated with doctrine:generate:entities command
49-
**/Entity/*~
50-
51-
# Embedded web-server pid file
52-
/.web-server-pid
6+
/phpunit.xml

.php-cs-fixer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->exclude(['var', 'vendor', 'migrations', 'fixtures'])
5+
->in(__DIR__)
6+
;
7+
8+
$config = new PhpCsFixer\Config();
9+
return $config
10+
->setRules([
11+
'@PSR12' => true,
12+
'strict_param' => true,
13+
'array_syntax' => ['syntax' => 'short'],
14+
])
15+
->setRiskyAllowed(true)
16+
->setFinder($finder);

README.md

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,75 @@
1-
# ddd-symfony-bundle
2-
Additions for domain driven development inside Symfony.
1+
# Symfony Bundle for DDD
2+
3+
Various additions for [domain driven development](https://martinfowler.com/tags/domain%20driven%20design.html) inside Symfony.
4+
5+
## Installation
6+
7+
To use this bundle, require it in Composer
8+
9+
```bash
10+
composer require geekcell/ddd-bundle
11+
```
12+
13+
## Aggregate Root
14+
15+
Extend from `AggregateRoot` to record and commit domain events. Domain events must implement the (marker) interface `DomainEvent`. Events will be dispatched via the currently configured Symfony event dispatcher.
16+
17+
### Example Usage
18+
19+
```php
20+
use GeekCell\DDDBundle\Domain\Event\DomainEvent;
21+
use GeekCell\DDDBundle\Domain\Model\AggregateRoot;
22+
23+
class OrderPlacedEvent implements DomainEvent
24+
{
25+
...
26+
}
27+
28+
class Order extends AggregateRoot
29+
{
30+
public function save(): void
31+
{
32+
$this->record(new OrderPlacedEvent());
33+
}
34+
35+
...
36+
}
37+
38+
$order = new Order();
39+
$order->save();
40+
$order->commit(); // <- Events will be dispatched
41+
```
42+
43+
_Hint: If you want to dispatch an event directly, use `AggregateRoot::dispatch()` instead of `AggregateRoot::record()`._
44+
45+
If you cannot (or don't want to) extend from `AggregateRoot`, you can alternative use `DispatchableTrait` to add dispatching capabilities to any class. The former is however the recommended way.
46+
47+
## Supporting Tools
48+
49+
### Facades
50+
51+
Facades are heavily inspired by [Laravel's Facades](https://laravel.com/docs/facades) and are more or less singletons on steroids. They are basically a shortcut to services inside the DIC.
52+
53+
```php
54+
use GeekCell\DDDBundle\Support\Facades\EventDispatcher;
55+
56+
EventDispatcher::dispatch($someEvent);
57+
58+
// same as: $container->get('event_dispatcher')->dispatch($someEvent);
59+
```
60+
61+
You can create your own facades by extending from `Facade` and implementing the `Facade::getFacadeAccessor()` method to return the DIC service alias.
62+
63+
```php
64+
use GeekCell\DDDBundle\Support\Facades\Facade;
65+
66+
class Logger extends Facade
67+
{
68+
protected static function getFacadeAccessor(): string
69+
{
70+
return 'logger';
71+
}
72+
}
73+
```
74+
75+
Although facades are better testable than regular singletons, it is highly recommended to only use them sparringly and always prefer normal dependency injection when possible.

composer.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "geekcell/ddd-bundle",
3+
"type": "symfony-bundle",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Pascal Cremer",
8+
"email": "pascal.cremer@geekcell.io"
9+
}
10+
],
11+
"require": {
12+
"symfony/http-kernel": "^6.2",
13+
"symfony/event-dispatcher": "^6.2",
14+
"symfony/dependency-injection": "^6.2",
15+
"symfony/config": "^6.2",
16+
"beberlei/assert": "^3.3"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"GeekCell\\DDDBundle\\": "src"
21+
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"GeekCell\\DDDBundle\\Tests\\": "tests/"
26+
}
27+
},
28+
"require-dev": {
29+
"mockery/mockery": "^1.5",
30+
"phpunit/phpunit": "^9.5",
31+
"phpstan/phpstan": "^1.9",
32+
"friendsofphp/php-cs-fixer": "^3.13",
33+
"phpstan/phpstan-mockery": "^1.1"
34+
},
35+
"scripts": {
36+
"gc:tests": "phpunit --testdox --colors=always",
37+
"gc:cs-lint": "php-cs-fixer fix --config .php-cs-fixer.php --diff -vvv --dry-run",
38+
"gc:cs-fix": "php-cs-fixer fix --config .php-cs-fixer.php -vvv || true",
39+
"gc:phpstan": "phpstan analyse --memory-limit=2G --no-progress --level=8"
40+
}
41+
}

0 commit comments

Comments
 (0)