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