tombroucke / wp-plugin-php-scoper-example
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
Requires (Dev)
- laravel/pint: ^1.26
- szepeviktor/phpstan-wordpress: ^2.0
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/vendortovendor_prefixed - Set custom suffix (
PhpScoperExampleVendorSuffix) forComposerAutoloaderInit - 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
- install jq (
brew install jq) and php-scoper (composer global require humbug/php-scoper) - 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. - Copy
scoper.sh, replacePhpScoperExampleVendorSuffixwith a custom suffix - Copy the build scripts from
composer.json - Add your dependencies in
extra.require-scopedincomposer.json - Run
composer build - Find & replace the namespaces in your plugin. (
use Illuminate\Support\Collection>use PhpScoperExampleVendor\Illuminate\Support\Collection) - Include
vendor_prefixedin 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 mycomposer.jsonfile requirespsr/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}invendor_prefixedwould otherwise collide with the classname invendor