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\Embeddable; |
12
|
|
|
use Cycle\Schema\GeneratorInterface; |
13
|
|
|
use Cycle\Schema\Registry; |
14
|
|
|
use Spiral\Annotations\Parser; |
15
|
|
|
use Spiral\Tokenizer\ClassesInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Generates ORM schema based on annotated classes. |
19
|
|
|
*/ |
20
|
|
|
final class Embeddings implements GeneratorInterface |
21
|
|
|
{ |
22
|
|
|
/** @var ClassesInterface */ |
23
|
|
|
private $locator; |
24
|
|
|
|
25
|
|
|
/** @var Parser */ |
26
|
|
|
private $parser; |
27
|
|
|
|
28
|
|
|
/** @var Generator */ |
29
|
|
|
private $generator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ClassesInterface $locator |
33
|
|
|
* @param Parser|null $parser |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ClassesInterface $locator, Parser $parser = null) |
36
|
|
|
{ |
37
|
|
|
$this->locator = $locator; |
38
|
|
|
$this->parser = $parser ?? Generator::getDefaultParser(); |
39
|
|
|
$this->generator = new Generator($this->parser); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Registry $registry |
44
|
|
|
* @return Registry |
45
|
|
|
*/ |
46
|
|
|
public function run(Registry $registry): Registry |
47
|
|
|
{ |
48
|
|
|
foreach ($this->locator->getClasses() as $class) { |
49
|
|
|
if ($class->getDocComment() === false) { |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$ann = $this->parser->parse($class->getDocComment()); |
54
|
|
|
if (!isset($ann[Embeddable::NAME])) { |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @var Embeddable $em */ |
59
|
|
|
$em = $ann[Embeddable::NAME]; |
60
|
|
|
|
61
|
|
|
$e = $this->generator->initEmbedding($em, $class); |
62
|
|
|
|
63
|
|
|
// columns |
64
|
|
|
$this->generator->initFields($e, $class, $em->getColumnPrefix()); |
65
|
|
|
|
66
|
|
|
// register entity (OR find parent) |
67
|
|
|
$registry->register($e); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $registry; |
71
|
|
|
} |
72
|
|
|
} |