Skip to content

Commit 409e165

Browse files
committed
custom class init method
1 parent ced8008 commit 409e165

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ class Policy
9494
) {
9595
}
9696

97-
public static function initSchema(\Tarantool\Mapper\Space $space)
97+
public static function initialize(\Tarantool\Mapper\Space $space)
9898
{
9999
$space->addIndex(['nick'], ['unique' => true]);
100100
}
101101
}
102102

103103
$policy = $mapper->createSpace('policy');
104-
$policy->setClass(Policy::class);
104+
$policy->setClass(Policy::class, 'initialize'); // use custom initialize method
105105
$policy->migrate();
106106
```
107107

src/Space.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Space
2323
private array $indexes = [];
2424

2525
private ?string $class = null;
26+
private ?string $init = null;
2627
private ?ReflectionMethod $constructor = null;
2728

2829
public function __construct(public readonly Mapper $mapper, array $meta = [])
@@ -322,17 +323,18 @@ public function migrate()
322323

323324
$this->addProperty($property->getName(), $tarantoolType, $opts);
324325
}
325-
$init = $reflection->getMethod('initSchema');
326+
$init = $reflection->getMethod($this->init);
326327

327328
if ($init && $init->isStatic()) {
328329
$init->invoke(null, $this);
329330
}
330331
}
331332

332-
public function setClass(string $class)
333+
public function setClass(string $class, string $init = 'initSchema')
333334
{
334335
$reflection = new ReflectionClass($class);
335336
$this->class = $class;
337+
$this->init = $init;
336338
$this->constructor = $reflection->getConstructor();
337339
}
338340

0 commit comments

Comments
 (0)