55namespace Tarantool \Mapper ;
66
77use Psr \Cache \CacheItemPoolInterface ;
8+ use ReflectionClass ;
89use Tarantool \Client \Client ;
910use Tarantool \Client \Exception \RequestFailed ;
1011use Tarantool \Client \Schema \Criteria ;
@@ -14,6 +15,7 @@ class Mapper
1415{
1516 use Api;
1617
18+ private array $ classNames = [];
1719 private array $ spaceId = [];
1820 private array $ spaces = [];
1921 private int $ schemaId = 0 ;
@@ -40,6 +42,7 @@ public function call(string $query, array $params = [])
4042
4143 public function createSpace (string $ space , array $ options = []): Space
4244 {
45+ $ space = $ this ->getClassSpace ($ space );
4346 $ this ->client ->evaluate ('box.schema.space.create(...) ' , $ space , $ options );
4447 return $ this ->getSpace ($ space );
4548 }
@@ -128,13 +131,33 @@ public function getChanges(): array
128131 return $ this ->middleware ->getChanges ();
129132 }
130133
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+
131146 public function getSpace (int |string $ id ): Space
132147 {
133148 if (!count ($ this ->spaces )) {
134149 $ this ->setSchemaId (0 );
135150 }
136151
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 ];
138161 }
139162
140163 public function getSpaces (): array
@@ -144,7 +167,7 @@ public function getSpaces(): array
144167
145168 public function hasSpace (string $ space ): bool
146169 {
147- return array_key_exists ($ space , $ this ->spaceId );
170+ return array_key_exists ($ this -> getClassSpace ( $ space) , $ this ->spaceId );
148171 }
149172
150173 public function migrate (array $ migrations = []): void
@@ -163,6 +186,26 @@ public function migrate(array $migrations = []): void
163186 array_map (fn (Migration $ migration ) => $ migration ->afterSchema ($ this ), $ instances );
164187 }
165188
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+
166209 public function setSchemaId (int $ schemaId )
167210 {
168211 if (!$ this ->schemaId || $ this ->schemaId !== $ schemaId ) {
@@ -186,6 +229,10 @@ public function setSchemaId(int $schemaId)
186229 $ this ->spaces [$ row ['id ' ]]->setFormat ($ row ['format ' ]);
187230 }
188231 $ 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+ }
189236 }
190237
191238 foreach (array_keys ($ this ->spaces ) as $ id ) {
0 commit comments