Skip to content

Commit 18febc8

Browse files
committed
close #99
1 parent bdde785 commit 18febc8

7 files changed

Lines changed: 89 additions & 22 deletions

File tree

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,22 @@ This xrDebug PHP client uses the following default configuration.
5050
'host' => 'localhost',
5151
'port' => 27420,
5252
'key' => '',
53+
'localPath' => '/Users/<name>/Code', # C:\Users\<name>\Documents\Code on Windows
54+
'remotePath' => '/home/vagrant/Code',
5355
]
5456
```
5557

56-
| Property | Type | Effect |
57-
| --------- | ------ | ----------------------------------------- |
58-
| isEnabled | bool | Controls sending messages to the server |
59-
| isHttps | bool | Controls use of https |
60-
| host | string | The host where xrDebug server is running |
61-
| port | int | The Port to connect to the `host` |
62-
| key | string | Private key (ed25519) for signed requests |
63-
64-
> `host` The hostname or IP. When running xrDebug on Docker use `host.docker.internal`.
58+
| Property | Type | Effect |
59+
| ---------- | ------ | ----------------------------------------- |
60+
| isEnabled | bool | Controls sending messages to the server |
61+
| isHttps | bool | Controls use of https |
62+
| host | string | The host where xrDebug server is running |
63+
| port | int | The Port to connect to the `host` |
64+
| key | string | Private key (ed25519) for signed requests |
65+
| localPath | string | Local path to project root |
66+
| remotePath | string | Remote path to project root |
67+
68+
> `host`: The hostname or IP. When running xrDebug on Docker use `host.docker.internal`.
6569
6670
### File-based config
6771

src/Interfaces/MessageInterface.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,19 @@ public function id(): string;
4141

4242
public function writer(): WriterInterface;
4343

44-
public function withBody(string $body): self;
44+
public function withBody(string $body): static;
4545

46-
public function withTopic(string $topic): self;
46+
public function withTopic(string $topic): static;
4747

48-
public function withEmote(string $emote): self;
48+
public function withEmote(string $emote): static;
4949

50-
public function withWriter(WriterInterface $writer): self;
50+
public function withWriter(WriterInterface $writer): static;
5151

52-
public function withVariables(mixed ...$variables): self;
52+
public function withVariables(mixed ...$variables): static;
5353

54-
public function withFlags(int $flags): self;
54+
public function withFlags(int $flags): static;
55+
56+
public function withPath(string $path): static;
5557

5658
/**
5759
* @return array<string, string>

src/Message.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,47 +110,47 @@ public function writer(): WriterInterface
110110
return $this->writer;
111111
}
112112

113-
public function withBody(string $body): self
113+
public function withBody(string $body): static
114114
{
115115
$new = clone $this;
116116
$new->body = $body;
117117

118118
return $new;
119119
}
120120

121-
public function withTopic(string $topic): self
121+
public function withTopic(string $topic): static
122122
{
123123
$new = clone $this;
124124
$new->topic = $topic;
125125

126126
return $new;
127127
}
128128

129-
public function withEmote(string $emote): self
129+
public function withEmote(string $emote): static
130130
{
131131
$new = clone $this;
132132
$new->emote = $emote;
133133

134134
return $new;
135135
}
136136

137-
public function withWriter(WriterInterface $writer): self
137+
public function withWriter(WriterInterface $writer): static
138138
{
139139
$new = clone $this;
140140
$new->writer = $writer;
141141

142142
return $new;
143143
}
144144

145-
public function withVariables(mixed ...$variables): self
145+
public function withVariables(mixed ...$variables): static
146146
{
147147
$new = clone $this;
148148
$new->vars = $variables;
149149

150150
return $new;
151151
}
152152

153-
public function withFlags(int $flags): self
153+
public function withFlags(int $flags): static
154154
{
155155
$new = clone $this;
156156
if ($flags & XR_BACKTRACE) {
@@ -160,6 +160,14 @@ public function withFlags(int $flags): self
160160
return $new;
161161
}
162162

163+
public function withPath(string $path): static
164+
{
165+
$new = clone $this;
166+
$new->filePath = $path;
167+
168+
return $new;
169+
}
170+
163171
public function toArray(): array
164172
{
165173
$this->handleDumpVars();

src/Traits/ClientTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public function __construct(
3737
private int $port = 27420,
3838
bool $isHttps = false,
3939
private ?PrivateKey $privateKey = null,
40+
private string $localPath = '',
41+
private string $remotePath = '',
4042
?CurlInterface $curl = null,
4143
) {
4244
$this->curl = $curl ?? new Curl();
@@ -57,6 +59,8 @@ public function getUrl(string $endpoint): string
5759

5860
public function sendMessage(MessageInterface $message): void
5961
{
62+
$message = $this->getMessageWithPathMapping($message);
63+
6064
try {
6165
$curl = $this->getCurlHandle(
6266
'POST',
@@ -71,6 +75,8 @@ public function sendMessage(MessageInterface $message): void
7175

7276
public function sendPause(MessageInterface $message): void
7377
{
78+
$message = $this->getMessageWithPathMapping($message);
79+
7480
try {
7581
$curl = $this->getCurlHandle(
7682
'POST',
@@ -167,4 +173,15 @@ private function handleSignature(array $data): void
167173
];
168174
}
169175
}
176+
177+
private function getMessageWithPathMapping(MessageInterface $message): MessageInterface
178+
{
179+
if ($this->localPath === '' || $this->remotePath === '') {
180+
return $message;
181+
}
182+
183+
return $message->withPath(
184+
str_replace($this->remotePath, $this->localPath, $message->filePath())
185+
);
186+
}
170187
}

src/Xr.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function __construct(
4444
private string $host = 'localhost',
4545
private int $port = 27420,
4646
private string $key = '',
47+
private string $localPath = '',
48+
private string $remotePath = '',
4749
) {
4850
$this->setClient();
4951
}
@@ -158,6 +160,8 @@ private function setClient(): void
158160
port: $this->port,
159161
isHttps: $this->isHttps,
160162
privateKey: $this->privateKey,
163+
localPath: $this->localPath,
164+
remotePath: $this->remotePath,
161165
);
162166
}
163167
}

src/functions.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,18 @@ function xrConfig(
284284
string $host = 'localhost',
285285
int $port = 27420,
286286
string $key = '',
287+
string $localPath = '',
288+
string $remotePath = '',
287289
): void {
288290
new XrInstance(
289291
new Xr(
290292
isEnabled: $isEnabled,
291293
isHttps: $isHttps,
292294
host: $host,
293295
port: $port,
294-
key: $key
296+
key: $key,
297+
localPath: $localPath,
298+
remotePath: $remotePath
295299
)
296300
);
297301
}

tests/ClientTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,32 @@ public function testPauseError()
126126
$client->isPaused($message->id())
127127
);
128128
}
129+
130+
/**
131+
* @dataProvider providerSendWithPathMapping
132+
*/
133+
public function testClientWithPathMapping(string $method): void
134+
{
135+
$curl = new CurlError();
136+
$client = new Client(
137+
curl: $curl,
138+
localPath: '/app',
139+
remotePath: __DIR__
140+
);
141+
$message = new Message();
142+
$client->{$method}($message);
143+
parse_str($client->options()[CURLOPT_POSTFIELDS], $postFields);
144+
$this->assertSame(
145+
'/app/' . basename(__FILE__),
146+
$postFields['file_path']
147+
);
148+
}
149+
150+
public static function providerSendWithPathMapping(): array
151+
{
152+
return [
153+
['sendMessage'],
154+
['sendPause'],
155+
];
156+
}
129157
}

0 commit comments

Comments
 (0)