-
Notifications
You must be signed in to change notification settings - Fork 1
/
SuluPhpcrMigrationBundle.php
144 lines (125 loc) · 4.68 KB
/
SuluPhpcrMigrationBundle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Sulu\Bundle\PhpcrMigrationBundle;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
class SuluPhpcrMigrationBundle extends AbstractBundle
{
/**
* @param mixed[] $config
*/
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
$loader = new XmlFileLoader($builder, new FileLocator(__DIR__ . '/Resources/config'));
$loader->load('repository.xml');
$loader->load('session.xml');
$loader->load('command.xml');
$loader->load('parser.xml');
$loader->load('persister.xml');
}
public function configure(DefinitionConfigurator $definition): void
{
/** @var ArrayNodeDefinition $rootNode */
$rootNode = $definition->rootNode();
$rootNode
->children()
->scalarNode('DSN')->isRequired()->end()
->arrayNode('target')
->children()
->arrayNode('dbal')
->children()
->scalarNode('connection')->isRequired()->end()
->end()
->end()
->end()
->end()
->end();
}
public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
{
/** @var array{'DSN': string}[] $config */
$config = $builder->getExtensionConfig('sulu_phpcr_migration');
$dsn = $config[0]['DSN'];
$builder->setParameter('sulu_phpcr_migration.dsn', $dsn);
$targetConnectionName = $config[0]['target']['dbal']['connection'];
$builder->setAlias('sulu_phpcr_migration.target_connection', \sprintf('doctrine.dbal.%s_connection', $targetConnectionName));
$configuration = $this->getConnectionConfiguration($dsn);
$builder->setParameter('sulu_phpcr_migration.configuration', $configuration);
if ('dbal' === $configuration['connection']['type'] && $name = ($configuration['connection']['name'] ?? null)) {
$builder->setAlias('sulu_phpcr_migration.connection', \sprintf('doctrine.dbal.%s_connection', $name));
return;
}
}
/**
* @return array{
* connection: array{
* type: string,
* name?: string,
* url?: string,
* user?: string|null,
* password?: string|null
* },
* workspace: array{
* default: string,
* live: string
* }
* }
*/
private function getConnectionConfiguration(string $dsn): array
{
/** @var array{
* scheme: string,
* host?: string,
* port?: string,
* path?: string,
* query: string,
* user?: string,
* pass?: string
* } $parts
*/
$parts = \parse_url($dsn);
\parse_str($parts['query'], $query);
/** @var string|null $workspace */
$workspace = $query['workspace'] ?? '';
unset($query['workspace']);
if (!$workspace) {
throw new \InvalidArgumentException('Workspace is missing in DSN');
}
$result = [
'connection' => [
'type' => $parts['scheme'],
],
'workspace' => [
'default' => $workspace,
'live' => $workspace . '_live',
],
];
if ('dbal' === $parts['scheme'] && ($host = $parts['host'] ?? null)) {
$result['connection']['name'] = $host;
return $result;
}
$result['connection']['url'] = \implode('', \array_filter([
$parts['host'] ?? '',
isset($parts['port']) ? ':' . $parts['port'] : null,
$parts['path'] ?? null,
$query ? '?' . \http_build_query($query) : '',
], function($value) {
return null !== $value && '' !== $value;
}));
$result['connection']['user'] = $parts['user'] ?? null;
$result['connection']['password'] = $parts['pass'] ?? null;
return $result;
}
}