1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* GpsLab component. |
5
|
|
|
* |
6
|
|
|
* @author Peter Gribanov <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
8
|
|
|
* @license http://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace GpsLab\Component\Query\Handler\Locator; |
12
|
|
|
|
13
|
|
|
use GpsLab\Component\Query\Query; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
17
|
|
|
|
18
|
|
|
class SymfonyContainerQueryHandlerLocator implements QueryHandlerLocator, ContainerAwareInterface |
19
|
|
|
{ |
20
|
|
|
use ContainerAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $query_handler_ids = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Query $query |
29
|
|
|
* |
30
|
|
|
* @return callable|null |
31
|
|
|
*/ |
32
|
5 |
|
public function findHandler(Query $query) |
33
|
|
|
{ |
34
|
5 |
|
return $this->lazyLoad(get_class($query)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $query_name |
39
|
|
|
* @param string $service |
40
|
|
|
* @param string $method |
41
|
|
|
*/ |
42
|
5 |
|
public function registerService($query_name, $service, $method = '__invoke') |
43
|
|
|
{ |
44
|
5 |
|
$this->query_handler_ids[$query_name] = [$service, $method]; |
45
|
5 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $query_name |
49
|
|
|
* |
50
|
|
|
* @return callable |
51
|
|
|
*/ |
52
|
5 |
|
private function lazyLoad($query_name) |
53
|
|
|
{ |
54
|
5 |
|
if ($this->container instanceof ContainerInterface && isset($this->query_handler_ids[$query_name])) { |
55
|
4 |
|
list($service, $method) = $this->query_handler_ids[$query_name]; |
56
|
|
|
|
57
|
4 |
|
return $this->resolve($this->container->get($service), $method); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param mixed $service |
65
|
|
|
* @param string $method |
66
|
|
|
* |
67
|
|
|
* @return callable|null |
68
|
|
|
*/ |
69
|
4 |
|
private function resolve($service, $method) |
70
|
|
|
{ |
71
|
4 |
|
if (is_callable($service)) { |
72
|
1 |
|
return $service; |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
if (is_callable([$service, $method])) { |
76
|
1 |
|
return [$service, $method]; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return null; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|