wp-cli/wp-config-transformer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

wp-cli/wp-config-transformer

Composer 安装命令:

composer require wp-cli/wp-config-transformer

包简介

Programmatically edit a wp-config.php file.

README 文档

README

Programmatically edit a wp-config.php file.

Testing

Quick links: Using | Options | How it works | Testing

Using

Instantiate

$config_transformer = new WPConfigTransformer( '/path/to/wp-config.php' );

Edit constants

$config_transformer->update( 'constant', 'WP_DEBUG', 'true', array( 'raw' => true ) );
$config_transformer->add( 'constant', 'MY_SPECIAL_CONFIG', 'foo' );
$config_transformer->remove( 'constant', 'MY_SPECIAL_CONFIG' );

Edit variables

$config_transformer->update( 'variable', 'table_prefix', 'wp_custom_' );
$config_transformer->add( 'variable', 'my_special_global', 'foo' );
$config_transformer->remove( 'variable', 'my_special_global' );

Check for existence

if ( $config_transformer->exists( 'constant', 'MY_SPECIAL_CONFIG' ) ) {
	// do stuff
}

if ( $config_transformer->exists( 'variable', 'my_special_global' ) ) {
	// do stuff
}

Options

Special behaviors when adding or updating configs are available using the options array.

Normalization

In contrast to the "edit in place" strategy above, there is the option to normalize the output during a config update and effectively replace the existing syntax with output that adheres to WP Coding Standards.

Let's reconsider a poorly-formatted example:

                 define   (    'WP_DEBUG'   ,
    false, false     )
;

This time running:

$config_transformer->update( 'constant', 'WP_DEBUG', 'true', array( 'raw' => true, 'normalize' => true ) );

Now we will get an output of:

define( 'WP_DEBUG', true );

Nice!

Raw format

Suppose you want to change your ABSPATH config (gasp!). To do that, we can run:

$config_transformer->update( 'constant', 'ABSPATH', "dirname( __FILE__ ) . '/somewhere/else/'", array( 'raw' => true ) );

The raw option means that instead of placing the value inside the config as a string "dirname( __FILE__ ) . '/somewhere/else/'" it will become unquoted (and executable) syntax dirname( __FILE__ ) . '/somewhere/else/'.

Anchor string

The anchor string is the piece of text that additions will be anchored to.

$config_transformer->update( 'constant', 'FOO', 'bar', array( 'anchor' => '/** Absolute path to the WordPress directory' ) ); // Default

Anchor placement

By default, new configs will be placed before the anchor string.

$config_transformer->update( 'constant', 'FOO', 'bar', array( 'placement' => 'before' ) ); // Default
$config_transformer->update( 'constant', 'BAZ', 'qux', array( 'placement' => 'after' ) );

Anchor separator

By default, the separator between a new config and its anchor string is an EOL ("\n" on *nix and "\r\n" on Windows).

$config_transformer->update( 'constant', 'FOO', 'bar', array( 'separator' => PHP_EOL . PHP_EOL ) ); // Default
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'separator' => PHP_EOL ) );

Add if missing

By default, when attempting to update a config that doesn't exist, one will be added. This behavior can be overridden by specifying the add option and setting it to false.

$config_transformer->update( 'constant', 'FOO', 'bar', array( 'add' => true ) ); // Default
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'add' => false ) );

If the constant FOO exists, it will be updated in-place. And if not, the update will return false:

$config_transformer->exists( 'constant', 'FOO' ); // Returns false
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'add' => false ) ); // Returns false

How it works

Parsing configs

Constants: https://regex101.com/r/6AeNGP/4

Variables: https://regex101.com/r/cSLZZz/4

Editing in place

Due to the unsemantic nature of the wp-config.php file, and PHP's loose syntax in general, the WP Config Transformer takes an "edit in place" strategy in order to preserve the original formatting and whatever other obscurities may be taking place in the block. After all, we only care about transforming values, not constant or variable names.

To achieve this, the following steps are performed:

  1. A PHP block containing a config is split into distinct parts.
  2. Only the part containing the config value is targeted for replacement.
  3. The parts are reassembled with the new value in place.
  4. The old PHP block is replaced with the new PHP block.

Consider the following horrifically-valid PHP block, that also happens to be using the optional (and rare) 3rd argument for constant case-sensitivity:

                 define   (    'WP_DEBUG'   ,
    false, false     )
;

The "edit in place" strategy means that running:

$config_transformer->update( 'constant', 'WP_DEBUG', 'true', array( 'raw' => true ) );

Will give us a result that safely changes only the value, leaving the formatting and additional argument(s) unscathed:

                 define   (    'WP_DEBUG'   ,
    true, false     )
;

Option forwarding

Any option supported by the add() method can also be passed through the update() method and forwarded along when the config does not exist.

For example, you want to update the FOO constant in-place if it exists, otherwise it should be added to a special location:

$config_transformer->update( 'constant', 'FOO', 'bar', array( 'anchor' => '/** My special location' ) );

Which has the same effect as the long-form logic:

if ( $config_transformer->exists( 'constant', 'FOO' ) ) {
    $config_transformer->update( 'constant', 'FOO', 'bar' );
} else {
    $config_transformer->add( 'constant', 'FOO', 'bar', array( 'anchor' => '/** My special area' ) );
}

Of course the exception to this is if you are using the add => false option, in which case the update will return false and no config will be added.

Known issues

  1. Regex will only match one config definition per line.

CORRECT

define( 'WP_DEBUG', true );
define( 'WP_SCRIPT_DEBUG', true );
$table_prefix = 'wp_';
$my_var = 'foo';

INCORRECT

define( 'WP_DEBUG', true ); define( 'WP_SCRIPT_DEBUG', true );
$table_prefix = 'wp_'; $my_var = 'foo';
  1. If the third argument in define() is used, it must be a boolean.

CORRECT

define( 'WP_DEBUG', true, false );
define( 'WP_DEBUG', true, FALSE );
define( 'foo', true, true );
define( 'foo', true, TRUE );

INCORRECT

define( 'WP_DEBUG', true, 0 );
define( 'WP_DEBUG', true, 'yes' );
define( 'WP_DEBUG', true, 'this comma, will break everything' );

Testing

$ composer global require phpunit/phpunit
$ composer install
$ phpunit

wp-cli/wp-config-transformer 适用场景与选型建议

wp-cli/wp-config-transformer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.66M 次下载、GitHub Stars 达 85, 最近一次更新时间为 2018 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 wp-cli/wp-config-transformer 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 10.66M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 86
  • 点击次数: 26
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 85
  • Watchers: 8
  • Forks: 26
  • 开发语言: PHP

其他信息

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