2017 © Pedro Peláez
 

library cf-helper-php

Cloudfoundry helper in php

image

cloudfoundry-community/cf-helper-php

Cloudfoundry helper in php

  • Friday, June 23, 2017
  • by ArthurHlt
  • Repository
  • 47 Watchers
  • 15 Stars
  • 18,656 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 7 Forks
  • 1 Open issues
  • 16 Versions
  • 6 % Grown

The README.md

cf-helper-php

An helper for php application inside cloudfoundry to access application and services bindings information without parsing the json-formatted VCAP_APPLICATION or VCAP_SERVICES env vars. This is similar to the https://www.npmjs.org/package/cfenv node package. You will never have to do this again:, (*1)

// Don't do this
$vcap_services = json_decode($_ENV['VCAP_SERVICES']);

This helper works with official php buildpack., (*2)

Usage

This php application is published as a composer package. Fetch it by adding the following to your composer.json:, (*3)

"cloudfoundry-community/cf-helper-php": "^2.0"

And include it the page you want to load:, (*4)

require_once __DIR__ .'/vendor/autoload.php';
use CfCommunity\CfHelper\CfHelper;
$cfHelper = new CfHelper();

You can access the service binding or application information through the service manager class, (*5)

Get your service(s)

For example you have a service called database with this credentials:, (*6)

{
    "hostname": "localhost",
    "username": "jojo",
    "password": "toto",
    "port": "3306"
}

You can simply get your service like this:, (*7)

$serviceManager = $cfHelper->getServiceManager();
$dbService = $serviceManager->getService('database'); //or regular expression example: getService('.*database.*')
//and for example get the host credential
$host = $dbService->getValue('hostname');//or regular expression example: getValue('ho[A-Za-z]+')

//get all your services
$services = $serviceManager->getAllServices();

//...

Get Application's informations

Simply like this:, (*8)

$applicationInfo = $cfHelper->getApplicationInfo();
$version = $applicationInfo->getVersion();
$name = $applicationInfo->getName();
$uris = $applicationInfo->getUris();

//for other information contains in VCAP_APPLICATION like limits get with that
$limits = $applicationInfo->limits;

Get a connector

cf-helper-php provide some connectors by auto-detecting., (*9)

It give you the possibility to have a PDO object when database is provided in services, or a Predis\Client object when you provide a redis (look at Predis ) or a MongoClient object when a mongodb is provided., (*10)

To get this access just follow this this:, (*11)

$pdo = $cfHelper->getDatabaseConnector()->getConnection();
$redis = $cfHelper->getRedisConnector()->getConnection();
$mongodb = $cfHelper->getMongoDbConnector()->getConnection();

You can directly get credentials by doing $cfHelper->get<TypeConnector>Connector()->getCredentials() it will give you an array with:, (*12)

  • host
  • port
  • pass
  • user
  • url (if url is provided by the service)
  • sentencePdo (only for database connector)
  • database (only for database connector)

Example usage of pdo connector

require_once __DIR__ .'/vendor/autoload.php';
use CfCommunity\CfHelper\CfHelper;
$cfHelper = new CfHelper();

//if we are in cloud foundry we use the connection given by cf-helper-php otherwise we use our database in local
if ($cfHelper->isInCloudFoundry()) {
    $db = $cfHelper->getDatabaseConnector()->getConnection();
} else {
    $db = new PDO('mysql:host=localhost;dbname=mydbinlocal;charset=utf8', 'root', '');
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//...

Set php configuration

With cf-helper-php we help you to set your php configuration, add in a new file in root project directory called cfhelper.json a php-ini variable and set your php configuration, example:, (*13)

"php-ini": {
    "display_errors": "On",
    "error_reporting": 24575, //equal to E_ALL & ~E_DEPRECATED
}

Set your php project in development mode

By default this two buildpacks hide error and it's not very good when you're in development phase. With cf-helper-php you can say that you are in development and app will do the rest and even show you error with filp/whoops package, to do that add in a new file in root project directory called cfhelper.json a cfhelper variable and put type variable in developement:, (*14)

//in cfhelper.json in your root project directory
"cfhelper":{
    "type": "development"
}

Simulate CloudFoundry environment

You can half simulate a CloudFoudry environment by using a manifest.yml, your environment variable from manifest will be set in $_ENV. You can also add simulate service by adding a key called serviceSimulate in your manifest.yml, example:, (*15)

#manifest.yml
---
#manifest
applications:
  - name: test
    memory: 1G
    env:
      MYAPP_APP_DIR: /home/vcap/app
      MYAPP_LOGS_DIR: /logs_dir
serviceSimulate:
  DATABASE: {"host": "localhost", "username": "jojo", "password": "toto", "port": "3306"} # a service database will be accessible, prefer writing with {'key": 'value'} to simplify your cups command

To run CloudFoundry simulation simply do:, (*16)

$cfHelper->simulateCloudFoundry(); //it use manifest.yml which is in the same folder where this script is called
//to set another manifest.yml:
$cfHelper->simulateCloudFoundry("your_manifest.yml);

The Versions

23/06 2017

dev-master

9999999-dev

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

23/06 2017

1.7.0

1.7.0.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

23/02 2017

1.6.5

1.6.5.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

13/12 2016

1.6.4

1.6.4.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

21/11 2016

1.6.3

1.6.3.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

17/06 2016

1.6.2

1.6.2.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

16/06 2016

1.6.1

1.6.1.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

16/09 2015

1.6.0

1.6.0.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

23/06 2015

1.5.1

1.5.1.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

12/05 2015

1.5.0

1.5.0.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

16/04 2015

1.4.9

1.4.9.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

16/04 2015

1.4.8

1.4.8.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

16/04 2015

1.4.7

1.4.7.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

11/04 2015

1.4.6

1.4.6.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

02/04 2015

1.4.5

1.4.5.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices

30/03 2015

1.4.2

1.4.2.0

Cloudfoundry helper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

services helper cloudfoundry vcapservices