Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to run sublime text linter with php running in virtualbox vm?

Tags:

php

virtualbox

I am using sublime text 2, and I wanted to use the plugin sublimelinter. It checks the code in the background with php -l. I'm developing with a webserver running in a VirtualBox VM, webserver is in the VM (guest) and sublime text is running on the host.

Any way to get those 2 working together? I really wanted to avoid installing php outside the VM.

like image 623
JSamir Avatar asked Sep 10 '12 15:09

JSamir


1 Answers

I've got something that works reasonably well on my machine, but it seems fragile.

The Idea

The idea here is to create a command-line script on your host that passes all its parameters to a PHP call on your VM, via an SSH tunnel. SublimeLinter can then call this script as it would usually call the PHP binary, and from Sublime's point of view everything will "just work" without having to install PHP on your host.

I'm using both Windows and OS X hosts so I've got two versions of my tunnel script. Note that on Windows you'll need to fetch the Windows PuTTY SSH tools, but for Unix-flavored hosts SSH will likely already be present.

For Windows Hosts

  • Download the putty tools and unzip them somewhere useful like C:/Users/Youruser/bin/putty.
  • Create the batch script at C:/Users/Youruser/bin/php_vm_tunnel.bat with this content:
    @echo off
    C:/Users/Youruser/bin/putty/plink -l youruser -pw yourpassword 192.168.56.101 php %*
    

NOTE - be sure and replace the path to plink with the correct path where you unzipped putty above. Also, replace the -l and -pw flag values with the username and password that you use when you SSH into your VM. Finally, be sure and replace the IP address in the example with the IP address you use to SSH into your VM.

For Unix-ish Hosts (OS X, Linux, etc)

  • Create the bash script at ~\bin\php_vm_tunnel
    #!/usr/bin/env bash
    FIXED_ARGS='';
    for (( i = 1; i <= $# ; i++ )); do
        eval ARG=\$$i
        FIXED_ARGS="$FIXED_ARGS $(echo "$ARG" | awk '{gsub(".", "\\&");print}')"
    done
    ssh -l root 192.168.56.101 php $FIXED_ARGS
    

NOTE - be sure to replace the -l flag value with the username that you use when you SSH into your VM. Also, be sure to replace the IP address in the example with the IP address you use to SSH into your VM.

Testing the Script

At this point you have a script that should tunnel anything you pass to it through to your VM. So if in a terminal you were to do, say:

cd wherever_you_put_the_script 
php_vm_tunnel -v

You should see the PHP version info return from your virtual machine.

Sublime Text 2 Configuration

Now that you have this tunnel set up, you can configure Sublime Text 2 to make use of it:

  • Install the Package Control Sublime Text 2 plugin
  • Install the SublimeLinter plugin (it sounds like you were already at this point)
  • Go to Preferences -> Package Settings -> SublimeLinter -> Settings - User
  • Add this to the configuration file (again substituting in the proper path to the script and noting the double slashes to properly escape the backslash characters) and save:

    {
        "sublimelinter_executable_map":
        {
            "php": "C:\\\Users\\\Youruser\\\bin\\\php_vm_tunnel.bat"
            "php": "/Users/youruser/bin/php_vm_tunnel"
        }
    }
    

    NOTE - You can only have one "php" statement. The first one above is the Windows version, the second one is the Unix version. Delete the one you don't need.

  • restart Sublime Text

If you open the Sublime Text console (and everything went according to plan) you should see something in the startup text like like:

SublimeLinter: php enabled (using "C:\Users\Youruser\bin\php_vm_tunnel.bat" for executable)

And SublimeLinter should be properly linting PHP files as if it were calling a native PHP binary.

Good luck.

like image 52
Jonathan Hanson Avatar answered Nov 06 '22 01:11

Jonathan Hanson