appfog/af — pull request #26 by mexitek (2013-01-10 17:03:00+00:00)
Title: VCAP_SERVICES available for local development via AF TUNNEL

`VCAP_SERVICES` environment variable is essential to continuous integration and deployment strategies. `af tunnel` command is extremely useful for local development. It allows for our code to be versioned locally and for our dev db to be kept in appfog for syncing purposes.

The only key missing is allowing your code to grab database credentials from `VCAP_SERVICES` environment variable locally. CLI tools can not write environment variables for all user on any given machine. Environment variables created by `af` command line tool will only be available to `af` and not your app. This pull request writes the `VCAP_SERVICES` data to a file locally, allowing your app to look for it `IF` the environment variable does not exist.

```php
# Check for AppFogs ENV variable
if( getenv("VCAP_SERVICES") ) {
$json = getenv("VCAP_SERVICES");
}
# Check for local file, placed by: af tunnel
else if( file_exists("/etc/af_vcap_services") ) {
$json = file_get_contents("/etc/af_vcap_services");
}
# No DB credentials
else {
throw new Exception("No Database Information Available.", 1);
}

# Decode JSON and gather DB Info
$services_json = json_decode($json,true);
$mysql_config = $services_json["mysql-5.1"][0]["credentials"];
```

This works for me, not sure if it might work for others. Might need some adjusting.