Passed
Push — master ( 9c8ace...d62147 )
by Alexander
13:22
created

AnnotatedSchemaConveyor::getTableNaming()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\Conveyor;
4
5
use Cycle\Annotated\Embeddings;
6
use Cycle\Annotated\Entities;
7
use Cycle\Annotated\MergeColumns;
8
use Cycle\Annotated\MergeIndexes;
9
use Doctrine\Common\Annotations\AnnotationRegistry;
10
use Spiral\Tokenizer\ClassLocator;
11
use Symfony\Component\Finder\Finder;
12
use Yiisoft\Aliases\Aliases;
13
use Yiisoft\Yii\Cycle\Exception\EmptyEntityPathsException;
14
15
final class AnnotatedSchemaConveyor extends SchemaConveyor
16
{
17
    /** @var string[] */
18
    private array $entityPaths = [];
19
20
    private int $tableNaming = Entities::TABLE_NAMING_SINGULAR;
21
22
    private bool $isAddedAnnotated = false;
23
24
    public function setTableNaming(int $type): void
25
    {
26
        $this->tableNaming = $type;
27
    }
28
29
    public function getTableNaming(): int
30
    {
31
        return $this->tableNaming;
32
    }
33
34
    /**
35
     * @param string[] $paths
36
     */
37
    public function addEntityPaths(array $paths): void
38
    {
39
        $this->entityPaths = array_merge($this->entityPaths, $paths);
40
    }
41
42
    public function getGenerators(): array
43
    {
44
        $this->addAnnotatedGenerators();
45
        return parent::getGenerators();
46
    }
47
48
    /**
49
     * Add some generators in this conveyor into the INDEX stage
50
     * Added generators will search for entity classes and read their annotations
51
     */
52
    private function addAnnotatedGenerators(): void
53
    {
54
        if ($this->isAddedAnnotated) {
55
            return;
56
        }
57
        // autoload annotations
58
        AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists') ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

58
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader('class_exists');

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.

Loading history...
59
60
        $this->isAddedAnnotated = true;
61
        $classLocator = $this->getEntityClassLocator();
62
63
        // register embeddable entities
64
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = new Embeddings($classLocator);
65
        // register annotated entities
66
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = new Entities($classLocator, null, $this->tableNaming);
67
        // add @Table(columns) declarations
68
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = MergeColumns::class;
69
        // add @Table(indexes) declarations
70
        $this->conveyor[SchemaConveyor::STAGE_RENDER][] = MergeIndexes::class;
71
    }
72
73
    private function getEntityClassLocator(): ClassLocator
74
    {
75
        $aliases = $this->container->get(Aliases::class);
76
        $list = [];
77
        foreach ($this->entityPaths as $path) {
78
            $list[] = $aliases->get($path);
79
        }
80
81
        if (!count($list)) {
82
            throw new EmptyEntityPathsException();
83
        }
84
85
        $finder = (new Finder())
86
            ->files()
87
            ->in($list);
88
89
        return new ClassLocator($finder);
90
    }
91
}
92