1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tarantool\Mapper; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Tarantool\Client\Client; |
9
|
|
|
|
10
|
|
|
class Mapper |
11
|
|
|
{ |
12
|
|
|
private $client; |
13
|
|
|
private $plugins = []; |
14
|
|
|
private $schema; |
15
|
|
|
private $bootstrap; |
16
|
|
|
|
17
|
|
|
public function __construct(Client $client) |
18
|
|
|
{ |
19
|
|
|
$this->client = $client; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getPlugin($mixed) |
23
|
|
|
{ |
24
|
|
|
if (!is_subclass_of($mixed, Plugin::class)) { |
|
|
|
|
25
|
|
|
throw new Exception("Plugin should extend " . Plugin::class . " class"); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$plugin = is_object($mixed) ? $mixed : new $mixed($this); |
29
|
|
|
$class = get_class($plugin); |
30
|
|
|
|
31
|
|
|
if ($plugin == $mixed && array_key_exists($class, $this->plugins)) { |
32
|
|
|
// overwrite plugin instance |
33
|
|
|
throw new Exception($class.' is registered'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (!array_key_exists($class, $this->plugins)) { |
37
|
|
|
$this->plugins[$class] = $plugin; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->plugins[$class]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function create(string $space, $data) : Entity |
44
|
|
|
{ |
45
|
|
|
return $this->getRepository($space)->create($data)->save(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function findOne(string $space, $params = []) : ?Entity |
49
|
|
|
{ |
50
|
|
|
return $this->getRepository($space)->findOne($params); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function findOrCreate(string $space, $params = []) : Entity |
54
|
|
|
{ |
55
|
|
|
return $this->getRepository($space)->findOrCreate($params)->save(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function findOrFail(string $space, $params = []) : Entity |
59
|
|
|
{ |
60
|
|
|
return $this->getRepository($space)->findOrFail($params); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function find(string $space, $params = []) : array |
64
|
|
|
{ |
65
|
|
|
return $this->getRepository($space)->find($params); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function findRepository(Entity $instance) : Repository |
69
|
|
|
{ |
70
|
|
|
foreach ($this->getSchema()->getSpaces() as $space) { |
71
|
|
|
if ($space->getRepository()->knows($instance)) { |
72
|
|
|
return $space->getRepository(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
throw new Exception("No Repository for given Entity"); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getBootstrap() : Bootstrap |
80
|
|
|
{ |
81
|
|
|
return $this->bootstrap ?: $this->bootstrap = new Bootstrap($this); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getClient() : Client |
85
|
|
|
{ |
86
|
|
|
return $this->client; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getMeta() : array |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
'schema' => $this->getSchema()->getMeta(), |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function hasPlugin(string $class) : bool |
97
|
|
|
{ |
98
|
|
|
return array_key_exists($class, $this->plugins); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getPlugins() : array |
102
|
|
|
{ |
103
|
|
|
return array_values($this->plugins); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getRepository(string $space) : Repository |
107
|
|
|
{ |
108
|
|
|
return $this->getSchema()->getSpace($space)->getRepository(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getRepositories() : array |
112
|
|
|
{ |
113
|
|
|
$repositories = []; |
114
|
|
|
foreach ($this->getSchema()->getSpaces() as $space) { |
115
|
|
|
if ($space->repositoryExists()) { |
116
|
|
|
$repositories[] = $space->getRepository(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
return $repositories; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getSchema() : Schema |
123
|
|
|
{ |
124
|
|
|
return $this->schema ?: $this->schema = new Schema($this); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function remove($space, $params = []) : self |
128
|
|
|
{ |
129
|
|
|
if ($space instanceof Entity) { |
130
|
|
|
$this->findRepository($space)->removeEntity($space); |
131
|
|
|
} else { |
132
|
|
|
$this->getRepository($space)->remove($params); |
133
|
|
|
} |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function save(Entity $instance) : Entity |
138
|
|
|
{ |
139
|
|
|
return $this->findRepository($instance)->save($instance); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function setClient(Client $client) : self |
143
|
|
|
{ |
144
|
|
|
$this->client = $client; |
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setMeta(array $meta) : self |
149
|
|
|
{ |
150
|
|
|
if ($this->schema) { |
151
|
|
|
$this->schema->setMeta($meta['schema']); |
152
|
|
|
} else { |
153
|
|
|
$this->schema = new Schema($this, $meta['schema']); |
154
|
|
|
} |
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|