-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path3-intermediate-custom-naming-conventions.php
49 lines (42 loc) · 1.52 KB
/
3-intermediate-custom-naming-conventions.php
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
<?php
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/repeated-sample-code.php';
/**
* Tactician is meant to be very customizable, it makes no assumptions about
* your code.
*
* For example, let's say you don't like that your handler methods need to be
* called "handleNameOfYourCommand" and you'd rather it always use a method
* named "handle".
*
* We can write a custom MethodNameInflector for that:
*/
use Tactician\CommandBus\Command;
use Tactician\CommandBus\Handler\MethodNameInflector\MethodNameInflector;
class MyCustomInflector implements MethodNameInflector
{
// You can use the command and commandHandler to generate any name you
// prefer but here, we'll always return the same one.
public function inflect(Command $command, $commandHandler)
{
return 'handle';
}
}
// And we'll create a new handler with the method name we prefer to invoke
class NewRegisterUserHandler
{
public function handle($command)
{
echo "See, Tactician now calls the handle method we prefer!\n";
}
}
// Now let's create our command bus again, but this time using our custom
// method naming strategy
use Tactician\CommandBus\HandlerExecutionCommandBus;
$locator->addHandler(new NewRegisterUserHandler(), RegisterUserCommand::class);
$commandBus = new HandlerExecutionCommandBus($locator, new MyCustomInflector());
// Controller Code time!
$command = new RegisterUserCommand();
$command->emailAddress = '[email protected]';
$command->password = 'secret';
$commandBus->execute($command);