5
5
namespace Tarantool \Mapper ;
6
6
7
7
use Psr \Cache \CacheItemPoolInterface ;
8
+ use ReflectionClass ;
8
9
use Tarantool \Client \Client ;
9
10
use Tarantool \Client \Exception \RequestFailed ;
10
11
use Tarantool \Client \Schema \Criteria ;
@@ -14,6 +15,7 @@ class Mapper
14
15
{
15
16
use Api;
16
17
18
+ private array $ classNames = [];
17
19
private array $ spaceId = [];
18
20
private array $ spaces = [];
19
21
private int $ schemaId = 0 ;
@@ -40,6 +42,7 @@ public function call(string $query, array $params = [])
40
42
41
43
public function createSpace (string $ space , array $ options = []): Space
42
44
{
45
+ $ space = $ this ->getClassSpace ($ space );
43
46
$ this ->client ->evaluate ('box.schema.space.create(...) ' , $ space , $ options );
44
47
return $ this ->getSpace ($ space );
45
48
}
@@ -128,13 +131,33 @@ public function getChanges(): array
128
131
return $ this ->middleware ->getChanges ();
129
132
}
130
133
134
+ public function getClassSpace (int |string $ class ): int |string
135
+ {
136
+ if (!is_integer ($ class ) && class_exists ($ class )) {
137
+ if (!array_key_exists ($ class , $ this ->classNames )) {
138
+ $ this ->registerClass ($ class );
139
+ }
140
+ return $ this ->classNames [$ class ];
141
+ }
142
+
143
+ return $ class ;
144
+ }
145
+
131
146
public function getSpace (int |string $ id ): Space
132
147
{
133
148
if (!count ($ this ->spaces )) {
134
149
$ this ->setSchemaId (0 );
135
150
}
136
151
137
- return is_string ($ id ) ? $ this ->getSpace ($ this ->spaceId [$ id ]) : $ this ->spaces [$ id ];
152
+ $ space = $ this ->getClassSpace ($ id );
153
+ if ($ space !== $ id ) {
154
+ if (!$ this ->hasSpace ($ space )) {
155
+ $ spaceInstance = $ this ->createSpace ($ space );
156
+ $ spaceInstance ->setClass ($ id );
157
+ $ spaceInstance ->migrate ();
158
+ }
159
+ }
160
+ return is_string ($ space ) ? $ this ->getSpace ($ this ->spaceId [$ space ]) : $ this ->spaces [$ space ];
138
161
}
139
162
140
163
public function getSpaces (): array
@@ -144,7 +167,7 @@ public function getSpaces(): array
144
167
145
168
public function hasSpace (string $ space ): bool
146
169
{
147
- return array_key_exists ($ space , $ this ->spaceId );
170
+ return array_key_exists ($ this -> getClassSpace ( $ space) , $ this ->spaceId );
148
171
}
149
172
150
173
public function migrate (array $ migrations = []): void
@@ -163,6 +186,26 @@ public function migrate(array $migrations = []): void
163
186
array_map (fn (Migration $ migration ) => $ migration ->afterSchema ($ this ), $ instances );
164
187
}
165
188
189
+ public function registerClass (string $ class )
190
+ {
191
+ if (!array_key_exists ($ class , $ this ->classNames )) {
192
+ if (method_exists ($ class , 'getSpaceName ' )) {
193
+ $ space = call_user_func ([$ class , 'getSpaceName ' ]);
194
+ } else {
195
+ $ space = preg_replace (
196
+ ['/(?<=[^A-Z])([A-Z])/ ' , '/(?<=[^0-9])([0-9])/ ' ],
197
+ '_$0 ' ,
198
+ (new ReflectionClass ($ class ))->getShortName (),
199
+ );
200
+ $ space = strtolower ($ space );
201
+ }
202
+ if (array_key_exists ($ space , $ this ->spaceId )) {
203
+ $ this ->spaces [$ this ->spaceId [$ space ]]->setClass ($ class );
204
+ }
205
+ $ this ->classNames [$ class ] = strtolower ($ space );
206
+ }
207
+ }
208
+
166
209
public function setSchemaId (int $ schemaId )
167
210
{
168
211
if (!$ this ->schemaId || $ this ->schemaId !== $ schemaId ) {
@@ -186,6 +229,10 @@ public function setSchemaId(int $schemaId)
186
229
$ this ->spaces [$ row ['id ' ]]->setFormat ($ row ['format ' ]);
187
230
}
188
231
$ this ->spaceId [$ row ['name ' ]] = $ row ['id ' ];
232
+
233
+ if (array_search ($ row ['name ' ], $ this ->classNames )) {
234
+ $ this ->spaces [$ row ['id ' ]]->setClass (array_search ($ row ['name ' ], $ this ->classNames ));
235
+ }
189
236
}
190
237
191
238
foreach (array_keys ($ this ->spaces ) as $ id ) {
0 commit comments