Passed
Push — master ( 4ae095...46ef9e )
by Alexander
02:10 queued 13s
created

AnnotatedSchemaConveyor::getEntityClassLocator()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 17
ccs 0
cts 14
cp 0
crap 12
rs 9.9332
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');
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

56
        /** @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...
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