|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { WorkspaceFolder, DebugConfiguration, ProviderResult, CancellationToken } from 'vscode' |
| 3 | +import { LaunchRequestArguments } from './phpDebug' |
| 4 | +import * as which from 'which' |
| 5 | + |
| 6 | +export function activate(context: vscode.ExtensionContext) { |
| 7 | + context.subscriptions.push( |
| 8 | + vscode.debug.registerDebugConfigurationProvider('php', { |
| 9 | + async resolveDebugConfiguration( |
| 10 | + folder: WorkspaceFolder | undefined, |
| 11 | + debugConfiguration: DebugConfiguration & LaunchRequestArguments, |
| 12 | + token?: CancellationToken |
| 13 | + ): Promise<ProviderResult<DebugConfiguration>> { |
| 14 | + if (!debugConfiguration.type && !debugConfiguration.request && !debugConfiguration.name) { |
| 15 | + const editor = vscode.window.activeTextEditor |
| 16 | + if (editor && editor.document.languageId === 'php') { |
| 17 | + debugConfiguration.type = 'php' |
| 18 | + debugConfiguration.name = 'Launch (dynamic)' |
| 19 | + debugConfiguration.request = 'launch' |
| 20 | + debugConfiguration.program = '${file}' |
| 21 | + debugConfiguration.cwd = '${fileDirname}' |
| 22 | + debugConfiguration.port = 0 |
| 23 | + debugConfiguration.runtimeArgs = ['-dxdebug.start_with_request=yes'] |
| 24 | + debugConfiguration.env = { |
| 25 | + XDEBUG_MODE: 'debug,develop', |
| 26 | + XDEBUG_CONFIG: 'client_port=${port}', |
| 27 | + } |
| 28 | + // debugConfiguration.stopOnEntry = true |
| 29 | + } |
| 30 | + } |
| 31 | + if (debugConfiguration.program && !debugConfiguration.runtimeExecutable) { |
| 32 | + // See if we have runtimeExecutable configured |
| 33 | + const conf = vscode.workspace.getConfiguration('php') |
| 34 | + const executablePath = |
| 35 | + conf.get<string>('executablePath') || conf.get<string>('validate.executablePath') |
| 36 | + if (executablePath) { |
| 37 | + debugConfiguration.runtimeExecutable = executablePath |
| 38 | + } |
| 39 | + // See if it's in path |
| 40 | + if (!debugConfiguration.runtimeExecutable) { |
| 41 | + try { |
| 42 | + await which.default('php') |
| 43 | + } catch (e) { |
| 44 | + const selected = await vscode.window.showErrorMessage( |
| 45 | + 'PHP executable not found. Install PHP and add it to your PATH or set the php.executablePath setting', |
| 46 | + 'Open settings' |
| 47 | + ) |
| 48 | + if (selected === 'Open settings') { |
| 49 | + await vscode.commands.executeCommand('workbench.action.openGlobalSettings', { |
| 50 | + query: 'php.executablePath', |
| 51 | + }) |
| 52 | + return undefined |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return debugConfiguration |
| 58 | + }, |
| 59 | + }) |
| 60 | + ) |
| 61 | + |
| 62 | + context.subscriptions.push( |
| 63 | + vscode.commands.registerCommand('php.debug.debugPhpFile', async (uri: vscode.Uri) => { |
| 64 | + vscode.debug.startDebugging(undefined, { type: '', name: '', request: '' }) |
| 65 | + }) |
| 66 | + ) |
| 67 | +} |
0 commit comments