定制 samsonos/php_config 二次开发

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

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

samsonos/php_config

Composer 安装命令:

composer require samsonos/php_config

包简介

SamsonPHP configuration system

README 文档

README

OOP based configuration system. This approach uses all abilities of PHP OOP for creating configurations based on classes.

Latest Stable Version Build Status Code Coverage Code Climate Total Downloads Scrutinizer Code Quality Stories in Ready

Configuration scheme

Your project can have any amount of possible configurations, which is usually used for different environments, such as development, test, deploy, production stages, for this purposes we have created samsonos\config\Scheme, each of them corresponds to specific environment. In practice you should have base configuration folder, by default it is located at app/config folder.

Global configuration

In root of your app/config folder you should create your default entity configuration classes. If no current configuration environment would be specified, this entity configuration classes would be used automatically.

IMPORTANT! If you have specified some configuration environment(for example production) but you do not have entity configuration class for some module/object in it - Global configuration entity will be used for it instead if present.

Creating configuration environment

To create new configuration environment you should create new folder in your base configuration folder(by default it located at app/config), for example we want to create production environment, new folder path would be: app/config/production/. And all entity configuration classes there would correspond to your production configuration scheme, which will be created automatically.

Entity configuration

To configure your project modules\objects - you should use classes, for correct finding this classes among others, we force you to extend our base entity configuration class samsonos\config\Entity :

namespace project;

class entityIDConfig extends samsonos\config\Entity
{    
}

Your entity configuration class name should meet next logic: [EntityIdentifier]Config:

  • EntityIdentifier - configured module/object identifier
  • All entity configuration class names must end with Config

Your entity configuration class namespace should meet next logic:

  • Every class has to have any namespace defined(by default your project name)
  • Global entity configuration classes(located at base configuration folder), should have name space defined in previous list item.
  • Other environments entity configuration classes (located at other inner folders), should have namespace: [ParentNamespace]\[EnvironmentName], if we extend our previous entity configuration example, but now for production environment:
namespace project\production;

class entityIDConfig extends samsonos\config\Entity
{
    public $parameter = 'value';
}

IMPORTANT! As we use PSR-* standard - Class name must match file name

Possible configuration parameters

The main beauty of OOP configuration approach is that you can specify any possible by PHP values as predefined class field values, the main limitation is that they have to be public accessible:

namespace project;

class entityIDConfig extends samsonos\config\Entity
{    
    public $stringParameter = 'I am a string';
    public $integerParameter = 1123;
    public $arrayParameter = array('1', 123);
}

Prohibited operations

Unfortunately you cannot predefine calculations and string concatenations(and perform other operations) in class field values, but you can always perform it in __construct method. When configuration scheme is created, for every environment all entity configuration instances are being created - so their __construct method is performed, where you can make any possible operation.

namespace project;

class entityIDConfig extends samsonos\config\Entity
{    
    public $stringParameter = 'I am a string';
    public $integerParameter = 1123;
    public $arrayParameter = array('1', 123);
    public $complicatedParameter;
    
    public function __construct()
    {
        // Fill your parameter with what you want
        $this->complicatedParameter = $this->stringParameter . dirname(__DIR__); 
    }
}

Extending existing configurations

Also extending parent entity configuration is available, if want to add just some little changes to some entity configuration we can just extend it:

// Mention - class name is the same, only namespace is different
namespace project\other;

// We are extending global configuration from previous example
class entityIDConfig extends project\entityIDConfig
{
  // We overload only this parameter, as we want all to be the same as parent
  public $integerParameter = 2222;
}

Configuration manager

All project modules/objects configuration is processed via configuration manager samsonos\config\Manager, you must create instance of it, you must specify only one parameter Path to base configuration folder, for initialization of manager you should use init() method, this methods are specially separated to give ability to load entity configuration classes as you wish.

$manager = new \samsonos\config\Manager('.../path/to/base/folder');
$manager->init();

Loading entity configuration classes

We have created special static method for loading entity configuration samsonos\config\Manager::import($path), this method is used internally when configuration scheme is being created or loaded, for this tasks you should use init(path):

$manager = new \samsonos\config\Manager();
$manager->init('.../path/to/base/folder');

Switching configuration

If you want to change current active manager configuration use should use change($identifier) method:

  • $identifier - Environment identifier(Configuration scheme identifier)

Load schemes from different locations

If you have separate base configuration folders with different configurations, for example one project inside another, and inner project want to take some configurations from parent object, you can load configuration from other location:

$manager = new \samsonos\config\Manager();
$manager->init('.../path/to/base/folder');
$manager->init('.../path/to/OTHER/folder');

IMPORTANT! Entity configuration for same environments in different locations are ovveritten.

Event system

This module has only one external dependency - Event system and all interoperabily with outside world must me done using this approach. Currently module does not firing any events, but has 2 subscriptions:

  • core.environment.change - When environment is changing outside
  • core.module.configure - When some object is being configured

samsonos/php_config 适用场景与选型建议

samsonos/php_config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 174 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 174
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: Open
  • 更新时间: 2015-01-05