wpify/scoper
Composer 安装命令:
composer require wpify/scoper
包简介
Composer plugin that scopes WordPress and WooCommerce dependencies for usage in WordPress plugins and themes.
README 文档
README
Using Composer in your WordPress plugin or theme can benefit from that. But it also comes with a danger of conflicts with dependencies of other plugins or themes. Luckily, a great tool called PHP Scoper adds all your needed dependencies to your namespace to prevent conflicts. Unfortunately, the configuration is non-trivial, and for that reason, we created the Composer plugin to make scoping easy in WordPress projects.
The main issue with PHP Scoper is that it also scopes global functions, constants and classes. Usually, that is what you want, but that also means that WordPress functions, classes and constants will be scoped. This Composer plugin solves that. It has an up-to-date database of all WordPress and WooCommerce symbols that we want to keep unscoped.
Requirements
- wpify/scoper:3.1
- PHP 7.4 || 8.0
- wpify/scoper:3.2
- PHP >= 8.1
Usage
- This composer plugin is meant to be installed globally, but you can also require it as a dev dependency.
- The configuration requires creating
composer-deps.jsonfile, that has exactly same structure likecomposer.jsonfile, but serves only for scoped dependencies. Dependencies that you don't want to scope comes tocomposer.json. - Add
extra.wpify-scoper.prefixto youcomposer.json, where you can specify the namespace, where your dependencies will be in. All other config options (folder,globals,composerjson,composerlock,autorun) are optional. - The easiest way how to use the scoper on development environment is to install WPify Scoper as a dev dependency.
After each
composer installorcomposer update, all the dependencies specified incomposer-deps.jsonwill be scoped for you. - Add a
config.platformoption in your composer.json and composer-deps.json. This settings will make sure that the dependencies will be installed with the correct PHP version.
Example of composer.json with its default values
{
"config": {
"platform": {
"php": "8.0.30"
}
},
"scripts": {
"wpify-scoper": "wpify-scoper"
},
"extra": {
"wpify-scoper": {
"prefix": "MyNamespaceForDeps",
"folder": "deps",
"globals": [
"wordpress",
"woocommerce",
"action-scheduler",
"wp-cli"
],
"composerjson": "composer-deps.json",
"composerlock": "composer-deps.lock",
"autorun": true
}
}
}
-
Option
autorundefaults totrueso that scoping is run automatically upon composerupdateorinstallcommand. That is not what you want in all cases, so you can set itfalseif you need. To start prefixing manually, you need to add for example the line"wpify-scoper": "wpify-scoper"to the "scripts" section of your composer.json. You then run the script with the commandcomposer wpify-scoper installorcomposer wpify-scoper update. -
Scoped dependencies will be in
depsfolder of your project. You must include the scoped autoload alongside with the composer autoloader. -
After that, you can use your dependencies with the namespace.
Example PHP file:
<?php require_once __DIR__ . '/deps/scoper-autoload.php'; require_once __DIR__ . '/vendor/autoload.php'; new \MyNamespaceForDeps\Example\Dependency();
Deployment
Deployment with Gitlab CI
To use WPify Scoper with Gitlab CI, you can add the following job to your .gitlab-ci.yml file:
composer: stage: .pre image: composer:2 artifacts: paths: - $CI_PROJECT_DIR/deps - $CI_PROJECT_DIR/vendor expire_in: 1 week script: - PATH=$(composer global config bin-dir --absolute --quiet):$PATH - composer global config --no-plugins allow-plugins.wpify/scoper true - composer global require wpify/scoper - composer install --prefer-dist --optimize-autoloader --no-ansi --no-interaction --no-dev
Deployment with Github Actions
To use WPify Scoper with Github Actions, you can add the following action:
name: Build vendor jobs: install: runs-on: ubuntu-20.04 steps: - name: Checkout uses: actions/checkout@v2 - name: Cache Composer dependencies uses: actions/cache@v2 with: path: /tmp/composer-cache key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} - name: Install composer uses: php-actions/composer@v6 with: php_extensions: json version: 2 dev: no - run: composer global config --no-plugins allow-plugins.wpify/scoper true - run: composer global require wpify/scoper - run: sudo chown -R $USER:$USER $GITHUB_WORKSPACE/vendor - run: composer install --no-dev --optimize-autoloader - name: Archive plugin artifacts uses: actions/upload-artifact@v2 with: name: vendor path: | deps/ vendor/
Advanced configuration
PHP Scoper has plenty
of configuration options. You
can modify this configuration array by creating scoper.custom.php file in root of your project. The file should
contain customize_php_scoper_config function, where the first parameter is the preconfigured configuration array. Expected output is
valid PHP Scoper configuration array.
Example scoper.custom.php file
<?php function customize_php_scoper_config( array $config ): array { $config['patchers'][] = function( string $filePath, string $prefix, string $content ): string { if ( strpos( $filePath, 'guzzlehttp/guzzle/src/Handler/CurlFactory.php' ) !== false ) { $content = str_replace( 'stream_for($sink)', 'Utils::streamFor()', $content ); } return $content; }; return $config; }
wpify/scoper 适用场景与选型建议
wpify/scoper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 75.39k 次下载、GitHub Stars 达 33, 最近一次更新时间为 2021 年 09 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wpify/scoper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wpify/scoper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 75.39k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 33
- 点击次数: 30
- 依赖项目数: 9
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2021-09-09