1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* GpsLab component. |
6
|
|
|
* |
7
|
|
|
* @author Peter Gribanov <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
9
|
|
|
* @license http://opensource.org/licenses/MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace GpsLab\Component\Command\Handler\Locator; |
13
|
|
|
|
14
|
|
|
use GpsLab\Component\Command\Command; |
15
|
|
|
use GpsLab\Component\Command\Handler\CommandSubscriber; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
19
|
|
|
|
20
|
|
|
class SymfonyContainerCommandHandlerLocator implements CommandHandlerLocator, ContainerAwareInterface |
21
|
|
|
{ |
22
|
|
|
use ContainerAwareTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array[] |
26
|
|
|
*/ |
27
|
|
|
private $command_handler_ids = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Command $command |
31
|
|
|
* |
32
|
|
|
* @return callable|null |
33
|
|
|
*/ |
34
|
7 |
|
public function findHandler(Command $command): ?callable |
35
|
|
|
{ |
36
|
7 |
|
return $this->lazyLoad(get_class($command)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $command_name |
41
|
|
|
* @param string $service |
42
|
|
|
* @param string $method |
43
|
|
|
*/ |
44
|
6 |
|
public function registerService(string $command_name, string $service, string $method = '__invoke'): void |
45
|
|
|
{ |
46
|
6 |
|
$this->command_handler_ids[$command_name] = [$service, $method]; |
47
|
6 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $service_name |
51
|
|
|
* @param string $class_name |
52
|
|
|
*/ |
53
|
1 |
|
public function registerSubscriberService(string $service_name, string $class_name): void |
54
|
|
|
{ |
55
|
1 |
|
$get_subscribed_commands = [$class_name, 'getSubscribedCommands']; |
56
|
1 |
|
if (is_callable($get_subscribed_commands) && is_a($class_name, CommandSubscriber::class, true)) { |
57
|
1 |
|
foreach ($get_subscribed_commands() as $command_name => $method) { |
58
|
1 |
|
$this->registerService($command_name, $service_name, $method); |
59
|
|
|
} |
60
|
|
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $command_name |
65
|
|
|
* |
66
|
|
|
* @return callable|null |
67
|
|
|
*/ |
68
|
7 |
|
private function lazyLoad(string $command_name): ?callable |
69
|
|
|
{ |
70
|
7 |
|
if ($this->container instanceof ContainerInterface && isset($this->command_handler_ids[$command_name])) { |
71
|
5 |
|
[$service, $method] = $this->command_handler_ids[$command_name]; |
|
|
|
|
72
|
|
|
|
73
|
5 |
|
return $this->resolve($this->container->get($service), $method); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $service |
81
|
|
|
* @param string $method |
82
|
|
|
* |
83
|
|
|
* @return callable|null |
84
|
|
|
*/ |
85
|
5 |
|
private function resolve($service, string $method): ?callable |
86
|
|
|
{ |
87
|
5 |
|
if (is_callable($service)) { |
88
|
1 |
|
return $service; |
89
|
|
|
} |
90
|
|
|
|
91
|
4 |
|
$handler = [$service, $method]; |
92
|
|
|
|
93
|
4 |
|
if (is_callable($handler)) { |
94
|
2 |
|
return $handler; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
return null; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.