Passed
Push — master ( ed2578...021b18 )
by Anton
05:05
created

SchemaBootloader::embeddings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
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\ORM\RepositoryInterface;
13
use Cycle\ORM\Schema;
14
use Cycle\ORM\SchemaInterface;
15
use Cycle\ORM\Select\Repository;
16
use Cycle\Schema\Generator;
17
use Cycle\Schema\GeneratorInterface;
18
use Spiral\Boot\Bootloader\Bootloader;
19
use Spiral\Boot\Bootloader\DependedInterface;
20
use Spiral\Boot\MemoryInterface;
21
use Spiral\Bootloader\Database\DatabaseBootloader;
22
use Spiral\Bootloader\TokenizerBootloader;
23
use Spiral\Config\ConfiguratorInterface;
24
use Spiral\Core\Container;
25
26
final class SchemaBootloader extends Bootloader implements DependedInterface, Container\SingletonInterface
27
{
28
    public const GROUP_INDEX       = 'index';
29
    public const GROUP_RENDER      = 'render';
30
    public const GROUP_POSTPROCESS = 'postprocess';
31
32
    public const BINDINGS = [
33
        SchemaInterface::class             => [self::class, 'schema'],
34
35
        // relations
36
        Generator\GenerateRelations::class => [self::class, 'relations'],
37
    ];
38
39
    /** @var Container */
40
    private $container;
41
42
    /** @var ConfiguratorInterface */
43
    private $generators = [];
44
45
    /**
46
     * CycleSchemaBootloader constructor.
47
     *
48
     * @param Container $container
49
     */
50
    public function __construct(Container $container)
51
    {
52
        $this->container = $container;
53
        $this->generators = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array(self::GROUP_INDEX ...nerateTypecast::class)) of type array<string,array|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...
54
            self::GROUP_INDEX       => [
55
                // find available entities
56
            ],
57
            self::GROUP_RENDER      => [
58
                // render tables and relations
59
                Generator\ResetTables::class,
60
                Generator\GenerateRelations::class,
61
                Generator\ValidateEntities::class,
62
                Generator\RenderTables::class,
63
                Generator\RenderRelations::class,
64
            ],
65
            self::GROUP_POSTPROCESS => [
66
                // post processing
67
                Generator\GenerateTypecast::class
68
            ],
69
        ];
70
    }
71
72
    /**
73
     * @param SchemaInterface|null $schema
74
     */
75
    public function boot(SchemaInterface $schema = null)
76
    {
77
        if (!is_null($schema)) {
78
            $this->bootRepositories($schema);
79
        }
80
    }
81
82
    /**
83
     * @param SchemaInterface $schema
84
     */
85
    public function bootRepositories(SchemaInterface $schema)
86
    {
87
        foreach ($schema->getRoles() as $role) {
88
            $repository = $schema->define($role, Schema::REPOSITORY);
89
            if ($repository === Repository::class || $repository === null) {
90
                // default repository can not be wired
91
                continue;
92
            }
93
94
            // initiate all repository dependencies using factory method forwarded to ORM
95
            $this->container->bind(
96
                $repository,
97
                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

97
                /** @scrutinizer ignore-type */ new Container\Autowire(RepositoryInterface::class, ['role' => $role])
Loading history...
98
            );
99
        }
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    public function defineDependencies(): array
106
    {
107
        return [
108
            TokenizerBootloader::class,
109
            DatabaseBootloader::class
110
        ];
111
    }
112
113
    /**
114
     * @param string $group
115
     * @param mixed  $generator
116
     */
117
    public function addGenerator(string $group, $generator)
118
    {
119
        $this->generators[$group] = $generator;
120
    }
121
122
    /**
123
     * @return GeneratorInterface[]
124
     */
125
    public function getGenerators(): array
126
    {
127
        $result = [];
128
        foreach ($this->generators as $group) {
129
            foreach ($group as $generator) {
130
                if (is_object($generator) && !$generator instanceof Container\Autowire) {
131
                    $result[] = $generator;
132
                } else {
133
                    $result[] = $this->container->get($generator);
134
                }
135
            }
136
        }
137
138
        return $result;
139
    }
140
141
    /**
142
     * @param MemoryInterface $memory
143
     * @return SchemaInterface|null
144
     */
145
    protected function schema(MemoryInterface $memory): ?SchemaInterface
146
    {
147
        $schema = $memory->loadData('cycle');
148
        if (is_null($schema)) {
149
            return null;
150
        }
151
152
        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

152
        return new Schema(/** @scrutinizer ignore-type */ $schema);
Loading history...
153
    }
154
155
    /**
156
     * @return Generator\GenerateRelations
157
     */
158
    protected function relations(): Generator\GenerateRelations
159
    {
160
        return new Generator\GenerateRelations();
161
    }
162
}