Giter Site home page Giter Site logo

cf-helper-php's Introduction

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:

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

This helper works with official php buildpack.

Usage

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

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

And include it the page you want to load:

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

Get your service(s)

For example you have a service called database with this credentials:

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

You can simply get your service like this:

$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:

$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.

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.

To get this access just follow this this:

$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:

  • 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:

"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:

//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:

#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:

$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);

cf-helper-php's People

Contributors

arthurhlt avatar guilluxnow avatar nicolas-wallerand avatar samuelchemla avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cf-helper-php's Issues

"PHP message: PHP Notice: Undefined index: CfCommunity\CfHelper\CfHelper in /home/vcap/app/lib/vendor/sphring/sphring/src/Arthurh/Sphring/Runner/SphringRunner.php on line 62

Hi, I'm trying to log a couple of statements.
And Every time I see the following:

2015-06-23T15:40:30.04-0400 [App/0]      OUT 19:40:30 nginx   | 2015/06/23 19:40:30 [error] 42#0: *2 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: CfCommunity\CfHelper\CfHelper in /home/vcap/app/lib/vendor/sphring/sphring/src/Arthurh/Sphring/Runner/SphringRunner.php on line 62
2015-06-23T15:40:30.04-0400 [App/0]      OUT 19:40:30 nginx   | PHP message: [2015-06-23 19:40:30] CloudFoundry Helper.INFO: Service started [] []

The log messages are written in the same line ( no line break after each message)

Hello
I would like to thank you for this library. It is very helpful.
However, using the component logger, I noticed that all messages are written in the same line and no line break are added to the end of line.

Here is the code

$log = \CfCommunity\CfHelper\CfHelper;:getInstance()->getLogger();
$log->error("-- 1 -- Log message using the CloudFoundryLogger class ");
$log->error("-- 2 -- Log message using the CloudFoundryLogger class ");
$log->error("-- 3 -- Log message using the CloudFoundryLogger class ");

Here is the result log in the pivotal plateform

OUT 13:31:54 httpd   | [Tue Nov 14 13:31:54.948542 2017] [proxy_fcgi:error] [pid 51:tid 139866079753984] [client 172.16.1.2:57638] AH01071: Got error 'PHP message: PHP Notice:  Undefined index: CfCommunity\\CfHelper\\CfHe
lper in lib/vendor/sphring/sphring/src/Arthurh/Sphring/Runner/SphringRunner.php on line 62\nPHP message: [2017-11-14 13:31:54] CloudFoundry Helper.ERROR: -- 1 -- Log message using the CloudFoundryLogger class  [] []\nPHP message: [2017-11-14 13:31
:54] CloudFoundry Helper.ERROR: -- 2 -- Log message using the CloudFoundryLogger class  [] []\nPHP message: [2017-11-14 13:31:54] CloudFoundry Helper.ERROR: -- 3 -- Log message using the CloudFoundryLogger class  [] []\n'

And I would like to have

OUT 13:31:54 httpd   | [Tue Nov 14 13:31:54.948542 2017] [proxy_fcgi:error] [pid 51:tid 139866079753984] [client 172.16.1.2:57638] AH01071: Got error 'PHP message: PHP Notice:  Undefined index: CfCommunity\\CfHelper\\CfHe
lper in lib/vendor/sphring/sphring/src/Arthurh/Sphring/Runner/SphringRunner.php on line 62
PHP message: [2017-11-14 13:31:54] CloudFoundry Helper.ERROR: -- 1 -- Log message using the CloudFoundryLogger class  [] []
PHP message: [2017-11-14 13:31:54] CloudFoundry Helper.ERROR: -- 2 -- Log message using the CloudFoundryLogger class  [] []
PHP message: [2017-11-14 13:31:54] CloudFoundry Helper.ERROR: -- 3 -- Log message using the CloudFoundryLogger class  [] []\n'

Including 1.6.2 in latest Cachet

I seem to have an incompatibility using 1.6.2 with the latest version of the composer.json of Cachet.

The composer sometimes hangs indefinitely trying to include this repo (running composer update), sometimes fails on sphring inclusion. Have you had a try?

Unable to see log statement in a CF console

Hi, I have an app that is using logger from helper:

<?php
    // set up Composer autoloader
    require 'vendor/autoload.php';
    use CfCommunity\CfHelper\CfHelper;
    $logger = CfHelper::getInstance()->getLogger();
    ...........
    $logger->addInfo('###########################################');
    $logger->addInfo('LOG OCCURED');
    $logger->addInfo('###########################################');
?>

I don't see any log statements.
cf logs <APP_NAME>

I'm using the following command.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.