|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Basis; |
|
4
|
|
|
|
|
5
|
|
|
use Tarantool\Mapper\Entity; |
|
6
|
|
|
use Tarantool\Mapper\Mapper; |
|
7
|
|
|
use Tarantool\Mapper\Pool; |
|
8
|
|
|
use Tarantool\Queue\Queue; |
|
9
|
|
|
|
|
10
|
|
|
trait Toolkit |
|
11
|
|
|
{ |
|
12
|
|
|
protected $app; |
|
13
|
|
|
|
|
14
|
56 |
|
public function __construct(Application $app) |
|
15
|
|
|
{ |
|
16
|
56 |
|
$this->app = $app; |
|
17
|
56 |
|
} |
|
18
|
|
|
|
|
19
|
8 |
|
protected function create(string $space, array $data) |
|
20
|
|
|
{ |
|
21
|
8 |
|
return $this->getRepository($space)->create($data)->save(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
2 |
|
public function send(string $job, array $params = [], string $service = null) |
|
25
|
|
|
{ |
|
26
|
2 |
|
return $this->get(Executor::class)->send($job, $params, $service); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
56 |
|
public function dispatch(string $job, array $params = [], string $service = null) |
|
30
|
|
|
{ |
|
31
|
56 |
|
return $this->app->dispatch($job, $params, $service); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
5 |
|
protected function find(string $space, $params = []) : array |
|
35
|
|
|
{ |
|
36
|
5 |
|
return $this->getRepository($space)->find($params); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
protected function findOne(string $space, $params = []) |
|
40
|
|
|
{ |
|
41
|
4 |
|
return $this->getRepository($space)->findOne($params); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
5 |
|
protected function findOrCreate(string $space, $params = [], $data = []) |
|
45
|
|
|
{ |
|
46
|
5 |
|
return $this->getRepository($space)->findOrCreate($params, $data); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
6 |
|
protected function findOrFail(string $space, $params = []) |
|
50
|
|
|
{ |
|
51
|
6 |
|
return $this->getRepository($space)->findOrFail($params); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function fire(string $event, array $context) |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->get(Event::class)->fire($event, $context); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
56 |
|
protected function get(string $class) |
|
60
|
|
|
{ |
|
61
|
56 |
|
return $this->app->get($class); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
protected function getDate() |
|
65
|
|
|
{ |
|
66
|
1 |
|
return call_user_func_array([$this->get(Converter::class), 'getDate'], func_get_args()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
8 |
|
protected function getMapper() |
|
70
|
|
|
{ |
|
71
|
8 |
|
return $this->get(Mapper::class); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
9 |
|
protected function getRepository($space) |
|
75
|
|
|
{ |
|
76
|
9 |
|
if (strpos($space, '.') !== false) { |
|
77
|
3 |
|
return $this->get(Pool::class)->getRepository($space); |
|
78
|
|
|
} |
|
79
|
6 |
|
return $this->get(Mapper::class)->getRepository($space); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getQueue($tube) |
|
83
|
|
|
{ |
|
84
|
|
|
$alias = "queue.$tube"; |
|
85
|
|
|
if (!$this->app->hasInstance($alias, true)) { |
|
86
|
|
|
if (strpos($tube, '.') !== false) { |
|
87
|
|
|
[$service, $tube] = explode('.', $tube); |
|
|
|
|
|
|
88
|
|
|
$client = $this->get(Pool::class)->get($service)->getClient(); |
|
89
|
|
|
} else { |
|
90
|
|
|
$client = $this->getMapper()->getClient(); |
|
91
|
|
|
} |
|
92
|
|
|
$client->evaluate(" |
|
93
|
|
|
if queue == nil then |
|
94
|
|
|
queue = require('queue') |
|
95
|
|
|
end |
|
96
|
|
|
"); |
|
97
|
|
|
$this->app->share($alias, new Queue($client, $tube)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->app->get($alias); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
protected function remove(string $space, array $params = []) |
|
104
|
|
|
{ |
|
105
|
2 |
|
return $this->getRepository($space)->remove($params); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
protected function select($fields, string $table, array $params) |
|
109
|
|
|
{ |
|
110
|
2 |
|
return $this->get(Clickhouse::class)->select($fields, $table, $params); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
protected function insert(string $table, array $data, array $headers) |
|
114
|
|
|
{ |
|
115
|
2 |
|
return $this->get(Clickhouse::class)->insert($table, $data, $headers); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
protected function upload(string $filename, $contents) : string |
|
119
|
|
|
{ |
|
120
|
1 |
|
return $this->get(Storage::class)->upload($filename, $contents); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
protected function download(string $hash) |
|
124
|
|
|
{ |
|
125
|
1 |
|
return $this->get(Storage::class)->download($hash); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function __debugInfo() |
|
129
|
|
|
{ |
|
130
|
|
|
$info = get_object_vars($this); |
|
131
|
|
|
unset($info['app']); |
|
132
|
|
|
return $info; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
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.