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\Definition\Entity as EntitySchema; |
13
|
|
|
use Cycle\Schema\GeneratorInterface; |
14
|
|
|
use Cycle\Schema\Registry; |
15
|
|
|
use Spiral\Annotations\Parser; |
16
|
|
|
|
17
|
|
|
final class MergeColumns implements GeneratorInterface |
18
|
|
|
{ |
19
|
|
|
/** @var Parser */ |
20
|
|
|
private $parser; |
21
|
|
|
|
22
|
|
|
/** @var Generator */ |
23
|
|
|
private $generator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Parser|null $parser |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Parser $parser = null) |
29
|
|
|
{ |
30
|
|
|
$this->parser = $parser ?? Generator::getDefaultParser();; |
31
|
|
|
$this->generator = new Generator($this->parser); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Registry $registry |
36
|
|
|
* @return Registry |
37
|
|
|
*/ |
38
|
|
|
public function run(Registry $registry): Registry |
39
|
|
|
{ |
40
|
|
|
foreach ($registry as $e) { |
41
|
|
|
if ($e->getClass() === null) { |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->copy($e, $e->getClass()); |
46
|
|
|
|
47
|
|
|
// copy from related classes |
48
|
|
|
$this->copy($e, $e->getMapper()); |
49
|
|
|
$this->copy($e, $e->getRepository()); |
50
|
|
|
$this->copy($e, $e->getSource()); |
51
|
|
|
$this->copy($e, $e->getConstrain()); |
52
|
|
|
|
53
|
|
|
foreach ($registry->getChildren($e) as $child) { |
54
|
|
|
$this->copy($e, $child->getClass()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $registry; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param EntitySchema $e |
63
|
|
|
* @param string|null $class |
64
|
|
|
*/ |
65
|
|
|
protected function copy(EntitySchema $e, ?string $class) |
66
|
|
|
{ |
67
|
|
|
if ($class === null) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$class = new \ReflectionClass($class); |
73
|
|
|
} catch (\ReflectionException $e) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($class->getDocComment() === false) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$ann = $this->parser->parse($class->getDocComment()); |
|
|
|
|
82
|
|
|
if (!isset($ann[Table::NAME])) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** @var Table $ta */ |
87
|
|
|
$ta = $ann[Table::NAME]; |
88
|
|
|
|
89
|
|
|
// additional columns (mapped to local fields automatically) |
90
|
|
|
$this->generator->initColumns($e, $ta->getColumns(), $class); |
91
|
|
|
} |
92
|
|
|
} |