-
Notifications
You must be signed in to change notification settings - Fork 8
/
SoftMocksRequestPreparer.php
179 lines (159 loc) · 5.71 KB
/
SoftMocksRequestPreparer.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
declare(strict_types=1);
namespace Mougrim\XdebugProxy\RequestPreparer;
// Badoo\SoftMocks is optional for xdebug-proxy
use /** @noinspection PhpUndefinedClassInspection */
/** @noinspection PhpUndefinedNamespaceInspection */
Badoo\SoftMocks;
use Mougrim\XdebugProxy\Handler\CommandToXdebugParser;
use Mougrim\XdebugProxy\Xml\XmlDocument;
use Psr\Log\LoggerInterface;
use Throwable;
use function file_exists;
use function in_array;
use function is_file;
use function parse_url;
use function rawurldecode;
use function realpath;
/**
* @author Mougrim <[email protected]>
* @psalm-suppress UndefinedClass
*/
class SoftMocksRequestPreparer implements RequestPreparer
{
protected LoggerInterface $logger;
/**
* @param string $initScript
*
* @throws Error
*/
public function __construct(LoggerInterface $logger, string $initScript = null)
{
if ($initScript === null) {
$initScript = '';
}
$this->logger = $logger;
if (!$initScript) {
$possibleInitScriptPaths = [
__DIR__.'/../../vendor/badoo/soft-mocks/src/init_with_composer.php',
__DIR__.'/../../../../badoo/soft-mocks/src/init_with_composer.php',
];
foreach ($possibleInitScriptPaths as $possiblInitScriptPath) {
if (file_exists($possiblInitScriptPath)) {
$initScript = $possiblInitScriptPath;
break;
}
}
}
if (!$initScript) {
throw new Error("Can't find soft-mocks init script");
}
/**
* @psalm-suppress UnresolvableInclude
* @noinspection PhpIncludeInspection
*/
require $initScript;
}
public function prepareRequestToIde(XmlDocument $xmlRequest, string $rawRequest): void
{
$context = [
'request' => $rawRequest,
];
$root = $xmlRequest->getRoot();
if (!$root) {
return;
}
foreach ($root->getChildren() as $child) {
if (!in_array($child->getName(), ['stack', 'xdebug:message'], true)) {
continue;
}
$attributes = $child->getAttributes();
if (isset($attributes['filename'])) {
$filename = $this->getOriginalFilePath($attributes['filename'], $context);
if ($attributes['filename'] !== $filename) {
$this->logger->info("Change '{$attributes['filename']}' to '{$filename}'", $context);
$child->addAttribute('filename', $filename);
}
}
}
}
protected function getOriginalFilePath(string $file, array $context): string
{
// workaround some symbols like '+' are encoded like %2B
$file = rawurldecode($file);
/** @var array|false $parts */
$parts = parse_url($file);
if ($parts === false) {
$this->logger->warning("Can't parse file '{$file}'", $context);
return $file;
}
if ($parts['scheme'] !== 'file') {
$this->logger->warning("Scheme isn't file '{$file}'", $context);
return $file;
}
try {
/** @noinspection PhpUndefinedClassInspection */
return 'file://'.SoftMocks::getOriginalFilePath($parts['path']);
} catch (Throwable $throwable) {
$this->logger->warning("Can't get original file path: {$throwable}", $context);
return $file;
}
}
public function prepareRequestToXdebug(string $request, CommandToXdebugParser $commandToXdebugParser): string
{
[$command, $arguments] = $commandToXdebugParser->parseCommand($request);
$context = [
'request' => $request,
'arguments' => $arguments,
];
if ($command === 'breakpoint_set') {
if (isset($arguments['-f'])) {
$file = $this->getRewrittenFilePath($arguments['-f'], $context);
if ($file) {
$this->logger->info("Change '{$arguments['-f']}' to '{$file}'", $context);
$arguments['-f'] = $file;
$request = $commandToXdebugParser->buildCommand($command, $arguments);
}
} else {
$this->logger->error("Command {$command} is without argument '-f'", $context);
}
}
return $request;
}
protected function getRewrittenFilePath(string $file, array $context): string
{
$originalFile = $file;
$parts = parse_url($file);
if ($parts === false) {
$this->logger->warning("Can't parse file '{$file}'", $context);
return '';
}
if (($parts['scheme'] ?? '') !== 'file') {
$this->logger->warning("Scheme isn't file '{$file}'", $context);
return '';
}
try {
/** @noinspection PhpUndefinedClassInspection */
$rewrittenFile = (string) SoftMocks::getRewrittenFilePath($parts['path']);
} catch (Throwable $throwable) {
$this->logger->warning("Can't get rewritten file path: {$throwable}", $context);
return '';
}
if (!$rewrittenFile) {
return '';
}
if (is_file($rewrittenFile)) {
$file = realpath($rewrittenFile);
if (!$file) {
$this->logger->error("Can't get real path for {$rewrittenFile}", $context);
}
} else {
$this->logger->debug("Rewritten file '{$rewrittenFile}' isn't exists for '{$originalFile}'", $context);
$file = $rewrittenFile;
}
if (!$file) {
return '';
}
return 'file://'.$file;
}
}