<?php
/**
 * Checks current directory for a running Vagrant virtual machine
 *
 * Author: John Kary <john@johnkary.net>
 */
$cwd = getcwd();
$uuid = getVagrantUuidInDir($cwd);

if (false === $uuid) {
    // No vagrant file in this dir
    exit(1);
}

$runningVms = getRunningVms();
if (!isVmRunning($runningVms, $uuid)) {
    // VM not running
    exit(1);
}

// VM running
exit(0);


function getVagrantUuidInDir($dir)
{
    $path = realpath($dir . '/.vagrant/machines/default/virtualbox/id');

    if (false === $path) {
        // File with UUID doesn't exist
        return false;
    }

    return file_get_contents($path);
}
function getRunningVms()
{
    exec('vboxmanage list runningvms', $output);

    return $output;
}
function isVmRunning($runningVms, $uuid)
{
    return (bool) strstr(implode(' ', $runningVms), $uuid);
}

<?php
/**
 * Checks current directory for a running Vagrant virtual machine
 *
 * Author: John Kary <john@johnkary.net>
 */
$cwd = getcwd();
$filecontents = getVagrantFileRunningInDir($cwd);

if (false === $filecontents) {
    // No vagrant file in this dir
    exit(1);
}

$uuid = getDirVagrantUuid($filecontents);
$runningVms = getRunningVms();

if (!isVmRunning($runningVms, $uuid)) {
    // VM not running
    exit(1);
}

// VM running
exit(0);


function getVagrantFileRunningInDir($dir)
{
    $path = realpath($dir . '/.vagrant');

    if (false === $path) {
        // File with UUID doesn't exist
        return false;
    }

    return file_get_contents($path);
}
function getDirVagrantUuid($filecontents)
{
    $vagrantJson = json_decode($filecontents);

    return $vagrantJson->active->default;
}
function getRunningVms()
{
    exec('vboxmanage list runningvms', $output);

    return $output;
}
function isVmRunning($runningVms, $uuid)
{
    return (bool) strstr(implode(' ', $runningVms), $uuid);
}

--- 
+++ 
@@ -5,16 +5,14 @@
  * Author: John Kary <john@johnkary.net>
  */
 $cwd = getcwd();
-$filecontents = getVagrantFileRunningInDir($cwd);
+$uuid = getVagrantUuidInDir($cwd);
 
-if (false === $filecontents) {
+if (false === $uuid) {
     // No vagrant file in this dir
     exit(1);
 }
 
-$uuid = getDirVagrantUuid($filecontents);
 $runningVms = getRunningVms();
-
 if (!isVmRunning($runningVms, $uuid)) {
     // VM not running
     exit(1);
@@ -24,9 +22,9 @@
 exit(0);
 
 
-function getVagrantFileRunningInDir($dir)
+function getVagrantUuidInDir($dir)
 {
-    $path = realpath($dir . '/.vagrant');
+    $path = realpath($dir . '/.vagrant/machines/default/virtualbox/id');
 
     if (false === $path) {
         // File with UUID doesn't exist
@@ -34,12 +32,6 @@
     }
 
     return file_get_contents($path);
-}
-function getDirVagrantUuid($filecontents)
-{
-    $vagrantJson = json_decode($filecontents);
-
-    return $vagrantJson->active->default;
 }
 function getRunningVms()
 {
Update for compatibility with Vagrant 1.2

1.2 changed how the box UUID was written to the filesystem. Instead of
JSON it's now written as a single file .vagrant/machines/default/virtualbox/id
