1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Application\Packages\Data; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Doctrine\DBAL\Connection; |
22
|
|
|
use Doctrine\DBAL\DriverManager; |
23
|
|
|
use Limoncello\Application\Data\ModelSchemaInfo; |
24
|
|
|
use Limoncello\Contracts\Application\ContainerConfiguratorInterface; |
25
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
26
|
|
|
use Limoncello\Contracts\Data\ModelSchemaInfoInterface; |
27
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
28
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
29
|
|
|
use function array_filter; |
30
|
|
|
use function array_key_exists; |
31
|
|
|
use function is_array; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @package Limoncello\Application |
35
|
|
|
*/ |
36
|
|
|
class DataContainerConfigurator implements ContainerConfiguratorInterface |
37
|
|
|
{ |
38
|
|
|
/** @var callable */ |
39
|
|
|
const CONFIGURATOR = [self::class, self::CONTAINER_METHOD_NAME]; |
40
|
|
|
|
41
|
1 |
|
/** |
42
|
|
|
* @inheritdoc |
43
|
1 |
|
* |
44
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
45
|
1 |
|
* @SuppressWarnings(PHPMD.IfStatementAssignment) |
46
|
1 |
|
*/ |
47
|
|
|
public static function configureContainer(LimoncelloContainerInterface $container): void |
48
|
1 |
|
{ |
49
|
|
|
$container[ModelSchemaInfoInterface::class] = |
50
|
|
|
function (PsrContainerInterface $container): ModelSchemaInfoInterface { |
51
|
|
|
$settings = $container->get(SettingsProviderInterface::class)->get(DataSettings::class); |
52
|
1 |
|
$data = $settings[DataSettings::KEY_MODELS_SCHEMA_INFO]; |
53
|
1 |
|
|
54
|
1 |
|
return (new ModelSchemaInfo())->setData($data); |
55
|
|
|
}; |
56
|
|
|
|
57
|
|
|
$container[Connection::class] = function (PsrContainerInterface $container): Connection { |
58
|
|
|
$settings = $container->get(SettingsProviderInterface::class)->get(DoctrineSettings::class); |
59
|
|
|
$params = array_filter([ |
60
|
|
|
'driver' => $settings[DoctrineSettings::KEY_DRIVER] ?? null, |
61
|
|
|
'dbname' => $settings[DoctrineSettings::KEY_DATABASE_NAME] ?? null, |
62
|
|
|
'user' => $settings[DoctrineSettings::KEY_USER_NAME] ?? null, |
63
|
1 |
|
'password' => $settings[DoctrineSettings::KEY_PASSWORD] ?? null, |
64
|
|
|
'host' => $settings[DoctrineSettings::KEY_HOST] ?? null, |
65
|
1 |
|
'port' => $settings[DoctrineSettings::KEY_PORT] ?? null, |
66
|
1 |
|
'url' => $settings[DoctrineSettings::KEY_URL] ?? null, |
67
|
1 |
|
'memory' => $settings[DoctrineSettings::KEY_MEMORY] ?? null, |
68
|
|
|
'path' => $settings[DoctrineSettings::KEY_PATH] ?? null, |
69
|
1 |
|
'charset' => $settings[DoctrineSettings::KEY_CHARSET] ?? 'UTF8', |
70
|
|
|
], function ($value) { |
71
|
1 |
|
return $value !== null; |
72
|
1 |
|
}); |
73
|
1 |
|
$extra = $settings[DoctrineSettings::KEY_EXTRA] ?? []; |
74
|
|
|
|
75
|
1 |
|
$connection = DriverManager::getConnection($params + $extra); |
76
|
1 |
|
|
77
|
|
|
if (array_key_exists(DoctrineSettings::KEY_EXEC, $settings) === true && |
78
|
|
|
is_array($toExec = $settings[DoctrineSettings::KEY_EXEC]) === true && |
79
|
|
|
empty($toExec) === false |
80
|
1 |
|
) { |
81
|
|
|
foreach ($toExec as $statement) { |
82
|
|
|
$connection->exec($statement); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $connection; |
87
|
|
|
}; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|