定制 sroze/companienv 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

sroze/companienv

Composer 安装命令:

composer require sroze/companienv

包简介

Companion for .env files

README 文档

README

Your companion for .env files. Everybody knows about 12 factor and environments variables now. A lot of frameworks such as Symfony are using a .env file to configure the application, but we don't have anything to help users to complete their local .env file.

Companienv will helps you manage the .env files, from a reference .env.dist version in your code repository. Companienv can:

Usage

  1. Require sroze/companienv as your project dependency:
composer req sroze/companienv
  1. Run your companion:
vendor/bin/companienv

Composer automation

You can run Companienv automatically after composer install or composer update commands by configuring the scripts in your composer.json file:

{
    "scripts": {
        "post-install-cmd": [
            "Companienv\\Composer\\ScriptHandler::run"
        ],
        "post-update-cmd": [
            "Companienv\\Composer\\ScriptHandler::run"
        ]
    }
}

By default, the file used as a template is .env.dist and the written file is .env. You can change these defaults within your composer.json file:

{
    "extra": {
        "companienv-parameters": [
            {
                "file": ".env.foo",
                "dist-file": ".env.foo.dist"
            }
        ]
    }
}

The .env.dist file

All your configuration is directly in your .env.dist file, as comments. The configuration is divided in blocks that will be displayed to the user for a greater understanding of the configuration. Here are the fondations for Companienv:

  • Blocks. They logically group variables together. They are defined by a title (line starting with a double-comment ##) and a description (every comment line directly bellow)
  • Attributes. Defined by a line starting with #+, an attribute is associated to one or multiple variables. These attributes are the entrypoint for extensions. In the example above, it says that the JWT_* variables are associated with an RSA key pair, so Companienv will automatically offer the user to generate one for them.
  • Comments. Lines starting by #~ will be ignored by Companienv.

Example of .env.dist. file

# .env.dist

## Welcome in the configuration of [my-project]
#
#~ Please run the `bin/start` command.
#~ These lines starting with `~` are not going to be displayed to the user

## GitHub
# In order to be able to login with GitHub, you need to create a GitHub application. To get access to the code
# repositories, you need to create a GitHub integration.
#
#+file-to-propagate(GITHUB_INTEGRATION_KEY_PATH)
#
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GITHUB_INTEGRATION_ID=
GITHUB_INTEGRATION_KEY_PATH=
GITHUB_SECRET=

## Security
# We need sauce! Well, no, we need an SSL certificate.
#
#+rsa-pair(JWT_PRIVATE_KEY_PATH JWT_PUBLIC_KEY_PATH JWT_PRIVATE_KEY_PASS_PHRASE)
#
JWT_PRIVATE_KEY_PATH=/runtime/keys/jwt-private.pem
JWT_PUBLIC_KEY_PATH=/runtime/keys/jwt-public.pem
JWT_PRIVATE_KEY_PASS_PHRASE=

## Another block
# With its (optional) description
AND_OTHER_VARIABLES=

Built-in extensions

only-if extension

Some of the blocks of your .env file might not even be relevant if some other variable was disabling a given feature.

Example: This will only ask for the INTERCOM_APPLICATION_ID variable if INTERCOM_ENABLED has the value (current or entered by the user) true.

## Support & Feedback
# If you would like to allow your users to get some support from you, give you some feedback and this
# sort of things, select the integrations you'd like.
#
#+only-if(INTERCOM_APPLICATION_ID):(INTERCOM_ENABLED=true)
#
INTERCOM_ENABLED=false
INTERCOM_APPLICATION_ID=none

file-to-propagate extension

Will ask the path of an existing file and copy it to the destination mentioned in the reference.

Example: this will ask the user to give the path of an existing file. It will copy this file to the path /runtime/keys/firebase.json, relative to the root directory of the project.

#+file-to-propagate(FIREBASE_SERVICE_ACCOUNT_PATH)
FIREBASE_SERVICE_ACCOUNT_PATH=/runtime/keys/firebase.json

rsa-pair extension

If the public/private key pair does not exist, Companienv will offer to generate one for the user.

#+rsa-pair(JWT_PRIVATE_KEY_PATH JWT_PUBLIC_KEY_PATH JWT_PRIVATE_KEY_PASS_PHRASE)
JWT_PRIVATE_KEY_PATH=/runtime/keys/jwt-private.pem
JWT_PUBLIC_KEY_PATH=/runtime/keys/jwt-public.pem
JWT_PRIVATE_KEY_PASS_PHRASE=

ssl-certificate-extension

Similar to the RSA keys pair: Companienv will offer to generate a self-signed SSL certificate if it does not exist yet.

#+ssl-certificate(SSL_CERTIFICATE_PRIVATE_KEY_PATH SSL_CERTIFICATE_CERTIFICATE_PATH SSL_CERTIFICATE_DOMAIN_NAME)
SSL_CERTIFICATE_PRIVATE_KEY_PATH=/runtime/keys/server.key
SSL_CERTIFICATE_CERTIFICATE_PATH=/runtime/keys/server.crt
SSL_CERTIFICATE_DOMAIN_NAME=

Your own extensions

You can easily create and use your own extensions with Companienv. In order to do so, you'll have to start Companienv with your own PHP file and use the registerExtension method of the Application:

use Companienv\Application;
use Companienv\Extension;

$application = new Application($rootDirectory);
$application->registerExtension(new class() implements Extension {
    // Implements the interface...
});
$application->run();

sroze/companienv 适用场景与选型建议

sroze/companienv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 424.62k 次下载、GitHub Stars 达 246, 最近一次更新时间为 2018 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 sroze/companienv 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 424.62k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 246
  • 点击次数: 15
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 246
  • Watchers: 6
  • Forks: 13
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-29