1 | <?php declare(strict_types=1); |
||
2 | /** |
||
3 | * Spiral Framework. |
||
4 | * |
||
5 | * @license MIT |
||
6 | * @author Anton Titov (Wolfy-J) |
||
7 | */ |
||
8 | |||
9 | namespace Cycle\Annotated; |
||
10 | |||
11 | use Cycle\Annotated\Annotation\Table; |
||
12 | use Cycle\Schema\GeneratorInterface; |
||
13 | use Cycle\Schema\Registry; |
||
14 | use Spiral\Annotations\Parser; |
||
15 | use Spiral\Database\Schema\AbstractTable; |
||
16 | |||
17 | final class Indexes implements GeneratorInterface |
||
18 | { |
||
19 | /** @var Parser */ |
||
20 | private $parser; |
||
21 | |||
22 | /** |
||
23 | * @param Parser $parser |
||
24 | */ |
||
25 | public function __construct(Parser $parser) |
||
26 | { |
||
27 | $this->parser = $parser; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param Registry $registry |
||
32 | * @return Registry |
||
33 | */ |
||
34 | public function run(Registry $registry): Registry |
||
35 | { |
||
36 | foreach ($registry as $e) { |
||
37 | if ($e->getClass() === null) { |
||
38 | continue; |
||
39 | } |
||
40 | |||
41 | $this->render($registry->getTableSchema($e), $e->getClass()); |
||
42 | |||
43 | // copy table declarations from related classes |
||
44 | $this->render($registry->getTableSchema($e), $e->getMapper()); |
||
45 | $this->render($registry->getTableSchema($e), $e->getRepository()); |
||
46 | $this->render($registry->getTableSchema($e), $e->getSource()); |
||
47 | $this->render($registry->getTableSchema($e), $e->getConstrain()); |
||
48 | |||
49 | foreach ($registry->getChildren($e) as $child) { |
||
50 | $this->render($registry->getTableSchema($e), $child->getClass()); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | return $registry; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param AbstractTable $table |
||
59 | * @param string|null $class |
||
60 | */ |
||
61 | protected function render(AbstractTable $table, ?string $class) |
||
62 | { |
||
63 | if ($class === null) { |
||
64 | return; |
||
65 | } |
||
66 | |||
67 | try { |
||
68 | $class = new \ReflectionClass($class); |
||
69 | } catch (\ReflectionException $e) { |
||
70 | return; |
||
71 | } |
||
72 | |||
73 | if ($class->getDocComment() === false) { |
||
74 | return; |
||
75 | } |
||
76 | |||
77 | $ann = $this->parser->parse($class->getDocComment()); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
78 | if (!isset($ann[Table::NAME])) { |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | /** @var Table $ta */ |
||
83 | $ta = $ann[Table::NAME]; |
||
84 | |||
85 | $this->renderIndexes($table, $ta->getIndexes()); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param AbstractTable $table |
||
90 | * @param Table\Index[] $indexes |
||
91 | */ |
||
92 | public function renderIndexes(AbstractTable $table, array $indexes) |
||
93 | { |
||
94 | foreach ($indexes as $index) { |
||
95 | if ($index->getColumns() === []) { |
||
96 | continue; |
||
97 | } |
||
98 | |||
99 | $indexSchema = $table->index($index->getColumns()); |
||
100 | $indexSchema->unique($index->isUnique()); |
||
101 | |||
102 | if ($index->getIndex() !== null) { |
||
103 | $indexSchema->setName($index->getIndex()); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |