|
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
|
52 |
|
public function dispatch(string $job, array $params = [], string $service = null) |
|
50
|
|
|
{ |
|
51
|
52 |
|
if ($service !== null) { |
|
52
|
|
|
if ($this->get(Service::class)->getName() == $service) { |
|
53
|
|
|
$service = null; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
52 |
|
return $this->get(Cache::class) |
|
58
|
|
|
->wrap([$job, $params, $service], function() use ($job, $params, $service) { |
|
59
|
52 |
|
$span = $this->get(Tracer::class)->createSpan($job); |
|
60
|
52 |
|
foreach ($params as $k => $v) { |
|
61
|
10 |
|
if (is_numeric($v) || is_string($v)) { |
|
62
|
7 |
|
$span->setAttribute($k, $v); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
52 |
|
$serviceName = $this->get(Service::class)->getName(); |
|
67
|
52 |
|
$dispatcher = $this->get(Dispatcher::class); |
|
68
|
52 |
|
$runner = $this->get(Runner::class); |
|
69
|
|
|
|
|
70
|
52 |
|
if ($service === null && $runner->hasJob($job)) { |
|
71
|
52 |
|
$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
|
52 |
|
$span->end(); |
|
79
|
52 |
|
return $result; |
|
80
|
52 |
|
}); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
52 |
|
public function get($alias, bool $new = false) : object |
|
84
|
|
|
{ |
|
85
|
52 |
|
if (!$this->hasInstance($alias)) { |
|
86
|
52 |
|
$instance = null; |
|
87
|
52 |
|
if (is_subclass_of($alias, Procedure::class)) { |
|
|
|
|
|
|
88
|
4 |
|
$instance = $this->get(Mapper::class) |
|
89
|
4 |
|
->getPlugin(ProcedurePlugin::class) |
|
90
|
4 |
|
->get($alias); |
|
91
|
|
|
} |
|
92
|
52 |
|
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
|
52 |
|
if ($instance) { |
|
102
|
|
|
$this->share($alias, function () use ($instance) { |
|
103
|
5 |
|
return $instance; |
|
104
|
5 |
|
}); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
52 |
|
return parent::get($alias, $new); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
52 |
|
public function hasInstance($id) : bool |
|
111
|
|
|
{ |
|
112
|
52 |
|
if ($this->definitions->has($id)) { |
|
113
|
52 |
|
return true; |
|
114
|
|
|
} |
|
115
|
52 |
|
if ($this->definitions->hasTag($id)) { |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
52 |
|
if ($this->providers->provides($id)) { |
|
119
|
52 |
|
return true; |
|
120
|
|
|
} |
|
121
|
52 |
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
52 |
|
public function has($id) : bool |
|
125
|
|
|
{ |
|
126
|
52 |
|
if ($this->definitions->has($id)) { |
|
127
|
52 |
|
return true; |
|
128
|
|
|
} |
|
129
|
52 |
|
if ($this->definitions->hasTag($id)) { |
|
130
|
|
|
return true; |
|
131
|
|
|
} |
|
132
|
52 |
|
if ($this->providers->provides($id)) { |
|
133
|
52 |
|
return true; |
|
134
|
|
|
} |
|
135
|
8 |
|
if ($this->reflection && $this->reflection->has($id)) { |
|
136
|
7 |
|
return true; |
|
137
|
|
|
} |
|
138
|
1 |
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
52 |
|
public function call($callback) |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
52 |
|
return $this->reflection->call(...func_get_args()); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|