承接 neam/php-app-config 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

neam/php-app-config

Composer 安装命令:

composer require neam/php-app-config

包简介

A PHP framework-agnostic approach to make the current [config](http://12factor.net/config) available to PHP applications and shell-scripts.

README 文档

README

A PHP framework-agnostic approach to make the current config available to PHP applications and shell-scripts.

For use by the application wherever it may be fit, for instance in the application's / PHP framework's configuration files.

Installation

  • Install via composer:
composer require neam/php-app-config:dev-develop
  • Copy the example/config folder into the root directory of your project.
cp -r vendor/neam/php-app-config/example/config config
  • Make sure that all app entry scripts include the file called include.php right after including the composer autoloader (adjust the path as necessary in each entry script):
// Make app config available as PHP constants
require(dirname(__FILE__) . '/../vendor/neam/php-app-config/include.php');
  • Replace config vars in your app with the corresponding PHP constants. Example of usage within Yii configuration:

Before:

'db' => array(
    'connectionString' => 'mysql:host=localhost;dbname=db',
    'emulatePrepare' => true,
    'username' => 'root',
    'password' => 'supersecret',
    'charset' => 'utf8',
    'enableParamLogging' => true, // Log SQL parameters
    //'schemaCachingDuration'=>3600*24,
),

After:

'db' => array(
    'connectionString' => 'mysql:host=' . DATABASE_HOST . (defined('DATABASE_PORT') && DATABASE_PORT != '' ? ';port=' . DATABASE_PORT : '') . ';dbname=' . DATABASE_NAME,
    'emulatePrepare' => true,
    'username' => DATABASE_USER,
    'password' => DATABASE_PASSWORD,
    'charset' => 'utf8',
    'enableParamLogging' => true, // Log SQL parameters
    //'schemaCachingDuration'=>3600*24,
),

How it works

Configuration directives are looked for in the following places:

  • $_ENV (which can be populated during a bootstrapping phase as in the local example config file)
  • php-fpm environment variables (set in php-fpm configuration files and available through getenv())
  • apache sub-process environment variables (set in Apache 2.x vhost config and available through apache_getenv())
  • php-cgi params (set in nginx location blocks using fastcgi_param and available through $_SERVER)

The include.php file will load a file (defaulting to project-root/config/local/include.php) that defines what application-specific configuration directives to expect.

To change the expected config include file, set the CONFIG_INCLUDE environment variable to the path to the include relative to the project root. For instance, in order to use project-root/config/remote/include.php, set the CONFIG_INCLUDE environment variable to config/remote/include.php.

Within the config include file, you define which directives to expect using the Config::expect() method:

Config::expect("DATABASE_URL", $default = null, $required = true);

The include.php file will then call Config::defineConstants() which will define expected configuration directives as constants.

Using the example config includes

The included example configuration includes sample config profiles used to deploy to Heroku-style services and/or running the code locally.

Remote deployment (Docker Cloud / Platform as a Service)

  • Deploy the app to Docker Cloud or a PaaS
  • Set the CONFIG_INCLUDE config var to config/remote/include.php

Example - Heroku

$ heroku config:set CONFIG_INCLUDE=config/remote/include.php
  • Set the expected config vars (DATABASE_URL, GA_TRACKING_ID etc) to their respective values

Your app should now run on Heroku using the expected config vars as PHP constants.

Local

The example local config include will first expect the remote, then load the overrides.php file, followed by a non-versioned file called secrets.php. To use the local configuration:

  • Create ang gitignore your secret local configuration file
cp .env.dist .env
echo ".env" >> .gitignore
  • Add general defaults/overrides that all developers use locally in the overrides.php file
  • Add secret defaults/overrides that should not be versioned to the secrets.php file

Your app should now run locally using the expected config vars as PHP constants.

Useful commands

To show your current config, you can run the following in a shell:

php -d variables_order="EGPCS" vendor/neam/php-app-config/export.php

To use the config in a shell-script:

source vendor/neam/php-app-config/shell-export.sh
# now the config is available to the shell-script

To export the current configuration to a file which can be sourced by a shell script:

php -d variables_order="EGPCS" vendor/neam/php-app-config/export.php > .exported.php-app-config.inc.sh

neam/php-app-config 适用场景与选型建议

neam/php-app-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 81.06k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2014 年 09 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「config」 「12factor」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 neam/php-app-config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 neam/php-app-config 我们能提供哪些服务?
定制开发 / 二次开发

基于 neam/php-app-config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 81.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 30
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2014-09-24