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