|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Paysera\PhpStormHelper\Service; |
|
5
|
|
|
|
|
6
|
|
|
class WorkspaceConfigurationHelper |
|
7
|
|
|
{ |
|
8
|
|
|
private $domHelper; |
|
9
|
|
|
|
|
10
|
24 |
|
public function __construct(DomHelper $domHelper) |
|
11
|
|
|
{ |
|
12
|
24 |
|
$this->domHelper = $domHelper; |
|
13
|
24 |
|
} |
|
14
|
|
|
|
|
15
|
10 |
|
public function addServer(string $pathToWorkspaceXml, string $hostWithPort, string $remoteRoot) |
|
16
|
|
|
{ |
|
17
|
10 |
|
if (strpos($hostWithPort, ':') === false) { |
|
18
|
7 |
|
$name = $hostWithPort; |
|
19
|
7 |
|
$host = $name; |
|
20
|
7 |
|
$port = null; |
|
21
|
|
|
} else { |
|
22
|
3 |
|
$parts = explode(':', $hostWithPort, 2); |
|
23
|
3 |
|
$name = $parts[0]; |
|
24
|
3 |
|
$host = $name; |
|
25
|
3 |
|
$port = $parts[1]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
10 |
|
$document = $this->domHelper->loadDocument($pathToWorkspaceXml); |
|
29
|
|
|
|
|
30
|
10 |
|
$projectElement = $this->domHelper->findNode($document, 'project'); |
|
31
|
10 |
|
$serversComponent = $this->domHelper->findNode($projectElement, 'component', ['name' => 'PhpServers']); |
|
32
|
|
|
|
|
33
|
10 |
|
$internalNode = $this->domHelper->findOrCreateChildNode($serversComponent, 'servers'); |
|
34
|
|
|
|
|
35
|
10 |
|
$serverNode = $this->domHelper->findOptionalNode($internalNode, 'server', ['name' => $name]); |
|
36
|
10 |
|
if ($serverNode === null) { |
|
37
|
7 |
|
$serverNode = $document->createElement('server'); |
|
38
|
7 |
|
$serverNode->setAttribute('id', $this->generateId($name)); |
|
39
|
7 |
|
$internalNode->appendChild($serverNode); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
10 |
|
$serverNode->setAttribute('host', $host); |
|
43
|
10 |
|
$serverNode->setAttribute('name', $name); |
|
44
|
10 |
|
if ($port === null) { |
|
45
|
7 |
|
$serverNode->removeAttribute('port'); |
|
46
|
|
|
} else { |
|
47
|
3 |
|
$serverNode->setAttribute('port', $port); |
|
48
|
|
|
} |
|
49
|
10 |
|
$serverNode->setAttribute('use_path_mappings', 'true'); |
|
50
|
|
|
|
|
51
|
10 |
|
$pathMappings = $this->domHelper->findOrCreateChildNode($serverNode, 'path_mappings'); |
|
52
|
10 |
|
$mapping = $this->domHelper->findOrCreateChildNode($pathMappings, 'mapping'); |
|
53
|
10 |
|
$mapping->setAttribute('local-root', '$PROJECT_DIR$'); |
|
54
|
10 |
|
$mapping->setAttribute('remote-root', $remoteRoot); |
|
55
|
|
|
|
|
56
|
10 |
|
$this->domHelper->saveDocument($pathToWorkspaceXml, $document); |
|
57
|
10 |
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
public function configureComposer(string $pathToWorkspaceXml, string $composerExecutable = 'composer') |
|
60
|
|
|
{ |
|
61
|
3 |
|
$document = $this->domHelper->loadDocument($pathToWorkspaceXml); |
|
62
|
|
|
|
|
63
|
3 |
|
$projectElement = $this->domHelper->findNode($document, 'project'); |
|
64
|
|
|
|
|
65
|
3 |
|
$component = $this->domHelper->findOrCreateChildNode( |
|
66
|
3 |
|
$projectElement, |
|
67
|
3 |
|
'component', |
|
68
|
3 |
|
['name' => 'ComposerSettings'] |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
3 |
|
$component->setAttribute('doNotAsk', 'true'); |
|
72
|
3 |
|
$component->setAttribute('synchronizationState', 'SYNCHRONIZE'); |
|
73
|
3 |
|
$component->setAttribute('updatePackages', 'false'); |
|
74
|
|
|
|
|
75
|
3 |
|
$pharConfigPath = $this->domHelper->findOrCreateChildNode($component, 'pharConfigPath'); |
|
76
|
3 |
|
$pharConfigPath->nodeValue = '$PROJECT_DIR$/composer.json'; |
|
77
|
|
|
|
|
78
|
3 |
|
$executablePath = $this->domHelper->findOrCreateChildNode($component, 'executablePath'); |
|
79
|
3 |
|
$executablePath->nodeValue = $composerExecutable; |
|
80
|
|
|
|
|
81
|
3 |
|
$this->domHelper->saveDocument($pathToWorkspaceXml, $document); |
|
82
|
3 |
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
public function configureFileTemplateScheme(string $pathToWorkspaceXml) |
|
85
|
|
|
{ |
|
86
|
3 |
|
$document = $this->domHelper->loadDocument($pathToWorkspaceXml); |
|
87
|
|
|
|
|
88
|
3 |
|
$projectElement = $this->domHelper->findNode($document, 'project'); |
|
89
|
|
|
|
|
90
|
3 |
|
$component = $this->domHelper->findOrCreateChildNode( |
|
91
|
3 |
|
$projectElement, |
|
92
|
3 |
|
'component', |
|
93
|
3 |
|
['name' => 'FileTemplateManagerImpl'] |
|
94
|
|
|
); |
|
95
|
3 |
|
$option = $this->domHelper->findOrCreateChildNode( |
|
96
|
3 |
|
$component, |
|
97
|
3 |
|
'option', |
|
98
|
3 |
|
['name' => 'SCHEME'] |
|
99
|
|
|
); |
|
100
|
3 |
|
$option->setAttribute('value', 'Project'); |
|
101
|
|
|
|
|
102
|
3 |
|
$this->domHelper->saveDocument($pathToWorkspaceXml, $document); |
|
103
|
3 |
|
} |
|
104
|
|
|
|
|
105
|
7 |
|
private function generateId(string $name) |
|
106
|
|
|
{ |
|
107
|
7 |
|
$hash = sha1($name); |
|
108
|
7 |
|
return substr($hash, 0, 8) . '-' |
|
109
|
7 |
|
. substr($hash, 8, 4) . '-' |
|
110
|
7 |
|
. substr($hash, 12, 4) . '-' |
|
111
|
7 |
|
. substr($hash, 16, 4) . '-' |
|
112
|
7 |
|
. substr($hash, 20, 12) |
|
113
|
|
|
; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|