BrianHenryIE / strauss

Prefix PHP namespaces and classnames to allow multiple versions of libraries to exist without conflict.
https://brianhenryie.github.io/strauss/
MIT License
142 stars 23 forks source link

Troubles Namespacing WPGraphQL #66

Closed tn3rb closed 1 year ago

tn3rb commented 1 year ago

Attempting to solve depedency conflicts with https://github.com/wp-graphql/wp-graphql

composer.json


"require": {
    "wp-graphql/wp-graphql": "^1.12"
},
"extra": {
    "strauss": {
        "target_directory": "Dependencies",
        "namespace_prefix": "MyProject\\Dependencies\\",
        "classmap_prefix": "Prefix_",
        "constant_prefix": "Prefix_",
        "packages": [
            "wp-graphql/wp-graphql"
        ],
        "delete_vendor_packages": true,
        "delete_vendor_files": true
    }
},

results in a whole lot of weirdness with the WPGraphQL package ultimately not working.

The main WPGraphQL class is not namespaced, the top of the file DOES have the following commented out line added:

// Global. - namespace MyProject\Dependencies\WPGraphQL;

but the actual class declaration remains untouched:

final class WPGraphQL {

whereas the expectation was for the class to either be namespaced via the namespace keyword or via the classmap_prefix prefix specified in composer.json.

Either way, autoloading does not work and results in errors like:

[26-Apr-2023 23:08:28 UTC] 
PHP Fatal error:  Uncaught Error: 
Class "MyProject\Dependencies\WPGraphQL" not found 
in /path/to/project/Dependencies/wp-graphql/wp-graphql/src/Registry/Utils/PostObject.php:181

which appears to be expecting the WPGraphQL to be namespaced like MyProject\Dependencies\WPGraphQL

PLease note that the autoload file generated by straus is loaded during the very first few steps of my bootstrapping process:

require_once __DIR__ . '/Dependencies/autoload.php';

Anyways, not sure whether my problems are caused by a bug, or due to misconfiguration, or if there is something else i failed to do.

Thank you in advance for all of your time, effort, and talent you have commited to this project. I'll gladly help write unit tests if any fix is needed to get this working.

BrianHenryIE commented 1 year ago

Hey, I think I have this fixed: https://github.com/BrianHenryIE/strauss/commit/49c444ace94720da2e93b85755c5557ba9fff57b

Try the master branch and let me know: composer require brianhenryie/strauss:dev-master

BrianHenryIE commented 1 year ago

Reopening: the unit test passes but the integration test does not :(

BrianHenryIE commented 1 year ago

OK, I made further changes. 168 passing tests now! Let me know if it works for you. I get:

final class Prefix_WPGraphQL {
tn3rb commented 1 year ago

OH it's better...

the global scope class is now prefixed correctly:

final class EECD_WPGraphQL {

however, the prefix is not applied consistently throughout the rest of the codebase, so everything fatals.

For example, in /Dependencies/wp-graphql/wp-graphql/src/Registry/Utils/PostObject.php:

the import is incorrect

namespace MyProject\Dependencies\WPGraphQL\Registry\Utils;

use MyProject\Dependencies\WPGraphQL;

and in the class code, the following code on line 181 is wrong :

$allowed_taxonomies = WPGraphQL::get_allowed_taxonomies( 'objects' );

but then a little bit below that on line 203 it's correct :

$taxonomies = \EECD_WPGraphQL::get_allowed_taxonomies();

so weird, since the two lines of code are almost identical.

Looking at the source code repo, it appears that the inconsistency is in the code there: https://github.com/wp-graphql/wp-graphql/blob/develop/src/Registry/Utils/PostObject.php

line 175:

$allowed_taxonomies = WPGraphQL::get_allowed_taxonomies( 'objects' );

line 197

$taxonomies = \WPGraphQL::get_allowed_taxonomies();

so it appears that Strauss is getting tripped up when the globally scoped class is imported.

Oh, and most importantly, THANK YOU so much for taking the time to work on this, it is so greatly appreaciated.

BrianHenryIE commented 1 year ago

I finally got this done. It's in master but not in a release yet: https://github.com/BrianHenryIE/strauss/commit/cbd1617631bdb029f4660f2a9350dcfffb2b1db4

I replaced the use WPGraphQL; with use Prefix_WPGraphQL as WPGraphQL;, then by doing that before the namespace replacements, it wasn't inadvertently changed to use MyProject\Dependencies\WPGraphQL;. Inside the class, WPGraphQL and \Prefix_WPGraphQL are both valid.

Give it a try and let me know how it's working.