1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Paysera\PhpStormHelper\Service; |
5
|
|
|
|
6
|
|
|
use RuntimeException; |
7
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
8
|
|
|
|
9
|
|
|
class WorkspaceConfigurationHelper |
10
|
|
|
{ |
11
|
|
|
private $domHelper; |
12
|
|
|
private $filesystem; |
13
|
|
|
|
14
|
24 |
|
public function __construct(DomHelper $domHelper, Filesystem $filesystem) |
15
|
|
|
{ |
16
|
24 |
|
$this->domHelper = $domHelper; |
17
|
24 |
|
$this->filesystem = $filesystem; |
18
|
24 |
|
} |
19
|
|
|
|
20
|
17 |
|
public function setupServerMappings(string $pathToWorkspaceXml, array $serverMappings) |
21
|
|
|
{ |
22
|
17 |
|
foreach ($serverMappings as $serverMapping) { |
23
|
11 |
|
$parts = explode('@', $serverMapping); |
24
|
11 |
|
if (count($parts) !== 2) { |
25
|
|
|
throw new RuntimeException(sprintf('Invalid server mapping format: %s', $serverMapping)); |
26
|
|
|
} |
27
|
|
|
|
28
|
11 |
|
$this->addServer($pathToWorkspaceXml, $parts[0], $parts[1]); |
29
|
|
|
} |
30
|
17 |
|
} |
31
|
|
|
|
32
|
11 |
|
private function addServer(string $pathToWorkspaceXml, string $hostWithPort, string $remoteRoot) |
33
|
|
|
{ |
34
|
11 |
|
if (strpos($hostWithPort, ':') === false) { |
35
|
8 |
|
$host = $hostWithPort; |
36
|
8 |
|
$name = $host; |
37
|
8 |
|
$port = null; |
38
|
|
|
} else { |
39
|
3 |
|
$parts = explode(':', $hostWithPort, 2); |
40
|
3 |
|
$host = $parts[0]; |
41
|
3 |
|
$port = $parts[1]; |
42
|
3 |
|
$name = $port === '80' ? $host : $hostWithPort; |
43
|
|
|
} |
44
|
|
|
|
45
|
11 |
|
$this->ensureWorkspaceFileExists($pathToWorkspaceXml); |
46
|
11 |
|
$document = $this->domHelper->loadDocument($pathToWorkspaceXml); |
47
|
|
|
|
48
|
11 |
|
$projectElement = $this->domHelper->findNode($document, 'project'); |
49
|
11 |
|
$serversComponent = $this->domHelper->findOrCreateChildNode( |
50
|
11 |
|
$projectElement, |
51
|
11 |
|
'component', |
52
|
11 |
|
['name' => 'PhpServers'] |
53
|
|
|
); |
54
|
|
|
|
55
|
11 |
|
$internalNode = $this->domHelper->findOrCreateChildNode($serversComponent, 'servers'); |
56
|
|
|
|
57
|
11 |
|
$serverNode = $this->domHelper->findOptionalNode($internalNode, 'server', ['host' => $host, 'port' => $port]); |
58
|
11 |
|
if ($serverNode === null) { |
59
|
8 |
|
$serverNode = $document->createElement('server'); |
60
|
8 |
|
$serverNode->setAttribute('id', $this->generateId($name)); |
61
|
8 |
|
$internalNode->appendChild($serverNode); |
62
|
|
|
} |
63
|
|
|
|
64
|
11 |
|
$serverNode->setAttribute('host', $host); |
65
|
11 |
|
$serverNode->setAttribute('name', $name); |
66
|
11 |
|
if ($port === null) { |
67
|
8 |
|
$serverNode->removeAttribute('port'); |
68
|
|
|
} else { |
69
|
3 |
|
$serverNode->setAttribute('port', $port); |
70
|
|
|
} |
71
|
11 |
|
$serverNode->setAttribute('use_path_mappings', 'true'); |
72
|
|
|
|
73
|
11 |
|
$pathMappings = $this->domHelper->findOrCreateChildNode($serverNode, 'path_mappings'); |
74
|
|
|
|
75
|
11 |
|
$this->domHelper->removeNodes($pathMappings, 'mapping'); |
76
|
11 |
|
$mapping = $this->domHelper->findOrCreateChildNode($pathMappings, 'mapping'); |
77
|
11 |
|
$mapping->setAttribute('local-root', '$PROJECT_DIR$'); |
78
|
11 |
|
$mapping->setAttribute('remote-root', $remoteRoot); |
79
|
|
|
|
80
|
11 |
|
$this->domHelper->saveDocument($pathToWorkspaceXml, $document); |
81
|
11 |
|
} |
82
|
|
|
|
83
|
9 |
|
public function configureComposer(string $pathToWorkspaceXml, string $composerExecutable = 'composer') |
84
|
|
|
{ |
85
|
9 |
|
$this->ensureWorkspaceFileExists($pathToWorkspaceXml); |
86
|
9 |
|
$document = $this->domHelper->loadDocument($pathToWorkspaceXml); |
87
|
|
|
|
88
|
9 |
|
$projectElement = $this->domHelper->findNode($document, 'project'); |
89
|
|
|
|
90
|
9 |
|
$component = $this->domHelper->findOrCreateChildNode( |
91
|
9 |
|
$projectElement, |
92
|
9 |
|
'component', |
93
|
9 |
|
['name' => 'ComposerSettings'] |
94
|
|
|
); |
95
|
|
|
|
96
|
9 |
|
$component->setAttribute('doNotAsk', 'true'); |
97
|
9 |
|
$component->setAttribute('synchronizationState', 'SYNCHRONIZE'); |
98
|
9 |
|
$component->setAttribute('updatePackages', 'false'); |
99
|
|
|
|
100
|
9 |
|
$pharConfigPath = $this->domHelper->findOrCreateChildNode($component, 'pharConfigPath'); |
101
|
9 |
|
$pharConfigPath->nodeValue = '$PROJECT_DIR$/composer.json'; |
102
|
|
|
|
103
|
9 |
|
$executablePath = $this->domHelper->findOrCreateChildNode($component, 'executablePath'); |
104
|
9 |
|
$executablePath->nodeValue = $composerExecutable; |
105
|
|
|
|
106
|
9 |
|
$this->domHelper->saveDocument($pathToWorkspaceXml, $document); |
107
|
9 |
|
} |
108
|
|
|
|
109
|
9 |
|
public function configureFileTemplateScheme(string $pathToWorkspaceXml) |
110
|
|
|
{ |
111
|
9 |
|
$this->ensureWorkspaceFileExists($pathToWorkspaceXml); |
112
|
9 |
|
$document = $this->domHelper->loadDocument($pathToWorkspaceXml); |
113
|
|
|
|
114
|
9 |
|
$projectElement = $this->domHelper->findNode($document, 'project'); |
115
|
|
|
|
116
|
9 |
|
$component = $this->domHelper->findOrCreateChildNode( |
117
|
9 |
|
$projectElement, |
118
|
9 |
|
'component', |
119
|
9 |
|
['name' => 'FileTemplateManagerImpl'] |
120
|
|
|
); |
121
|
9 |
|
$option = $this->domHelper->findOrCreateChildNode( |
122
|
9 |
|
$component, |
123
|
9 |
|
'option', |
124
|
9 |
|
['name' => 'SCHEME'] |
125
|
|
|
); |
126
|
9 |
|
$option->setAttribute('value', 'Project'); |
127
|
|
|
|
128
|
9 |
|
$this->domHelper->saveDocument($pathToWorkspaceXml, $document); |
129
|
9 |
|
} |
130
|
|
|
|
131
|
7 |
|
public function setupPhpUnitRunConfiguration(string $pathToWorkspaceXml) |
132
|
|
|
{ |
133
|
7 |
|
$this->ensureWorkspaceFileExists($pathToWorkspaceXml); |
134
|
7 |
|
$document = $this->domHelper->loadDocument($pathToWorkspaceXml); |
135
|
|
|
|
136
|
7 |
|
$projectElement = $this->domHelper->findNode($document, 'project'); |
137
|
|
|
|
138
|
7 |
|
$component = $this->domHelper->findOrCreateChildNode( |
139
|
7 |
|
$projectElement, |
140
|
7 |
|
'component', |
141
|
7 |
|
['name' => 'RunManager'] |
142
|
|
|
); |
143
|
7 |
|
$configuration = $this->domHelper->findOrCreateChildNode( |
144
|
7 |
|
$component, |
145
|
7 |
|
'configuration', |
146
|
7 |
|
['name' => 'PHPUnit', 'type' => 'PHPUnitRunConfigurationType', 'factoryName' => 'PHPUnit'] |
147
|
|
|
); |
148
|
|
|
|
149
|
7 |
|
$testRunnerNode = $this->domHelper->findOrCreateChildNode( |
150
|
7 |
|
$configuration, |
151
|
7 |
|
'TestRunner' |
152
|
|
|
); |
153
|
7 |
|
$testRunnerNode->setAttribute('scope', 'XML'); |
154
|
|
|
|
155
|
7 |
|
$methodNode = $this->domHelper->findOrCreateChildNode( |
156
|
7 |
|
$configuration, |
157
|
7 |
|
'method' |
158
|
|
|
); |
159
|
7 |
|
$methodNode->setAttribute('v', '2'); |
160
|
|
|
|
161
|
7 |
|
$this->domHelper->saveDocument($pathToWorkspaceXml, $document); |
162
|
7 |
|
} |
163
|
|
|
|
164
|
8 |
|
private function generateId(string $name) |
165
|
|
|
{ |
166
|
8 |
|
$hash = sha1($name); |
167
|
8 |
|
return substr($hash, 0, 8) . '-' |
168
|
8 |
|
. substr($hash, 8, 4) . '-' |
169
|
8 |
|
. substr($hash, 12, 4) . '-' |
170
|
8 |
|
. substr($hash, 16, 4) . '-' |
171
|
8 |
|
. substr($hash, 20, 12) |
172
|
|
|
; |
173
|
|
|
} |
174
|
|
|
|
175
|
24 |
|
private function ensureWorkspaceFileExists(string $pathToWorkspaceXml) |
176
|
|
|
{ |
177
|
24 |
|
if (file_exists($pathToWorkspaceXml)) { |
178
|
24 |
|
return; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$emptyWorkspaceFile = <<<'FILE' |
182
|
6 |
|
<?xml version="1.0" encoding="UTF-8"?> |
183
|
|
|
<project version="4"/> |
184
|
|
|
FILE; |
185
|
|
|
|
186
|
6 |
|
$directory = dirname($pathToWorkspaceXml); |
187
|
6 |
|
$this->filesystem->mkdir($directory); |
188
|
6 |
|
$this->filesystem->dumpFile($pathToWorkspaceXml, $emptyWorkspaceFile); |
189
|
6 |
|
} |
190
|
|
|
} |
191
|
|
|
|