-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathide-config-updates.php
136 lines (115 loc) · 4.05 KB
/
ide-config-updates.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
<?php
$DB_USER = getenv('DB_USER');
$DB_PORT = getenv('DB_PORT');
$DB_NAME = getenv('DB_NAME');
const DATASOURCES_XML = '.idea/dataSources.xml';
const PHP_XML = '.idea/php.xml';
const WORKSPACE_XML = '.idea/workspace.xml';
const MISC_XML = '.idea/misc.xml';
$defaultXml = <<<END
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
</project>
END;
/*******************
* DATASOURCES XML
*/
$datasourcesXml = loadXml(DATASOURCES_XML, $defaultXml);
$component = getElement($datasourcesXml->documentElement, 'component', 'DataSourceManagerImpl', function (DOMElement $newEl) {
$newEl->setAttribute('format', 'xml');
$newEl->setAttribute('multifile-model', 'true');
});
$dataSource = getElement($component, 'data-source', 'neos-local');
// Build fake UUID deterministically
$dataSource->setAttribute('uuid', '01234567-890a-bcde-0123-' . substr(md5(getcwd()), 0, 12));
replace_child_xml(
$dataSource, <<<EOF
<driver-ref>mysql</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.mariadb.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mysql://127.0.0.1:$DB_PORT</jdbc-url>
<working-dir>\$ProjectFileDir$</working-dir>
<user-name>$DB_USER</user-name>
<schema-mapping>
<introspection-scope>
<node kind="schema" qname="neos" />
</introspection-scope>
</schema-mapping>
EOF
);
$datasourcesXml->save(DATASOURCES_XML);
/*******************
* PHP XML
*/
$phpXml = loadXml(PHP_XML, $defaultXml);
$component = getElement($phpXml->documentElement, 'component', 'PhpInterpreters');
$interpreters = getElement($component, 'interpreters');
$interpreter = getElement($interpreters, 'interpreter', 'PHP devenv.sh');
$interpreter->setAttribute('id', 'EC918378-8957-4AEA-9FA0-CD4A10A174E6');
$interpreter->setAttribute('home', '$PROJECT_DIR$/.devenv/profile/bin/php');
$interpreter->setAttribute('false', 'false');
$interpreter->setAttribute('debugger_id', 'php.debugger.XDebug');
$phpXml->save(PHP_XML);
/*******************
* WORKSPACE XML
*/
$workspaceXml = loadXml(WORKSPACE_XML, $defaultXml);
$component = getElement($workspaceXml->documentElement, 'component', 'PhpWorkspaceProjectConfiguration');
$component->setAttribute('interpreter_name', 'PHP devenv.sh');
$workspaceXml->save(WORKSPACE_XML);
/*******************
* MISC XML
*/
$miscXml = loadXml(MISC_XML, $defaultXml);
$component = getElement($miscXml->documentElement, 'component', 'NeosPluginSettings');
replace_child_xml($component, '
<option name="pluginEnabled" value="true" />
');
$miscXml->save(MISC_XML);
/*******************
* HELPERS
*/
function loadXml(string $filename, string $defaultXml): DOMDocument
{
if (!file_exists($filename)) {
file_put_contents(DATASOURCES_XML, $defaultXml);
}
$datasourcesXml = new DOMDocument();
$datasourcesXml->load($filename);
return $datasourcesXml;
}
function getElement(DOMElement $context, string $tagName, ?string $nameAttributeValue = null, ?Closure $newElCreator = null): DOMElement
{
$xpath = new DOMXPath($context->ownerDocument);
$xpathExpression = './' . $tagName;
if ($nameAttributeValue) {
$xpathExpression .= '[@name="' . $nameAttributeValue . '"]';
}
$xpathResult = $xpath->query($xpathExpression, $context);
if ($xpathResult->count() === 0) {
$newElement = $context->ownerDocument->createElement($tagName);
if ($nameAttributeValue) {
$newElement->setAttribute('name', $nameAttributeValue);
}
if ($newElCreator) {
$newElCreator($newElement);
}
$context->appendChild($newElement);
return $newElement;
} else {
$el = $xpathResult->item(0);
assert($el instanceof DOMElement);
return $el;
}
}
function replace_child_xml(DOMElement $parent, string $childXml): void
{
$childXml = (string)$childXml;
// empty all children
while ($parent->hasChildNodes()){
$parent->removeChild($parent->childNodes->item(0));
}
$fragment = $parent->ownerDocument->createDocumentFragment();
$fragment->appendXML($childXml);
$parent->appendChild($fragment);
}