From 3785b75582ef3df617ee3fcb9d4439c9c4396d43 Mon Sep 17 00:00:00 2001 From: Sameh Elhawary Date: Wed, 2 Dec 2026 18:29:45 +0200 Subject: [PATCH] feat(run): Symfony server run configuration (#7557) Symfony projects (framework dependency, bin/console and public/index.php present) get a detected PHP built-in server run configuration serving through the front controller; Debug keeps PHP's web-server arguments. Co-authored-by: samehelhawary <8745956+samehelhawary@users.noreply.github.com> --- crates/ide-run/src/detect.rs | 11 ++++++ crates/ide-run/tests/detect.rs | 35 +++++++++++++++++++ docs/en/languages/index.mdx | 2 +- docs/en/languages/php.mdx | 4 ++- docs/zh/languages/index.mdx | 2 +- docs/zh/languages/php.mdx | 4 ++- .../debugger/__tests__/launch.test.ts | 20 +++++++++++ src/features/debugger/launch.ts | 16 ++++++++- 8 files changed, 89 insertions(+), 5 deletions(-) diff --git a/crates/ide-run/src/detect.rs b/crates/ide-run/src/detect.rs index a43bb9479c..c9f907c28c 100644 --- a/crates/ide-run/src/detect.rs +++ b/crates/ide-run/src/detect.rs @@ -1450,6 +1450,17 @@ fn detect_php(root: &Path, out: &mut Vec) { if root.join("artisan").is_file() { out.push(config("php:artisan-serve", "artisan serve", "php", "php", &["artisan", "serve"]).debug("php-debug")); } + // A Symfony application: PHP's own web server on the front controller's folder. The + // Symfony CLI's `symfony serve` is not required, and cannot be given Xdebug's options. + if requires("symfony/framework-bundle") + && root.join("bin/console").is_file() + && root.join("public/index.php").is_file() + { + out.push( + config("php:symfony-serve", "Symfony server", "php", "php", &["-S", "127.0.0.1:8000", "-t", "public"]) + .debug("php-debug"), + ); + } let scripts = composer.as_ref().and_then(|c| c.get("scripts")).and_then(Json::as_object); for name in scripts.into_iter().flat_map(|m| m.keys()) { if is_composer_event(name) { diff --git a/crates/ide-run/tests/detect.rs b/crates/ide-run/tests/detect.rs index 219fc1b983..4ce6fdbf56 100644 --- a/crates/ide-run/tests/detect.rs +++ b/crates/ide-run/tests/detect.rs @@ -1286,6 +1286,41 @@ fn php_pest_project() { assert_eq!(find(&detect(dir.path()), "phpunit").name, "PHPUnit"); } +#[test] +fn php_framework_servers() { + // Laravel: `artisan` at the root. + let dir = project(&[("composer.json", r#"{"require":{"laravel/framework":"^12"}}"#), ("artisan", "")]); + let c = detect(dir.path()); + assert_eq!(ids(&c), ["php:artisan-serve"]); + let serve = find(&c, "php:artisan-serve"); + assert_eq!((serve.kind.as_str(), cmdline(serve).as_str()), ("php", "php artisan serve")); + assert_eq!(serve.debug_adapter.as_deref(), Some("php-debug")); + assert!(!serve.is_test); + + // Symfony: the framework bundle, its console and a front controller. + let symfony = r#"{"require":{"symfony/framework-bundle":"^7.2"}}"#; + let dir = project(&[("composer.json", symfony), ("bin/console", ""), ("public/index.php", "`, pytest, Django `runserver` and tests | -| PHP | `composer.json`, `phpunit.xml` | Composer scripts, PHPUnit or Pest, Laravel `artisan serve` | +| PHP | `composer.json`, `phpunit.xml` | Composer scripts, PHPUnit or Pest, Laravel `artisan serve`, Symfony server | | Ruby | `Gemfile`, `Rakefile`, `*.gemspec`, `config.ru` | Rails server, Rake tasks, RSpec, Minitest | | C / C++ | `CMakeLists.txt` | CMake targets | | C# | `.sln`, `.slnx`, `.csproj` | .NET Project, `dotnet test` | diff --git a/docs/en/languages/php.mdx b/docs/en/languages/php.mdx index fd9bb68fa8..618842ef7b 100644 --- a/docs/en/languages/php.mdx +++ b/docs/en/languages/php.mdx @@ -83,9 +83,11 @@ The PHPUnit run configuration type has **Target**, **Configuration file**, **Tes ## Debugging +- **Symfony server** in Symfony applications (`symfony/framework-bundle`, `bin/console` and `public/index.php`): PHP's own web server, `php -S 127.0.0.1:8000 -t public`. The Symfony CLI is not needed. + Debugging uses PHP Debug (vscode-php-debug) with Xdebug. The selected PHP must load the Xdebug extension; the PHP settings page shows whether it does. -- **Debug** works on PHP files, PHPUnit/Pest and `artisan serve`. The IDE turns on Xdebug for the session with `-d` options and a free port, so you don't need to edit `php.ini`, and another IDE listening on 9003 doesn't conflict. +- **Debug** works on PHP files, PHPUnit/Pest, `artisan serve` and the Symfony server. The IDE turns on Xdebug for the session with `-d` options and a free port, so you don't need to edit `php.ini`, and another IDE listening on 9003 doesn't conflict. - **PHP Remote Debug (Xdebug)** is a run configuration type that listens for incoming Xdebug connections (default port 9003), for requests served elsewhere such as php-fpm or a container. Its **Start the remote side with** field shows the matching `php -dxdebug.mode=debug …` command; for web requests use the `XDEBUG_SESSION` cookie or query parameter. Set **Local root** and **Remote root** to map paths. See [Debugging](/en/guide/debugging). diff --git a/docs/zh/languages/index.mdx b/docs/zh/languages/index.mdx index 98dd248b9e..81113e5fb5 100644 --- a/docs/zh/languages/index.mdx +++ b/docs/zh/languages/index.mdx @@ -200,7 +200,7 @@ IDE 安装工具从不需要 `sudo`。工具装在本地数据目录下 IDE 自 | Go | `go.mod` | Go Build、Go Test | | Java / Kotlin | `pom.xml`、Gradle 构建文件 | Maven 目标、Gradle 任务、Application、JAR Application、Spring Boot、Tomcat | | Python | `pyproject.toml`、`setup.py`、`requirements.txt`、`manage.py`、`.venv` 等 | `python -m `、pytest、Django `runserver` 和测试 | -| PHP | `composer.json`、`phpunit.xml` | Composer 脚本、PHPUnit 或 Pest、Laravel `artisan serve` | +| PHP | `composer.json`、`phpunit.xml` | Composer 脚本、PHPUnit 或 Pest、Laravel `artisan serve`、Symfony server | | Ruby | `Gemfile`、`Rakefile`、`*.gemspec`、`config.ru` | Rails server、Rake 任务、RSpec、Minitest | | C / C++ | `CMakeLists.txt` | CMake 目标 | | C# | `.sln`、`.slnx`、`.csproj` | .NET Project、`dotnet test` | diff --git a/docs/zh/languages/php.mdx b/docs/zh/languages/php.mdx index b3db9f1227..ca40436238 100644 --- a/docs/zh/languages/php.mdx +++ b/docs/zh/languages/php.mdx @@ -83,9 +83,11 @@ PHPUnit 运行配置类型包含 **目标**、**配置文件**、**测试过滤 ## 调试 +- Symfony 应用(依赖 `symfony/framework-bundle`,并有 `bin/console` 和 `public/index.php`)中的 **Symfony server**:PHP 自带的 Web 服务器,`php -S 127.0.0.1:8000 -t public`,不需要 Symfony CLI。 + 调试使用 PHP Debug(vscode-php-debug)配合 Xdebug。所选 PHP 必须已加载 Xdebug 扩展,PHP 设置页会显示是否已加载。 -- **调试** 适用于 PHP 文件、PHPUnit/Pest 和 `artisan serve`。IDE 通过 `-d` 选项为本次会话开启 Xdebug 并使用空闲端口,不需要改 `php.ini`,也不会与另一个监听 9003 端口的 IDE 冲突。 +- **调试** 适用于 PHP 文件、PHPUnit/Pest、`artisan serve` 和 Symfony server。IDE 通过 `-d` 选项为本次会话开启 Xdebug 并使用空闲端口,不需要改 `php.ini`,也不会与另一个监听 9003 端口的 IDE 冲突。 - **PHP 远程调试 (Xdebug)** 是一种运行配置类型,用于监听外部的 Xdebug 连接(默认端口 9003),适合调试 php-fpm、容器等其他地方处理的请求。其 **远程端启动命令** 字段给出对应的 `php -dxdebug.mode=debug …` 命令;Web 请求可使用 `XDEBUG_SESSION` cookie 或查询参数。用 **本地根目录** 和 **远程根目录** 做路径映射。 详见[调试](/zh/guide/debugging)。 diff --git a/src/features/debugger/__tests__/launch.test.ts b/src/features/debugger/__tests__/launch.test.ts index 2f124c3677..2af4dbf63c 100644 --- a/src/features/debugger/__tests__/launch.test.ts +++ b/src/features/debugger/__tests__/launch.test.ts @@ -158,6 +158,26 @@ describe("run config → launch config", () => { runtimeArgs: [...xdebug, "-dmemory_limit=1G"], runtimeExecutable: "/usr/bin/php8.4", }); + // PHP's own web server (Symfony): an option's value is not the script, and there is none. + const server = launchConfigFor( + rc({ kind: "php", command: "php", args: ["-S", "127.0.0.1:8000", "-t", "public"], debugAdapter: "php-debug" }), + "/app", + "/opt/php@8.3/bin/php", + )!; + expect(server).toMatchObject({ adapter: "php-debug", program: "", args: [], cwd: "/app" }); + expect(extra(server).runtimeArgs).toEqual([...xdebug, "-S", "127.0.0.1:8000", "-t", "public"]); + // … and with a router script, that is the program. + const router = launchConfigFor( + rc({ + kind: "php", + command: "php", + args: ["-S", "127.0.0.1:8000", "public/router.php"], + debugAdapter: "php-debug", + }), + "/app", + )!; + expect(router).toMatchObject({ program: "/app/public/router.php", args: [] }); + expect(extra(router).runtimeArgs).toEqual([...xdebug, "-S", "127.0.0.1:8000"]); // PHPUnit runs its script directly; without a PHP in Settings, php-debug uses the `php` on PATH. const unit = launchConfigFor( rc({ kind: "phpunit", command: "vendor/bin/phpunit", args: ["--filter", "testA"], debugAdapter: "php-debug" }), diff --git a/src/features/debugger/launch.ts b/src/features/debugger/launch.ts index ac4555b29b..7da89da4e8 100644 --- a/src/features/debugger/launch.ts +++ b/src/features/debugger/launch.ts @@ -484,6 +484,19 @@ function flutterAttach(rc: RunConfig, cwd: string, adapter: string): LaunchConfi const PHP_NAME = /^php(\d+(\.\d+)*)?(\.exe)?$/i; +/** PHP's options whose value is the next argument (`-S 127.0.0.1:8000`, `-t public`). */ +const PHP_VALUE_OPTIONS = new Set(["-S", "-t", "-c", "-d", "-z"]); + +/** Where the script is among `php`'s arguments, after its options and their values; -1 without one. */ +function phpScriptIndex(args: string[]): number { + for (let i = 0; i < args.length; i++) { + const arg = args[i]!; + if (PHP_VALUE_OPTIONS.has(arg)) i++; + else if (!arg.startsWith("-")) return i; + } + return -1; +} + /** * php-debug runs `runtimeExecutable runtimeArgs program args` and doesn't enable Xdebug: debug mode and * the connection back come from `-d` options, to the free port php-debug picks for `port: 0` (another @@ -497,8 +510,9 @@ function phpDebugLaunch(rc: RunConfig, cwd: string, php: string | null): LaunchC let args: string[]; if (PHP_NAME.test(name)) { // `php [options] script args`: the options stay with PHP; the script is a file (`artisan`). + // Its own web server (`php -S … -t public`) has none: every request is a session. if (name !== rc.command) runtime = resolveCommand(rc.command, cwd); - const at = rc.args.findIndex((a) => !a.startsWith("-")); + const at = phpScriptIndex(rc.args); runtimeArgs.push(...(at < 0 ? rc.args : rc.args.slice(0, at))); program = at < 0 ? "" : join(cwd, rc.args[at]!); args = at < 0 ? [] : rc.args.slice(at + 1);