-
-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use this to prefix a WordPress plugin? #442
Comments
I see that Yoast and Google Site Kit uses PHP Scoper to prefix their WordPress plugins. Both implementations seem to go through a fair amount of effort to implement their own autoloading solutions for the prefixed vendor classes. Yoast Prefixes the vendor classes, removes the unprefixed ones from Composer Autoloader using bash automation, then implement a Google Site Kit Takes a similar approach, implementing their own WooCommerce Can PHP Scoper work with Composer autoloader? Would you recommend using PHP Scoper on a WordPress plugin? |
I ended up figuring it out. This approach is similar to Google Site Kit, generating a class map to avoid Composer doing The scoping process should happen as part of a build process of a distributable version of the plugin. not for development. Understand why here. (This approach uses the Optimization Level 1: Class map generation) This is not mean to be a copy-and-work solution. In this example I use Docker to have predictable paths, you might need to adjust those to your reality. I also use the Phar version of PHP-Scoper. Makefile:
composer.json (Triggers a Makefile to run inside the Docker container):
Loading the autoloader in the plugin (Inspired by Google Site Kit): $class_map = array_merge(
require_once __DIR__ . '/vendor/composer/autoload_classmap.php',
require_once __DIR__ . '/vendor_prefixed/composer/autoload_classmap.php'
);
spl_autoload_register(
function ($class) use ($class_map) {
if (isset($class_map[$class])) {
require_once $class_map[$class];
return true;
}
return null;
},
true,
true
);
Check the contents of those two files to see if everything looks right to you. |
Re-opening this as privately requested for visibility and further discussion around the subject. Maintainers of this package, please feel free to close this if you see fit. |
Since you mentioned Site Kit by Google, you might also want to check out how we've done the prefixing in another Google plugin here: https://github.com/google/web-stories-wp/. |
I just published how I scoped my WordPress plugin: https://graphql-api.com/blog/graphql-api-for-wp-is-now-scoped-thanks-to-php-scoper/ |
There is plenty of answers here so I'll be closing this issue. I am not satisfied with the WP support but I'll keep #303 for this purpose. |
Howdy!
I tried to use PHP-Scoper to prefix a WordPress plugin but couldn't. I saw the issue #303 and the PR #433, I've read through a fair amount of comments and tried to get hands down with it myself.
I will list the steps I took to scope my WordPress plugin
vendor
folder, and the results that I've got:Approach 1, per-dependency scope:
File: scoper-di52.php
Result: The prefixed di52 package in
vendor_prefixed/lucatume/di52
, as is expected. But there are a few issues with this approach:Approach 2, scope the entire
vendor
folder:File: scoper.php
Result: All
vendor
dependencies prefixed in thevendor_prefixed
folder, including Composer itself. This should work, but I had to add this to thescoper.php
file, which raises an eyebrown for other exceptions that I might be missing and find out it can cause a fatal down the road:Patching is somewhat expected, but the fact that I had to do it in Composer Autoloader worries me, as the autoloader seems fragile as it's auto-generated.
With approach 1, I have added this to my composer.json so it can find the prefixed classes:
I'm not sure if this would work well for attempt 2.
Am I taking the right approach to scope this WordPress plugin? Is there a third approach I should consider?
Thanks!
The text was updated successfully, but these errors were encountered: