Passed
Pull Request — master (#4)
by
unknown
01:39
created

AnnotatedSchemaConveyor::getEntityClassLocator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 8
nc 2
nop 0
dl 0
loc 12
ccs 0
cts 11
cp 0
c 0
b 0
f 0
cc 2
crap 6
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\Generator;
4
5
use Cycle\Annotated;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use Psr\Container\ContainerInterface;
8
use Spiral\Tokenizer\ClassLocator;
9
use Symfony\Component\Finder\Finder;
10
use Yiisoft\Aliases\Aliases;
11
12
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 $isAnnotated = false;
21
22
    public function __construct(ContainerInterface $container) {
23
        parent::__construct($container);
24
    }
25
26
    public function setTableNaming(int $type): void
27
    {
28
        $this->tableNaming = $type;
29
    }
30
31
    public function getTableNaming(): int
32
    {
33
        return $this->tableNaming;
34
    }
35
36
    /**
37
     * @param string|string[] $paths
38
     */
39
    public function addEntityPaths($paths): void
40
    {
41
        $paths = (array)$paths;
42
        $this->entityPaths = array_merge($this->entityPaths, $paths);
43
    }
44
45
    public function getGenerators(): array
46
    {
47
        $this->annotateConveyor();
48
        return parent::getGenerators();
49
    }
50
51
    protected function annotateConveyor()
52
    {
53
        if ($this->isAnnotated) {
54
            return;
55
        }
56
        // autoload annotations
57
        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

57
        /** @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...
58
59
        $this->isAnnotated = true;
60
        $classLocator = $this->getEntityClassLocator();
61
62
        // register embeddable entities
63
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = new Annotated\Embeddings($classLocator);
64
        // register annotated entities
65
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = new Annotated\Entities($classLocator, null, $this->tableNaming);
66
        // add @Table(columns) declarations
67
        $this->conveyor[SchemaConveyor::STAGE_INDEX][] = Annotated\MergeColumns::class;
68
        // add @Table(indexes) declarations
69
        $this->conveyor[SchemaConveyor::STAGE_RENDER][] = Annotated\MergeIndexes::class;
70
    }
71
72
    protected function getEntityClassLocator(): ClassLocator
73
    {
74
        $aliases = $this->container->get(Aliases::class);
75
        $list = [];
76
        foreach ($this->entityPaths as $path) {
77
            $list[] = $aliases->get($path);
78
        }
79
        $finder = (new Finder())
80
            ->files()
81
            ->in($list);
82
83
        return new ClassLocator($finder);
84
    }
85
}
86