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

AnnotatedSchemaConveyor::annotateConveyor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 9
nc 2
nop 0
dl 0
loc 19
ccs 0
cts 12
cp 0
c 0
b 0
f 0
cc 2
crap 6
rs 9.9666
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 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|string[] $paths
34
     */
35
    public function addEntityPaths($paths): void
36
    {
37
        $paths = (array)$paths;
38
        $this->entityPaths = array_merge($this->entityPaths, $paths);
39
    }
40
41
    public function getGenerators(): array
42
    {
43
        $this->annotateConveyor();
44
        return parent::getGenerators();
45
    }
46
47
    protected function annotateConveyor()
48
    {
49
        if ($this->isAnnotated) {
50
            return;
51
        }
52
        // autoload annotations
53
        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

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