Skip to content

Commit 81dd7f4

Browse files
tonischranzzobo
authored andcommitted
feat: Launch integrated dev webserver without router-script (program) (#389)
* Allow starting PHP without providing program (for built-in web server) * Add initial debug configuration for built-in web server. * Initial and configuration snippets. * Fixed missing Promise return type. * Changelog. * Readme.
1 parent 886e24d commit 81dd7f4

File tree

5 files changed

+85
-13
lines changed

5 files changed

+85
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [Unreleased]
8+
9+
### Added
10+
11+
- Option to start PHP built-in web server without router script.
12+
713
## [1.15.1]
814

915
### Changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,16 @@ This extension is a debug adapter between VS Code and [Xdebug](https://xdebug.or
5252

5353
### VS Code Configuration
5454

55-
In your project, go to the debugger and hit the little gear icon and choose _PHP_. A new launch configuration will be created for you with two configurations:
55+
In your project, go to the debugger and hit the little gear icon and choose _PHP_. A new launch configuration will be created for you with three configurations:
5656

5757
- **Listen for Xdebug**
58-
This setting will simply start listening on the specified port (by default 9000) for Xdebug. If you configured Xdebug like recommended above, everytime you make a request with a browser to your webserver or launch a CLI script Xdebug will connect and you can stop on breakpoints, exceptions etc.
58+
This setting will simply start listening on the specified port (by default 9000) for Xdebug. If you configured Xdebug like recommended above, every time you make a request with a browser to your webserver or launch a CLI script Xdebug will connect and you can stop on breakpoints, exceptions etc.
5959
- **Launch currently open script**
6060
This setting is an example of CLI debugging. It will launch the currently opened script as a CLI, show all stdout/stderr output in the debug console and end the debug session once the script exits.
61+
- **Launch Built-in web server**
62+
This configuration starts the PHP built-in web server on a random port and opens the browser with the `serverReadyAction` directive. The port is random (localhost:0) but can be changed to a desired fixed port (ex: localhost:8080). If a router script is needed, add it with `program` directive. Additional PHP/Xdebug directives trigger debugging on every page load.
63+
64+
More general information on debugging with VS Code can be found on https://code.visualstudio.com/docs/editor/debugging.
6165

6266
#### Supported launch.json settings:
6367

package.json

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@
146146
"debuggers": [
147147
{
148148
"type": "php",
149-
"languages": ["php"],
149+
"languages": [
150+
"php"
151+
],
150152
"label": "PHP",
151153
"program": "./out/phpDebug.js",
152154
"runtime": "node",
@@ -278,6 +280,25 @@
278280
"program": "${file}",
279281
"cwd": "${fileDirname}",
280282
"port": 9000
283+
},
284+
{
285+
"name": "Launch Built-in web server",
286+
"type": "php",
287+
"request": "launch",
288+
"runtimeArgs": [
289+
"-dxdebug.mode=debug",
290+
"-dxdebug.start_with_request=yes",
291+
"-S",
292+
"localhost:0"
293+
],
294+
"program": "",
295+
"cwd": "${workspaceRoot}",
296+
"port": 9000,
297+
"serverReadyAction": {
298+
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
299+
"uriFormat": "http://localhost:%s",
300+
"action": "openExternally"
301+
}
281302
}
282303
],
283304
"configurationSnippets": [
@@ -302,9 +323,32 @@
302323
"cwd": "^\"${2:\\${fileDirname\\}}\"",
303324
"port": 9000
304325
}
326+
},
327+
{
328+
"label": "PHP: Launch Built-in web server",
329+
"description": "Start built-in PHP web server and open browser on debug start",
330+
"body": {
331+
"name": "Launch Built-in web server",
332+
"type": "php",
333+
"request": "launch",
334+
"runtimeArgs": [
335+
"-dxdebug.mode=debug",
336+
"-dxdebug.start_with_request=yes",
337+
"-S",
338+
"localhost:${1:0}"
339+
],
340+
"program": "",
341+
"cwd": "^\"${2:\\${workspaceRoot\\}}\"",
342+
"port": 9000,
343+
"serverReadyAction": {
344+
"pattern": "Development Server \\\\(http://localhost:([0-9]+)\\\\) started",
345+
"uriFormat": "http://localhost:%s",
346+
"action": "openExternally"
347+
}
348+
}
305349
}
306350
]
307351
}
308352
]
309353
}
310-
}
354+
}

src/phpDebug.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,19 +212,22 @@ class PhpDebugSession extends vscode.DebugSession {
212212
/** launches the script as CLI */
213213
const launchScript = async () => {
214214
// check if program exists
215-
await new Promise<void>((resolve, reject) =>
216-
fs.access(args.program!, fs.constants.F_OK, err => (err ? reject(err) : resolve()))
217-
)
215+
if (args.program) {
216+
await new Promise<void>((resolve, reject) =>
217+
fs.access(args.program!, fs.constants.F_OK, err => (err ? reject(err) : resolve()))
218+
)
219+
}
218220
const runtimeArgs = args.runtimeArgs || []
219221
const runtimeExecutable = args.runtimeExecutable || 'php'
220222
const programArgs = args.args || []
223+
const program = args.program ? [args.program] : []
221224
const cwd = args.cwd || process.cwd()
222225
const env = args.env || process.env
223226
// launch in CLI mode
224227
if (args.externalConsole) {
225228
const script = await Terminal.launchInTerminal(
226229
cwd,
227-
[runtimeExecutable, ...runtimeArgs, args.program!, ...programArgs],
230+
[runtimeExecutable, ...runtimeArgs, ...program, ...programArgs],
228231
env
229232
)
230233
if (script) {
@@ -234,7 +237,7 @@ class PhpDebugSession extends vscode.DebugSession {
234237
})
235238
}
236239
} else {
237-
const script = childProcess.spawn(runtimeExecutable, [...runtimeArgs, args.program!, ...programArgs], {
240+
const script = childProcess.spawn(runtimeExecutable, [...runtimeArgs, ...program, ...programArgs], {
238241
cwd,
239242
env,
240243
})
@@ -341,7 +344,7 @@ class PhpDebugSession extends vscode.DebugSession {
341344
if (!args.noDebug) {
342345
await createServer()
343346
}
344-
if (args.program) {
347+
if (args.program || args.runtimeArgs) {
345348
await launchScript()
346349
}
347350
} catch (error) {

testproject/.vscode/launch.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,27 @@
1111
},
1212
{
1313
//"debugServer": 4711, // Uncomment for debugging the adapter
14-
"name": "Launch",
15-
"request": "launch",
14+
"name": "Launch currently open script",
1615
"type": "php",
16+
"request": "launch",
1717
"program": "${file}",
18+
"cwd": "${fileDirname}",
19+
"port": 9000
20+
},
21+
{
22+
//"debugServer": 4711, // Uncomment for debugging the adapter
23+
"name": "Launch Built-in web server",
24+
"type": "php",
25+
"request": "launch",
26+
"runtimeArgs": ["-dxdebug.mode=debug", "-dxdebug.start_with_request=yes", "-S", "localhost:0"],
27+
"program": "",
1828
"cwd": "${workspaceRoot}",
19-
"externalConsole": false
29+
"port": 9000,
30+
"serverReadyAction": {
31+
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
32+
"uriFormat": "http://localhost:%s",
33+
"action": "openExternally"
34+
}
2035
}
2136
]
2237
}

0 commit comments

Comments
 (0)