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