【问题标题】:How to validate PHP in VS code using WSL php如何使用 WSL php 在 VS 代码中验证 PHP
【发布时间】:2017-02-02 07:42:51
【问题描述】:

在 VS 代码中,您可以使用 php 可执行文件来验证 php。但是有没有办法使用安装在 WSL 上的 php 代替?

【问题讨论】:

    标签: visual-studio-code windows-subsystem-for-linux


    【解决方案1】:

    什么对我有用:

    创建 php.bat 文件:

    @echo off
    set v_params=%*
    set v_params=%v_params:\=/%
    set v_params=%v_params:c:=/mnt/c%
    set v_params=%v_params:"=\"%
    @bash -c "php %v_params%"
    

    把它放在C:\WSL-Tools\php.bat

    然后,在 VScode 设置中(Ctrl+逗号):

    "php.validate.executablePath": "C:\\WSL-Tools\\php.bat"

    它有效。

    【讨论】:

      【解决方案2】:

      另一个答案对我也不起作用:经过一些工作,我想出了这两个脚本:

      这个叫php.bat,我放在C:\wsl-tools\

      @echo OFF
      setlocal ENABLEDELAYEDEXPANSION
      rem Collect the arguments and replace:
      rem  '\' with '/'
      rem  'c:' with 'mnt/c'
      rem  '"' with '\"'
      set v_params=%*
      set v_params=%v_params:\=/%
      set v_params=%v_params:C:=/mnt/c%
      set v_params=%v_params%
      set v_params=%v_params:"=\"%
      
      rem Call the windows-php inside WSL.
      rem windows-php is just a script which passes the arguments onto
      rem the original php executable and converts its output from UNIX
      rem syntax to Windows syntax.
      C:\Windows\sysnative\bash.exe -l -c "windows-php %v_params%"
      

      这个叫做 windows-php,放在 WSL 路径的某个地方(我选择了/usr/local/bin)。

      # Pass all the arguments to PHP.
      output=$(php "$@")
      # Perform UNIX->WINDOWS syntax replacements.
      output="${output//$'\n'/$'\r'$'\n'}"
      output="${output//\/mnt\/c/C:}"
      output="${output//\//\\}"
      # Echo corrected output.
      echo $output
      

      设置 "php.validate.executablePath": "c:\\wsl-tools\\php.bat" 对我有用。

      注意

      您可能想要关注this issuethis pull request,因为看起来这个问题将在下一个版本中得到正式解决。

      【讨论】:

      • 对于 x64 版本的 Visual Studio Code,您必须将第一个脚本的最后一行更改为:C:\Windows\system32\bash.exe -l -c "windows-php %v_params%"
      • C:\Windows\system32\wsl.exe "windows-php %v_params%" 因为 bash.exe 已被弃用,这是新的方法
      【解决方案3】:

      使用老式批处理文件可以做到这一点。

      php.bat

      @echo off
      c:\windows\sysnative\bash.exe -c "php %*"`
      

      setting.json

      "php.validate.executablePath": "c:\\PATH_TO\\php.bat"
      

      【讨论】:

      • 对于 php.bat,将“sysnative”替换为“system32”并删除字符串末尾的`。
      • 完美答案,但是 bat 文件最好是:bash.exe -c "php %*" 而不是:c:\windows\sysnative\bash.exe -c "p​​hp %*"`
      【解决方案4】:

      如果我还不算太晚,这就是使用 ubuntu 20.04 WSL 2 的方法

      @echo OFF
      setlocal ENABLEDELAYEDEXPANSION
      
      rem Collect the arguments and replace:
      rem  '\' with '/'
      rem  'c:' with 'mnt/c'
      rem  '"' with '\"'
      set v_params=%*
      IF DEFINED v_params (
          set v_params=%v_params:\=/%
          set v_params=%v_params:C:=/mnt/c%
          set v_params=%v_params%
          set v_params=%v_params:"=\"%
      )
      rem Call the windows-php inside WSL.
      rem windows-php is just a script which passes the arguments onto
      rem the original php executable and converts its output from UNIX
      rem syntax to Windows syntax.
      @wsl.exe -d Ubuntu-20.04 -- php %v_params%
      

      通过将这个 php.bat 放入

      c:/wsl-plugins/

      你不需要做 WSL 位。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-25
        • 1970-01-01
        • 1970-01-01
        • 2011-07-15
        • 1970-01-01
        相关资源
        最近更新 更多