cloudfoundry-community/cf-helper-php

Cloudfoundry的PHP辅助工具

2.0.1 2020-01-08 10:17 UTC

This package is auto-updated.

Last update: 2024-08-30 01:32:02 UTC


README

为Cloudfoundry内部的PHP应用程序提供一个辅助工具,以便在无需解析JSON格式的VCAP_APPLICATIONVCAP_SERVICES环境变量的情况下访问应用程序和服务绑定信息。这与https://npmjs.net.cn/package/cfenv节点包类似。您将再也不必这样做

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

此辅助工具与官方php buildpack兼容。

用法

此PHP应用程序作为composer包发布。通过在您的composer.json中添加以下内容来获取它

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

并在您想要加载的页面上包含它

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

您可以通过服务管理类访问服务绑定或应用程序信息

获取您的服务

例如,您有一个名为database的服务,其凭证如下

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

您可以简单地通过这种方式获取您的服务

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

//...

获取应用程序信息

就像这样

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

获取连接器

cf-helper-php通过自动检测提供一些连接器。

它为您提供在服务中提供数据库时获得PDO对象的可能性,或者在提供redis(请参阅Predis)或MongoDB时获得Predis\Client对象的可能性,或者当提供MongoDB时获得MongoClient对象的可能性。

要获取此访问权限,请按照以下步骤进行

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

您可以直接通过执行$cfHelper->get<TypeConnector>Connector()->getCredentials()来获取凭据,它将返回一个包含以下内容的数组

  • 主机
  • 端口
  • 密码
  • 用户
  • URL(如果服务提供了URL)
  • pdo(仅适用于数据库连接器)
  • 数据库名(仅适用于数据库连接器)

pdo连接器的示例用法

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);
//...

设置PHP配置

使用cf-helper-php,我们帮助您设置PHP配置,在根项目目录中添加一个名为cfhelper.json的新文件,并设置您的PHP配置,例如

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

将PHP项目设置为开发模式

默认情况下,这两个构建包会隐藏错误,这在开发阶段并不好。使用cf-helper-php,您可以告诉您处于开发状态,应用程序将完成其余工作,甚至可以使用filp/whoops包显示错误,为此,在根项目目录中添加一个名为cfhelper.json的新文件,并添加一个名为cfhelper的变量,将type变量放在development

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

模拟CloudFoundry环境

您可以通过使用manifest.yml来半模拟一个CloudFoudry环境,您的manifest中的环境变量将在$_ENV中设置。您还可以在manifest.yml中添加模拟服务,通过添加一个名为serviceSimulate的键来实现,例如

#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

要运行CloudFoundry模拟,请这样做

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