Skip to content

Commit b78aa77

Browse files
committed
Add support for Docker based PHP Environmet
Support for Docker based PHP environment configurable with config keys: { "Laravel.dockerService": "php", // from `docker compose config --services` "Laravel.dockerBase": "/app", // from `docker compose exec {dockerService} pwd` } This plugin does not auto boot Docker container(s) although it can be implemented in later revisions. For now, ensure that Docker container is running with configured dockerService name and dockerBase path as working directory before attempting to use the plugin with Docker support.
1 parent 9b21b9c commit b78aa77

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

package.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
"sail",
233233
"lando",
234234
"ddev",
235+
"docker",
235236
"local"
236237
],
237238
"enumItemLabels": [
@@ -241,6 +242,7 @@
241242
"Sail",
242243
"Lando",
243244
"DDEV",
245+
"Docker",
244246
"Local"
245247
],
246248
"markdownEnumDescriptions": [
@@ -250,6 +252,7 @@
250252
"Sail",
251253
"Lando",
252254
"DDEV",
255+
"Docker",
253256
"Use PHP installed on the local machine."
254257
],
255258
"default": "auto",
@@ -259,6 +262,16 @@
259262
"type": "string",
260263
"description": "Template for running PHP code. Use {code} as an optional placeholder for the php file to run. e.g. `php \"{code}\"`.\n\nIf no {code} is present, code filepath will be appended to the command."
261264
},
265+
"Laravel.dockerService": {
266+
"type": "string",
267+
"default": "php",
268+
"description": "Name of the Docker service container holding PHP executable and Laravel app source code."
269+
},
270+
"Laravel.dockerBase": {
271+
"type": "string",
272+
"default": "/app",
273+
"description": "Base path of the Laravel app source code inside Docker container."
274+
},
262275
"Laravel.basePath": {
263276
"type": "string",
264277
"default": "",
@@ -674,4 +687,4 @@
674687
"vscode-languageserver-textdocument": "^1.0.11",
675688
"vscode-languageserver-types": "^3.17.5"
676689
}
677-
}
690+
}

src/support/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ type ConfigKey =
66
| "basePath"
77
| "phpEnvironment"
88
| "phpCommand"
9+
| "dockerService"
10+
| "dockerBase"
911
| "tests.docker.enabled"
1012
| "tests.ssh.enabled"
1113
| "tests.suiteSuffix"

src/support/php.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getTemplate, TemplateName } from "@src/templates";
22
import * as cp from "child_process";
33
import * as fs from "fs";
4+
import { sep as pathsep } from "path";
45
import * as vscode from "vscode";
56
import { BoundedFileCache } from "./cache";
67
import { config } from "./config";
@@ -110,6 +111,15 @@ const getPhpCommand = (): string => {
110111
let check = checks.shift();
111112
let result = "";
112113

114+
if (phpEnv === "docker") {
115+
const dockerService = config<string>("dockerService", "php");
116+
check = check?.replace("{dockerService}", dockerService);
117+
option.command = option.command.replace(
118+
"{dockerService}",
119+
dockerService,
120+
);
121+
}
122+
113123
while (check) {
114124
info(`Checking ${key} PHP installation: ${check}`);
115125

@@ -251,7 +261,7 @@ export const runInLaravel = <T>(
251261

252262
export const fixFilePath = (path: string) => {
253263
if (phpEnvironmentsThatUseRelativePaths.includes(phpEnvKey!)) {
254-
return relativePath(path);
264+
return relativePath(path).replaceAll(pathsep, "/");
255265
}
256266

257267
return path;

src/support/phpEnvironments.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type PhpEnvironment =
1414
| "sail"
1515
| "lando"
1616
| "ddev"
17+
| "docker"
1718
| "local";
1819

1920
export const phpEnvironments: Record<PhpEnvironment, PhpEnvironmentConfig> = {
@@ -52,6 +53,12 @@ export const phpEnvironments: Record<PhpEnvironment, PhpEnvironmentConfig> = {
5253
command: "ddev php",
5354
relativePath: true,
5455
},
56+
docker: {
57+
label: "Docker",
58+
check: "docker compose exec {dockerService} which php",
59+
command: 'docker compose exec {dockerService} "{binaryPath}"',
60+
relativePath: true,
61+
},
5562
local: {
5663
label: "Local",
5764
description: "Use PHP installed on the local machine.",

src/support/project.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import * as path from "path";
33
import * as vscode from "vscode";
44
import { config } from "./config";
5-
import { isPhpEnv } from "./php";
5+
import { getPhpEnv, isPhpEnv } from "./php";
66

77
let internalVendorExists: boolean | null = null;
88

@@ -35,6 +35,14 @@ export const pathForPhpEnv = (srcPath: string): string => {
3535
return srcPath.replace(new RegExp("^/var/www/html/"), "");
3636
}
3737

38+
if (srcPath.match(/^\//) && getPhpEnv() === "docker") {
39+
const dockerBase = config<string>("dockerBase", "/app").replace(
40+
/\/$/,
41+
"",
42+
);
43+
return projectPath(srcPath.replace(new RegExp(`^${dockerBase}/?`), ""));
44+
}
45+
3846
return srcPath;
3947
};
4048

0 commit comments

Comments
 (0)