1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Cycle\Generator; |
4
|
|
|
|
5
|
|
|
use Cycle\Annotated; |
6
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
7
|
|
|
use Spiral\Tokenizer\ClassLocator; |
8
|
|
|
use Symfony\Component\Finder\Finder; |
9
|
|
|
use Yiisoft\Aliases\Aliases; |
10
|
|
|
use Yiisoft\Yii\Cycle\Exception\EmptyEntityPathsException; |
11
|
|
|
|
12
|
|
|
final class AnnotatedSchemaConveyor extends SchemaConveyor |
13
|
|
|
{ |
14
|
|
|
/** @var string[] */ |
15
|
|
|
protected $entityPaths = []; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
protected $tableNaming = Annotated\Entities::TABLE_NAMING_SINGULAR; |
19
|
|
|
|
20
|
|
|
protected $isAddedAnnotated = false; |
21
|
|
|
|
22
|
|
|
public function setTableNaming(int $type): void |
23
|
|
|
{ |
24
|
|
|
$this->tableNaming = $type; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getTableNaming(): int |
28
|
|
|
{ |
29
|
|
|
return $this->tableNaming; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string[] $paths |
34
|
|
|
*/ |
35
|
|
|
public function addEntityPaths(array $paths): void |
36
|
|
|
{ |
37
|
|
|
$this->entityPaths = array_merge($this->entityPaths, $paths); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getGenerators(): array |
41
|
|
|
{ |
42
|
|
|
$this->addAnnotatedGenerators(); |
43
|
|
|
return parent::getGenerators(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Add some generators in this conveyor into the INDEX stage |
48
|
|
|
* Added generators will search for entity classes and read their annotations |
49
|
|
|
*/ |
50
|
|
|
protected function addAnnotatedGenerators() |
51
|
|
|
{ |
52
|
|
|
if ($this->isAddedAnnotated) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
// autoload annotations |
56
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->isAddedAnnotated = true; |
59
|
|
|
$classLocator = $this->getEntityClassLocator(); |
60
|
|
|
|
61
|
|
|
// register embeddable entities |
62
|
|
|
$this->conveyor[SchemaConveyor::STAGE_INDEX][] = new Annotated\Embeddings($classLocator); |
63
|
|
|
// register annotated entities |
64
|
|
|
$this->conveyor[SchemaConveyor::STAGE_INDEX][] = new Annotated\Entities($classLocator, null, $this->tableNaming); |
65
|
|
|
// add @Table(columns) declarations |
66
|
|
|
$this->conveyor[SchemaConveyor::STAGE_INDEX][] = Annotated\MergeColumns::class; |
67
|
|
|
// add @Table(indexes) declarations |
68
|
|
|
$this->conveyor[SchemaConveyor::STAGE_RENDER][] = Annotated\MergeIndexes::class; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getEntityClassLocator(): ClassLocator |
72
|
|
|
{ |
73
|
|
|
$aliases = $this->container->get(Aliases::class); |
74
|
|
|
$list = []; |
75
|
|
|
foreach ($this->entityPaths as $path) { |
76
|
|
|
$list[] = $aliases->get($path); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!count($list)) { |
80
|
|
|
throw new EmptyEntityPathsException(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$finder = (new Finder()) |
84
|
|
|
->files() |
85
|
|
|
->in($list); |
86
|
|
|
|
87
|
|
|
return new ClassLocator($finder); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.