-
Notifications
You must be signed in to change notification settings - Fork 1
/
blog.phpt
76 lines (64 loc) · 2.71 KB
/
blog.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
--TEST--
Complete example of usage in a basic app.
--FILE--
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Star\Component\DomainEvent\Messaging\MessageMapBus;
use Star\Component\DomainEvent\Ports\Symfony\SymfonyPublisher;
use Star\Example\Blog\Application\Bridge\Event\UserWasRegistered;
use Star\Example\Blog\Application\Command as AppCommands;
use Star\Example\Blog\Application\Http\Controller\PostController;
use Star\Example\Blog\Application\Processor;
use Star\Example\Blog\Domain\Command\Blog as BlogCommands;
use Star\Example\Blog\Domain\Command\Post as PostCommands;
use Star\Example\Blog\Domain\Query\Post as PostQueries;
use Star\Example\Blog\Infrastructure\Persistence\InMemory\PostCollection;
use Symfony\Component\EventDispatcher\EventDispatcher;
// Setup application. Would normally be in a DI container, or in a bootstrap script
$messageBus = new MessageMapBus();
$publisher = new SymfonyPublisher(new EventDispatcher());
$posts = new PostCollection();
$messageBus->registerHandler(
AppCommands\RegisterUser::class,
new AppCommands\RegisterUserHandler($publisher)
);
$messageBus->registerHandler(
BlogCommands\CreateBlog::class,
new BlogCommands\CreateBlogHandler($publisher)
);
$messageBus->registerHandler(
PostCommands\CreateNewPost::class,
new PostCommands\CreateNewPostHandler($posts, $publisher)
);
$messageBus->registerHandler(
PostCommands\PublishPost::class,
new PostCommands\PublishPostHandler($posts, $publisher)
);
$messageBus->registerHandler(
PostQueries\SearchForPost::class,
$postIndex = new PostQueries\SearchForPostHandler()
);
$publisher->subscribe(new Processor\CreateBlogOnUserRegister($messageBus));
$publisher->subscribe($postIndex);
$controller = new PostController($messageBus, $messageBus);
// Requests made by a user to the server
echo $controller->search('cqrs') . "\n";
$publisher->publish(new UserWasRegistered($blogName = 'my-blog'));
echo $controller->createPost($blogName, ['data' => ['title' => 'DDD rocks !!!']]) . "\n"; // not published
echo $controller->createPost($blogName, ['data' => ['title' => 'Example for cqrs concepts.']]) . "\n"; // published
echo $controller->createPost($blogName, ['data' => ['title' => 'Another DDD post.']]) . "\n"; // published
$controller->publish(2);
$controller->publish(3);
echo $controller->search('cqrs') . "\n";
echo $controller->search('DDD') . "\n";
?>
--EXPECTF--
Searching: cqrs.
[]
{"id":"1"}
{"id":"2"}
{"id":"3"}
Searching: cqrs.
[{"id":"2","title":"Example for cqrs concepts.","blogName":"my-blog","published":true,"publishedAt":"2000-01-01","publishedBy":"username"}]
Searching: DDD.
[{"id":"3","title":"Another DDD post.","blogName":"my-blog","published":true,"publishedAt":"2000-01-01","publishedBy":"username"}]