tombroucke/wp-plugin-php-scoper-example

There is no license information available for the latest version (1.0.0) of this package.

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:wordpress-plugin

pkg:composer/tombroucke/wp-plugin-php-scoper-example

1.0.0 2025-12-02 15:03 UTC

This package is auto-updated.

Last update: 2025-12-04 12:42:15 UTC


README

This example plugin shows how you can introduce PHP-Scoper to your WordPress plugin.

In this project, PHP-scoper will prefix all dependencies. We build a vendor_prefixed directory that contains all the prefixed dependencies. devDependencies will be installed to the vendor directory, so we can run tests (composer test). As the scoped dependencies are not in the composer.json require list, this plugin can be installed alongside other version of dependencies. E.g. if your root composer.json requires psr/log ^1 or psr/log ^3, you will still be able to install this plugin.

See it in action

To install this example plugin you can run

composer require tombroucke/wp-plugin-php-scoper-example

Alternatively, you can download the zipfile and place it in your (app|wp-content)/plugins.

Building vendor_prefixed

Prerequisites

  • jq (brew install jq)
  • php-scoper (composer global require humbug/php-scoper)

Run the build script

Run composer build (or ./scoper.sh) to build the vendor_prefixed directory. This will:

  • Fetch dependencies that need prefixes from composer.json
  • Install these dependencies (composer require {dependency})
  • Run composer update
  • Add prefixes
  • Remove all devDependencies
  • Move the prefixed vendor dir from build/vendor to vendor_prefixed
  • Set custom suffix (PhpScoperExampleVendorSuffix) for ComposerAutoloaderInit
  • Remove the scoped dependencies from composer.json

Adding dependencies

To add dependencies, you can add them in extra.require-scoped in composer.json. After adding the dependency, you need to rebuild the vendor_prefixed directory.

Integrating in an existing plugin

  1. install jq (brew install jq) and php-scoper (composer global require humbug/php-scoper)
  2. Copy scoper.inc.php, replace 'prefix' => 'PhpScoperExampleVendor' with your prefix and 'exclude-namespaces' => ['Otomaties\\PhpScoperExample'] at the bottom of the file with the namespace of your plugin.
  3. Copy scoper.sh, replace PhpScoperExampleVendorSuffix with a custom suffix
  4. Copy the build scripts from composer.json
  5. Add your dependencies in extra.require-scoped in composer.json
  6. Run composer build
  7. Find & replace the namespaces in your plugin. (use Illuminate\Support\Collection > use PhpScoperExampleVendor\Illuminate\Support\Collection)
  8. Include vendor_prefixed in your repository (should NOT be .gitignored)

FAQ

Why not use the dependencies from require in composer.json?

I want this plugin to be installable with composer. If the project requires psr/log ^2, and my composer.json file requires psr/log ^3, composer would not be able to install the plugin.

Why do I need a custom suffix for ComposerAutoloaderInit?

The classname ComposerAutoloaderInit{calculated_hash} in vendor_prefixed would otherwise collide with the classname in vendor