Passed
Push — master ( b222de...2f2094 )
by Anton
02:04 queued 13s
created

SchemaBootloader::embeddings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Spiral\Bootloader\Cycle;
11
12
use Cycle\Annotated;
13
use Cycle\ORM\RepositoryInterface;
14
use Cycle\ORM\Schema;
15
use Cycle\ORM\SchemaInterface;
16
use Cycle\ORM\Select\Repository;
17
use Cycle\Schema\Generator;
18
use Cycle\Schema\GeneratorInterface;
19
use Spiral\Boot\Bootloader\Bootloader;
20
use Spiral\Boot\Bootloader\DependedInterface;
21
use Spiral\Boot\MemoryInterface;
22
use Spiral\Bootloader\Database\DatabaseBootloader;
23
use Spiral\Bootloader\TokenizerBootloader;
24
use Spiral\Config\ConfiguratorInterface;
25
use Spiral\Core\Container;
26
use Spiral\Tokenizer\ClassesInterface;
27
28
final class SchemaBootloader extends Bootloader implements DependedInterface, Container\SingletonInterface
29
{
30
    public const BINDINGS = [
31
        SchemaInterface::class             => [self::class, 'schema'],
32
33
        // annotated entities
34
        Annotated\Embeddings::class        => [self::class, 'embeddings'],
35
        Annotated\Entities::class          => [self::class, 'entities'],
36
        Annotated\MergeColumns::class      => [self::class, 'mergeColumns'],
37
        Annotated\MergeIndexes::class      => [self::class, 'mergeIndexes'],
38
39
        // relations
40
        Generator\GenerateRelations::class => [self::class, 'relations'],
41
    ];
42
43
    /** @var Container */
44
    private $container;
45
46
    /** @var ConfiguratorInterface */
47
    private $generators;
48
49
    /**
50
     * CycleSchemaBootloader constructor.
51
     *
52
     * @param Container $container
53
     */
54
    public function __construct(Container $container)
55
    {
56
        $this->container = $container;
57
        $this->generators = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array(Cycle\Annotated\Em...enerateTypecast::class) of type array<integer,string> is incompatible with the declared type Spiral\Config\ConfiguratorInterface of property $generators.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
            Annotated\Embeddings::class,
59
            Annotated\Entities::class,
60
            Generator\ResetTables::class,
61
            Annotated\MergeColumns::class,
62
            Generator\GenerateRelations::class,
63
            Generator\ValidateEntities::class,
64
            Generator\RenderTables::class,
65
            Generator\RenderRelations::class,
66
            Annotated\MergeIndexes::class,
67
            Generator\GenerateTypecast::class
68
        ];
69
    }
70
71
    /**
72
     * @param SchemaInterface|null $schema
73
     */
74
    public function boot(SchemaInterface $schema = null)
75
    {
76
        if (!is_null($schema)) {
77
            $this->bootRepositories($schema);
78
        }
79
    }
80
81
    /**
82
     * @param SchemaInterface $schema
83
     */
84
    public function bootRepositories(SchemaInterface $schema)
85
    {
86
        foreach ($schema->getRoles() as $role) {
87
            $repository = $schema->define($role, Schema::REPOSITORY);
88
            if ($repository === Repository::class || $repository === null) {
89
                // default repository can not be wired
90
                continue;
91
            }
92
93
            // initiate all repository dependencies using factory method forwarded to ORM
94
            $this->container->bind(
95
                $repository,
96
                new Container\Autowire(RepositoryInterface::class, ['role' => $role])
0 ignored issues
show
Bug introduced by
new Spiral\Core\Containe...array('role' => $role)) of type Spiral\Core\Container\Autowire is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

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

96
                /** @scrutinizer ignore-type */ new Container\Autowire(RepositoryInterface::class, ['role' => $role])
Loading history...
97
            );
98
        }
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function defineDependencies(): array
105
    {
106
        return [
107
            TokenizerBootloader::class,
108
            DatabaseBootloader::class
109
        ];
110
    }
111
112
    /**
113
     * @param mixed $generator
114
     */
115
    public function addGenerator($generator)
116
    {
117
        $this->generators[] = $generator;
118
    }
119
120
    /**
121
     * @return GeneratorInterface[]
122
     */
123
    public function getGenerators(): array
124
    {
125
        $result = [];
126
        foreach ($this->generators as $generator) {
127
            if (is_object($generator) && !$generator instanceof Container\Autowire) {
128
                $result[] = $generator;
129
            } else {
130
                $result[] = $this->container->get($generator);
131
            }
132
        }
133
134
        return $result;
135
    }
136
137
    /**
138
     * @param MemoryInterface $memory
139
     * @return SchemaInterface|null
140
     */
141
    protected function schema(MemoryInterface $memory): ?SchemaInterface
142
    {
143
        $schema = $memory->loadData('cycle');
144
        if (is_null($schema)) {
145
            return null;
146
        }
147
148
        return new Schema($schema);
0 ignored issues
show
Bug introduced by
It seems like $schema can also be of type string; however, parameter $schema of Cycle\ORM\Schema::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

148
        return new Schema(/** @scrutinizer ignore-type */ $schema);
Loading history...
149
    }
150
151
    /**
152
     * @param ClassesInterface $classes
153
     * @return Annotated\Embeddings
154
     */
155
    protected function embeddings(ClassesInterface $classes): Annotated\Embeddings
156
    {
157
        return new Annotated\Embeddings($classes);
158
    }
159
160
    /**
161
     * @param ClassesInterface $classes
162
     * @return Annotated\Entities
163
     */
164
    protected function entities(ClassesInterface $classes): Annotated\Entities
165
    {
166
        return new Annotated\Entities($classes);
167
    }
168
169
    /**
170
     * @return Annotated\MergeColumns
171
     */
172
    protected function mergeColumns(): Annotated\MergeColumns
173
    {
174
        return new Annotated\MergeColumns();
175
    }
176
177
    /**
178
     * @return Annotated\MergeIndexes
179
     */
180
    protected function mergeIndexes(): Annotated\MergeIndexes
181
    {
182
        return new Annotated\MergeIndexes();
183
    }
184
185
    /**
186
     * @return Generator\GenerateRelations
187
     */
188
    protected function relations(): Generator\GenerateRelations
189
    {
190
        return new Generator\GenerateRelations();
191
    }
192
}