1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis; |
4
|
|
|
|
5
|
|
|
use League\Container\Container; |
6
|
|
|
use League\Container\ReflectionContainer; |
7
|
|
|
use OpenTelemetry\Tracing\Tracer; |
8
|
|
|
use Tarantool\Mapper\Mapper; |
9
|
|
|
use Tarantool\Mapper\Plugin\Annotation; |
10
|
|
|
use Tarantool\Mapper\Plugin\Procedure as ProcedurePlugin; |
11
|
|
|
use Tarantool\Mapper\Procedure; |
12
|
|
|
use Tarantool\Mapper\Repository; |
13
|
|
|
|
14
|
|
|
class Application extends Container |
15
|
|
|
{ |
16
|
|
|
use Toolkit; |
17
|
|
|
|
18
|
|
|
private $reflection; |
19
|
|
|
|
20
|
|
|
public function __construct(string $root) |
21
|
|
|
{ |
22
|
|
|
parent::__construct(); |
23
|
|
|
|
24
|
|
|
$fs = new Filesystem($this, $root); |
25
|
|
|
|
26
|
|
|
$this->share(Application::class, $this); |
27
|
|
|
$this->share(Container::class, $this); |
28
|
|
|
$this->share(Filesystem::class, $fs); |
29
|
|
|
|
30
|
|
|
$this->addServiceProvider(Provider\ClickhouseProvider::class); |
31
|
|
|
$this->addServiceProvider(Provider\CoreProvider::class); |
32
|
|
|
$this->addServiceProvider(Provider\GuzzleProvider::class); |
33
|
|
|
$this->addServiceProvider(Provider\OpenTelemetryProvider::class); |
34
|
|
|
$this->addServiceProvider(Provider\PoolProvider::class); |
35
|
|
|
$this->addServiceProvider(Provider\PredisProvider::class); |
36
|
|
|
$this->addServiceProvider(Provider\ServiceProvider::class); |
37
|
|
|
$this->addServiceProvider(Provider\TarantoolProvider::class); |
38
|
|
|
|
39
|
|
|
foreach ($fs->listClasses('Provider') as $provider) { |
40
|
|
|
$this->addServiceProvider($provider); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->delegate($this->reflection = new ReflectionContainer()); |
44
|
|
|
|
45
|
|
|
// toolkit helpers fix |
46
|
|
|
$this->app = $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
56 |
|
public function dispatch(string $job, array $params = [], string $service = null) |
50
|
|
|
{ |
51
|
56 |
|
if ($service !== null) { |
52
|
|
|
if ($this->get(Service::class)->getName() == $service) { |
53
|
|
|
$service = null; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
56 |
|
return $this->get(Cache::class) |
58
|
|
|
->wrap([$job, $params, $service], function() use ($job, $params, $service) { |
59
|
56 |
|
$span = $this->get(Tracer::class)->createSpan($job); |
60
|
56 |
|
foreach ($params as $k => $v) { |
61
|
10 |
|
if (is_numeric($v) || is_string($v)) { |
62
|
7 |
|
$span->setAttribute($k, $v); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
56 |
|
$serviceName = $this->get(Service::class)->getName(); |
67
|
56 |
|
$dispatcher = $this->get(Dispatcher::class); |
68
|
56 |
|
$runner = $this->get(Runner::class); |
69
|
|
|
|
70
|
56 |
|
if ($service === null && $runner->hasJob($job)) { |
71
|
56 |
|
$result = $runner->dispatch($job, $params); |
72
|
1 |
|
} elseif ($service === null && explode('.', $job)[0] == $serviceName) { |
73
|
|
|
$result = $runner->dispatch($job, $params); |
74
|
|
|
} else { |
75
|
1 |
|
$result = $dispatcher->dispatch($job, $params, $service); |
76
|
|
|
} |
77
|
|
|
|
78
|
56 |
|
$span->end(); |
79
|
56 |
|
return $result; |
80
|
56 |
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
56 |
|
public function get($alias, bool $new = false) : object |
84
|
|
|
{ |
85
|
56 |
|
if (!$this->hasInstance($alias)) { |
86
|
56 |
|
$instance = null; |
87
|
56 |
|
if (is_subclass_of($alias, Procedure::class)) { |
|
|
|
|
88
|
7 |
|
$instance = $this->get(Mapper::class) |
89
|
7 |
|
->getPlugin(ProcedurePlugin::class) |
90
|
7 |
|
->get($alias); |
91
|
|
|
} |
92
|
56 |
|
if (is_subclass_of($alias, Repository::class)) { |
|
|
|
|
93
|
1 |
|
$spaceName = $this->get(Mapper::class) |
94
|
1 |
|
->getPlugin(Annotation::class) |
95
|
1 |
|
->getRepositorySpaceName($alias); |
96
|
|
|
|
97
|
1 |
|
if ($spaceName) { |
98
|
1 |
|
$instance = $this->get(Mapper::class)->getRepository($spaceName); |
99
|
|
|
} |
100
|
|
|
} |
101
|
56 |
|
if ($instance) { |
102
|
|
|
$this->share($alias, function () use ($instance) { |
103
|
8 |
|
return $instance; |
104
|
8 |
|
}); |
105
|
|
|
} |
106
|
|
|
} |
107
|
56 |
|
return parent::get($alias, $new); |
108
|
|
|
} |
109
|
|
|
|
110
|
56 |
|
public function hasInstance($id) : bool |
111
|
|
|
{ |
112
|
56 |
|
if ($this->definitions->has($id)) { |
113
|
56 |
|
return true; |
114
|
|
|
} |
115
|
56 |
|
if ($this->definitions->hasTag($id)) { |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
56 |
|
if ($this->providers->provides($id)) { |
119
|
56 |
|
return true; |
120
|
|
|
} |
121
|
56 |
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
56 |
|
public function has($id) : bool |
125
|
|
|
{ |
126
|
56 |
|
if ($this->definitions->has($id)) { |
127
|
56 |
|
return true; |
128
|
|
|
} |
129
|
56 |
|
if ($this->definitions->hasTag($id)) { |
130
|
|
|
return true; |
131
|
|
|
} |
132
|
56 |
|
if ($this->providers->provides($id)) { |
133
|
56 |
|
return true; |
134
|
|
|
} |
135
|
8 |
|
if ($this->reflection && $this->reflection->has($id)) { |
136
|
7 |
|
return true; |
137
|
|
|
} |
138
|
1 |
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
56 |
|
public function call($callback) |
|
|
|
|
142
|
|
|
{ |
143
|
56 |
|
return $this->reflection->call(...func_get_args()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|