ManagerSettings::setProxyGenerationMode()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
nc 3
nop 0
dl 0
loc 7
c 0
b 0
f 0
cc 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of orm
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Orm\Infrastructure\Persistence;
13
14
use Doctrine\DBAL\Tools\DsnParser;
15
use Doctrine\ORM\Proxy\ProxyFactory;
16
17
/**
18
 * ManagerSettings
19
 *
20
 * @package Slick\Orm\Infrastructure\Persistence
21
 */
22
final class ManagerSettings
23
{
24
25
    public const ATTRIBUTE_DRIVER_IMPL = 'attribute';
26
    public const XML_DRIVER_IMPL = 'xml';
27
    private const AUTOGENERATE_DEV = 99;
28
29
    /** @var array<string, mixed> */
30
    private static array $defaultSettings = [
31
        'url' => 'sqlite3:///:memory:?cache=shared',
32
        'entityPaths' => ["/src/Domain"],
33
        'proxiesDir' => "/tmp/Proxies",
34
        'proxiesNamespace' => 'App\Persistence\Proxies',
35
        'devMode' => true,
36
        'cache' => null,
37
        'SQLLogger' => null,
38
        'autoGenerateProxiesMode' => self::AUTOGENERATE_DEV,
39
        'implDriver' => self::ATTRIBUTE_DRIVER_IMPL,
40
        'autoGenerateProxyClasses' => true,
41
        'filterSchemaAssetsExpression' => null
42
    ];
43
44
    /** @var array<string, mixed>  */
45
    private array $settings;
46
47
    /**
48
     * @var array<string, mixed>
49
     */
50
    private array $connectionSettings;
51
52
    private string $proxiesDir;
53
54
    private string $proxiesNamespace;
55
56
    private bool $inDevMode;
57
58
    private ?string $cacheContainerId;
59
60
    private ?string $sqlLoggerContainerId;
61
62
    private int $generateProxiesMode;
63
64
    private string $implDriver;
65
66
    private bool $generateProxyClasses;
67
68
    private ?string $filterSchemaAssetsExpression;
69
70
    /**
71
     * Constructor for the class.
72
     *
73
     * @param array<string, mixed> $settings An array of settings.
74
     */
75
    public function __construct(array $settings = [])
76
    {
77
        $this->settings = [...self::$defaultSettings, ...$settings];
78
        $dsnParser = new DsnParser();
79
        $this->connectionSettings = $dsnParser->parse($this->settings['url']);
80
        $this->proxiesDir = $this->settings['proxiesDir'];
81
        $this->proxiesNamespace = $this->settings['proxiesNamespace'];
82
        $this->inDevMode = (bool) $this->settings['devMode'];
83
        $this->cacheContainerId = $this->settings['cache'];
84
        $this->sqlLoggerContainerId = $this->settings['SQLLogger'];
85
        $this->setProxyGenerationMode();
86
87
        $this->implDriver = $this->settings['implDriver'];
88
        $this->filterSchemaAssetsExpression = $this->settings['filterSchemaAssetsExpression'];
89
        $this->generateProxyClasses = (bool) $this->settings['autoGenerateProxyClasses'];
90
    }
91
92
    /**
93
     * Get the entity paths to search entities for.
94
     *
95
     * @return array<string> An array of entity paths.
96
     */
97
    public function entityPaths(): array
98
    {
99
        return $this->settings['entityPaths'];
100
    }
101
102
    /**
103
     * Returns the database connection settings.
104
     *
105
     * @return array<string, mixed> The connection settings.
106
     */
107
    public function connectionSettings(): array
108
    {
109
        return $this->connectionSettings;
110
    }
111
112
    /**
113
     * Get the directory for storing proxy classes.
114
     *
115
     * @return string The directory path for proxy classes.
116
     */
117
    public function proxiesDir(): string
118
    {
119
        return $this->proxiesDir;
120
    }
121
122
    /**
123
     * Retrieves the proxies namespace.
124
     *
125
     * @return string The proxies' namespace.
126
     */
127
    public function proxiesNamespace(): string
128
    {
129
        return $this->proxiesNamespace;
130
    }
131
132
    /**
133
     * Check if the application is in development mode.
134
     *
135
     * @return bool Returns true if the application is in development mode, false otherwise.
136
     */
137
    public function isInDevMode(): bool
138
    {
139
        return $this->inDevMode;
140
    }
141
142
    /**
143
     * Returns the cache container ID.
144
     *
145
     * @return string|null The cache container ID.
146
     */
147
    public function cacheContainerId(): ?string
148
    {
149
        return $this->cacheContainerId;
150
    }
151
152
    /**
153
     * Returns the SQL Logger container ID.
154
     *
155
     * @return string|null The SQL Logger container ID.
156
     */
157
    public function sqlLoggerContainerId(): ?string
158
    {
159
        return $this->sqlLoggerContainerId;
160
    }
161
162
    /**
163
     * Returns the auto-generate proxies mode.
164
     *
165
     * @return bool|int The auto-generate proxies mode.
166
     */
167
    public function proxyGenerationMode(): bool|int
168
    {
169
        return $this->generateProxiesMode;
170
    }
171
172
    /**
173
     * Returns the implementation driver.
174
     *
175
     * @return string The implementation driver.
176
     */
177
    public function implDriver(): string
178
    {
179
        return $this->implDriver;
180
    }
181
182
    /**
183
     * Returns the flag indicating whether to auto generate proxy classes.
184
     *
185
     * @return bool The flag indicating whether to auto generate proxy classes.
186
     */
187
    public function generateProxyClasses(): bool
188
    {
189
        return $this->generateProxyClasses;
190
    }
191
192
    public function filterSchemaAssetsExpression(): ?string
193
    {
194
        return $this->filterSchemaAssetsExpression;
195
    }
196
197
    /**
198
     * Sets the proxy generation mode based on the application settings.
199
     */
200
    private function setProxyGenerationMode(): void
201
    {
202
        $this->generateProxiesMode = $this->settings['autoGenerateProxiesMode'];
203
        if ($this->settings['autoGenerateProxiesMode'] === self::AUTOGENERATE_DEV) {
204
            $this->generateProxiesMode = $this->inDevMode
205
                ? ProxyFactory::AUTOGENERATE_EVAL
206
                : ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS;
207
        }
208
    }
209
}
210