|
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\Query\Handler\Locator; |
|
13
|
|
|
|
|
14
|
|
|
use GpsLab\Component\Query\Handler\QuerySubscriber; |
|
15
|
|
|
use GpsLab\Component\Query\Query; |
|
16
|
|
|
use Psr\Container\ContainerInterface; |
|
17
|
|
|
|
|
18
|
|
|
class ContainerQueryHandlerLocator implements QueryHandlerLocator |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ContainerInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $container; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $query_handler_ids = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ContainerInterface $container |
|
32
|
|
|
*/ |
|
33
|
6 |
|
public function __construct(ContainerInterface $container) |
|
34
|
|
|
{ |
|
35
|
6 |
|
$this->container = $container; |
|
36
|
6 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Query $query |
|
40
|
|
|
* |
|
41
|
|
|
* @return callable|null |
|
42
|
|
|
*/ |
|
43
|
6 |
|
public function findHandler(Query $query): ?callable |
|
44
|
|
|
{ |
|
45
|
6 |
|
return $this->lazyLoad(get_class($query)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $query_name |
|
50
|
|
|
* @param string $service |
|
51
|
|
|
* @param string $method |
|
52
|
|
|
*/ |
|
53
|
5 |
|
public function registerService(string $query_name, string $service, string $method = '__invoke'): void |
|
54
|
|
|
{ |
|
55
|
5 |
|
$this->query_handler_ids[$query_name] = [$service, $method]; |
|
56
|
5 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $service_name |
|
60
|
|
|
* @param string $class_name |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function registerSubscriberService(string $service_name, string $class_name): void |
|
63
|
|
|
{ |
|
64
|
1 |
|
$get_subscribed_queries = [$class_name, 'getSubscribedQueries']; |
|
65
|
1 |
|
if (is_callable($get_subscribed_queries) && is_a($class_name, QuerySubscriber::class, true)) { |
|
66
|
1 |
|
foreach ($get_subscribed_queries() as $query_name => $method) { |
|
67
|
1 |
|
$this->registerService($query_name, $service_name, $method); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $query_name |
|
74
|
|
|
* |
|
75
|
|
|
* @return callable|null |
|
76
|
|
|
*/ |
|
77
|
6 |
|
private function lazyLoad(string $query_name): ?callable |
|
78
|
|
|
{ |
|
79
|
6 |
|
if (isset($this->query_handler_ids[$query_name])) { |
|
80
|
5 |
|
[$service, $method] = $this->query_handler_ids[$query_name]; |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
5 |
|
return $this->resolve($this->container->get($service), $method); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param mixed $service |
|
90
|
|
|
* @param string $method |
|
91
|
|
|
* |
|
92
|
|
|
* @return callable|null |
|
93
|
|
|
*/ |
|
94
|
5 |
|
private function resolve($service, string $method): ?callable |
|
95
|
|
|
{ |
|
96
|
5 |
|
if (is_callable($service)) { |
|
97
|
1 |
|
return $service; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
4 |
|
$handler = [$service, $method]; |
|
101
|
|
|
|
|
102
|
4 |
|
if (is_callable($handler)) { |
|
103
|
2 |
|
return $handler; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
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.