wpify / scoper
Composer plugin that scopes WordPress and WooCommerce dependencies for usage in WordPress plugins and themes.
Installs: 22 315
Dependents: 1
Suggesters: 0
Security: 0
Stars: 15
Watchers: 2
Forks: 2
Open Issues: 5
Type:composer-plugin
Requires
- php: ^8.1
- composer-plugin-api: ^2.3
- composer/composer: ^2.6
- wpify/php-scoper: ^0.18
Requires (Dev)
- jetbrains/phpstorm-stubs: ^v2024.1
- johnpbloch/wordpress: *
- nikic/php-parser: ^v4.19.1
- woocommerce/action-scheduler: *
- wp-cli/wp-cli: ^v2.11.0
- wpackagist-plugin/woocommerce: *
- yahnis-elsts/plugin-update-checker: ^v5.4
- dev-master
- 3.2.17
- 3.2.16
- 3.2.15
- 3.2.14
- 3.2.13
- 3.2.12
- 3.2.11
- 3.2.10
- 3.2.9
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- 2.5.4
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.9
- 2.4.8
- 2.4.7
- 2.4.6
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-version-3.2
- dev-version-3.1
This package is auto-updated.
Last update: 2024-10-15 18:10:08 UTC
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.json
file, that has exactly same structure likecomposer.json
file, but serves only for scoped dependencies. Dependencies that you don't want to scope comes tocomposer.json
. - Add
extra.wpify-scoper.prefix
to 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 install
orcomposer update
, all the dependencies specified incomposer-deps.json
will be scoped for you. - Add a
config.platform
option 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
autorun
defaults totrue
so that scoping is run automatically upon composerupdate
orinstall
command. That is not what you want in all cases, so you can set itfalse
if 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 install
orcomposer wpify-scoper update
. -
Scoped dependencies will be in
deps
folder 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__ . '/deps/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; }