|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\Annotated; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Annotated\Annotation\Column; |
|
8
|
|
|
use Cycle\Annotated\Annotation\Table; |
|
9
|
|
|
use Cycle\Annotated\Exception\AnnotationException; |
|
10
|
|
|
use Cycle\Schema\Definition\Entity as EntitySchema; |
|
11
|
|
|
use Cycle\Schema\GeneratorInterface; |
|
12
|
|
|
use Cycle\Schema\Registry; |
|
13
|
|
|
use Doctrine\Common\Annotations\Reader as DoctrineReader; |
|
14
|
|
|
use Spiral\Attributes\ReaderInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Copy column definitions from Mapper/Repository to Entity. |
|
18
|
|
|
*/ |
|
19
|
|
|
final class MergeColumns implements GeneratorInterface |
|
20
|
|
|
{ |
|
21
|
|
|
private ReaderInterface $reader; |
|
22
|
|
|
|
|
23
|
|
|
private Configurator $generator; |
|
24
|
|
|
|
|
25
|
440 |
|
public function __construct(DoctrineReader|ReaderInterface $reader = null) |
|
26
|
|
|
{ |
|
27
|
440 |
|
$this->reader = ReaderFactory::create($reader); |
|
28
|
440 |
|
$this->generator = new Configurator($this->reader); |
|
29
|
440 |
|
} |
|
30
|
|
|
|
|
31
|
416 |
|
public function run(Registry $registry): Registry |
|
32
|
|
|
{ |
|
33
|
416 |
|
foreach ($registry as $e) { |
|
34
|
416 |
|
if ($e->getClass() === null || !$registry->hasTable($e)) { |
|
35
|
24 |
|
continue; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
416 |
|
$this->copy($e, $e->getClass()); |
|
39
|
|
|
|
|
40
|
|
|
// copy from related classes |
|
41
|
413 |
|
$this->copy($e, $e->getMapper()); |
|
42
|
413 |
|
$this->copy($e, $e->getRepository()); |
|
43
|
413 |
|
$this->copy($e, $e->getSource()); |
|
44
|
413 |
|
$this->copy($e, $e->getScope()); |
|
45
|
|
|
|
|
46
|
413 |
|
foreach ($registry->getChildren($e) as $child) { |
|
47
|
268 |
|
$this->copy($e, $child->getClass()); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
413 |
|
return $registry; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param class-string|null $class |
|
|
|
|
|
|
56
|
|
|
*/ |
|
57
|
416 |
|
private function copy(EntitySchema $e, ?string $class): void |
|
58
|
|
|
{ |
|
59
|
416 |
|
if ($class === null) { |
|
60
|
413 |
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
416 |
|
$class = new \ReflectionClass($class); |
|
65
|
|
|
} catch (\ReflectionException) { |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
try { |
|
70
|
|
|
/** @var Table|null $tableMeta */ |
|
71
|
416 |
|
$tableMeta = $this->reader->firstClassMetadata($class, Table::class); |
|
72
|
|
|
/** @var Column[] $columnsMeta */ |
|
73
|
416 |
|
$columnsMeta = $this->reader->getClassMetadata($class, Column::class); |
|
74
|
|
|
} catch (\Exception $e) { |
|
75
|
|
|
throw new AnnotationException($e->getMessage(), $e->getCode(), $e); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
416 |
|
$columns = $tableMeta === null ? [] : $tableMeta->getColumns(); |
|
79
|
416 |
|
foreach ($columnsMeta as $column) { |
|
80
|
217 |
|
$columns[] = $column; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
416 |
|
if ($columns === []) { |
|
84
|
388 |
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// additional columns (mapped to local fields automatically) |
|
88
|
236 |
|
$this->generator->initColumns($e, $columns, $class); |
|
89
|
233 |
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|