|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Basis; |
|
4
|
|
|
|
|
5
|
|
|
use League\Container\Container; |
|
6
|
|
|
use League\Container\ReflectionContainer; |
|
7
|
|
|
use Tarantool\Mapper\Mapper; |
|
8
|
|
|
use Tarantool\Mapper\Plugin\Annotation; |
|
9
|
|
|
use Tarantool\Mapper\Plugin\Procedure as ProcedurePlugin; |
|
10
|
|
|
use Tarantool\Mapper\Procedure; |
|
11
|
|
|
use Tarantool\Mapper\Repository; |
|
12
|
|
|
|
|
13
|
|
|
class Application extends Container |
|
14
|
|
|
{ |
|
15
|
|
|
public function __construct(string $root) |
|
16
|
|
|
{ |
|
17
|
|
|
parent::__construct(); |
|
18
|
|
|
|
|
19
|
|
|
$fs = new Filesystem($this, $root); |
|
20
|
|
|
|
|
21
|
|
|
$this->share(Application::class, $this); |
|
22
|
|
|
$this->share(Container::class, $this); |
|
23
|
|
|
$this->share(Filesystem::class, $fs); |
|
24
|
|
|
|
|
25
|
|
|
$this->addServiceProvider(Provider\ClickhouseProvider::class); |
|
26
|
|
|
$this->addServiceProvider(Provider\CoreProvider::class); |
|
27
|
|
|
$this->addServiceProvider(Provider\GuzzleProvider::class); |
|
28
|
|
|
$this->addServiceProvider(Provider\PoolProvider::class); |
|
29
|
|
|
$this->addServiceProvider(Provider\PredisProvider::class); |
|
30
|
|
|
$this->addServiceProvider(Provider\ServiceProvider::class); |
|
31
|
|
|
$this->addServiceProvider(Provider\TarantoolProvider::class); |
|
32
|
|
|
|
|
33
|
|
|
foreach ($fs->listClasses('Provider') as $provider) { |
|
34
|
|
|
$this->addServiceProvider($provider); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->delegate($this->reflection = new ReflectionContainer()); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
38 |
|
public function dispatch(string $job, array $params = [], string $service = null) |
|
41
|
|
|
{ |
|
42
|
38 |
|
if ($service !== null) { |
|
43
|
|
|
if ($this->get(Service::class)->getName() == $service) { |
|
44
|
|
|
$service = null; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
38 |
|
return $this->get(Cache::class) |
|
49
|
38 |
|
->wrap([$job, $params, $service], function() use ($job, $params, $service) { |
|
50
|
38 |
|
$runner = $this->get(Runner::class); |
|
51
|
38 |
|
if ($service === null) { |
|
52
|
38 |
|
if ($runner->hasJob($job)) { |
|
53
|
38 |
|
return $runner->dispatch($job, $params); |
|
54
|
|
|
} |
|
55
|
1 |
|
if (explode('.', $job)[0] == $this->get(Service::class)->getName()) { |
|
56
|
|
|
return $runner->dispatch($job, $params); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
$dispatcher = $this->get(Dispatcher::class); |
|
61
|
1 |
|
return $dispatcher->dispatch($job, $params, $service); |
|
62
|
38 |
|
}); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
38 |
|
public function get($alias, bool $new = false) : object |
|
66
|
|
|
{ |
|
67
|
38 |
|
if (!$this->has($alias)) { |
|
68
|
38 |
|
$instance = null; |
|
69
|
38 |
|
if (is_subclass_of($alias, Procedure::class)) { |
|
|
|
|
|
|
70
|
|
|
$instance = $this->get(Mapper::class) |
|
71
|
|
|
->getPlugin(ProcedurePlugin::class) |
|
72
|
|
|
->get($alias); |
|
73
|
|
|
} |
|
74
|
38 |
|
if (is_subclass_of($alias, Repository::class)) { |
|
|
|
|
|
|
75
|
1 |
|
$spaceName = $this->get(Mapper::class) |
|
76
|
1 |
|
->getPlugin(Annotation::class) |
|
77
|
1 |
|
->getRepositorySpaceName($alias); |
|
78
|
|
|
|
|
79
|
1 |
|
if ($spaceName) { |
|
80
|
1 |
|
$instance = $this->get(Mapper::class)->getRepository($spaceName); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
38 |
|
if ($instance) { |
|
84
|
1 |
|
$this->share($alias, function () use ($instance) { |
|
85
|
1 |
|
return $instance; |
|
86
|
1 |
|
}); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
38 |
|
return parent::get($alias, $new); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
38 |
|
public function has($id) : bool |
|
93
|
|
|
{ |
|
94
|
38 |
|
if ($this->definitions->has($id)) { |
|
95
|
38 |
|
return true; |
|
96
|
|
|
} |
|
97
|
38 |
|
if ($this->definitions->hasTag($id)) { |
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
38 |
|
if ($this->providers->provides($id)) { |
|
101
|
38 |
|
return true; |
|
102
|
|
|
} |
|
103
|
38 |
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
38 |
|
public function call($callback) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
38 |
|
return $this->reflection->call(...func_get_args()); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: