Skip to content
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

Request: prefix global functions. #74

Open
TussendoorHQ opened this issue Oct 10, 2023 · 2 comments
Open

Request: prefix global functions. #74

TussendoorHQ opened this issue Oct 10, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@TussendoorHQ
Copy link

The strauss package is working perfectly for prefixing classes and constants. So far it helps us avoiding dependency hell within our custom WordPress plugin and we prevented a lot of compatibility issues with other plugins.

But I'm running into an issue. The illuminate\collections is being used and this package loads global functions in the helpers.php. Which is checking if a function exist like in this example:

if (! function_exists('collect')) {
    /**
     * Create a collection from the given value.
     *
     * @param  mixed  $value
     * @return \Custom\Prefix\Illuminate\Support\Collection
     */
    function collect($value = null)
    {
        return new Collection($value);
    }
} 

But when a different plugin is using the same package and this plugin is loaded first; we end up in the global function of that plugin. Which is not using my custom prefixed package and which is not using the same version as we do in our plugin.

This is resulting in this error:

#0 /home/user/public_html/wp-content/plugins/reviewx/vendor/jouleslabs/warehouse/src/Supports/common.php(58): explode()
#1 /home/user/public_html/wp-content/plugins/reviewx/vendor/jouleslabs/warehouse/src/Supports/common.php(50): array_get()
#2 /home/user/public_html/wp-content/plugins/our-custom-plugin/vendor/illuminate/collections/Arr.php(459): data_get()
#3 /home/user/public_html/wp-content/plugins/our-custom-plugin/vendor/illuminate/collections/Collection.php(688): Custom\Prefix\Illuminate\Support\Arr::pluck()

This is my composer.json:

{
    "name": "custom/plugin",
    "license": "GPL-2.0-or-later",
    "description": "WordPress plugin developed by Tussendoor.",
    "type": "wordpress-plugin",
    "require": {
        "php": "^8.0",
        "nesbot/carbon": "^2.66.0",
        "illuminate/database": "^8.0.0",
        "twbs/bootstrap": "^5.2.3",
        "fortawesome/font-awesome": "^6.4.0",
        "adbario/php-dot-notation": "^3.3.0",
        "woocommerce/action-scheduler": "^3.4.0",
        "yahnis-elsts/plugin-update-checker": "^4.13"
    },
    "autoload": {
        "psr-4": {
            "Custom\\Namespace\\": "app/"
        },
        "files": [
            "vendor/woocommerce/action-scheduler/action-scheduler.php"
        ],
        "classmap": [
            "vendor"
        ]
    },
    "config": {
        "optimize-autoloader": true,
        "sort-packages": true,
        "platform": {
            "php": "8.0.3"
        }
    },
    "scripts": {
        "pre-update-cmd": [
            "sh strauss-observer.sh"
        ],
        "post-install-cmd": [
            "sh strauss-prefixer.sh"
        ],
        "post-update-cmd": [
            "sh strauss-prefixer.sh"
        ]
    },
    "extra": {
        "strauss": {
            "target_directory": "vendor",
            "namespace_prefix": "Custom\\Prefix\\",
            "constant_prefix": "TSD_CUSTOM_PREFIX_",
            "include_modified_date": false,
            "include_author": false,
            "delete_vendor_packages": true,
            "exclude_from_prefix": {
                "packages": [
                    "yahnis-elsts/plugin-update-checker",
                    "woocommerce/action-scheduler"
                ]
            },
            "override_autoload": {
                "nesbot/carbon": {
                    "autoload": {
                        "psr-4": {
                            "Carbon\\": "src/Carbon/"
                        }
                    },
                    "classmap": [
                        "lazy"
                    ]
                }
            }
        }
    }
}

I'm using the strauss.phar at the moment. Is there a way to support prefixing global functions?

@BrianHenryIE BrianHenryIE added the enhancement New feature or request label Nov 16, 2023
@korridor
Copy link

korridor commented Dec 8, 2023

We have a similar problem with the template engine Twig. They declare global functions, with a twig_ prefix. The issue here is a bit different, since Twig doesn't do function_exists checks before defining global functions.

An example in the Twig codebase twig_cyle : https://github.com/twigphp/Twig/blob/aeeec9a5e907a79e50a6bb78979154599401726e/src/Extension/CoreExtension.php#L328C16-L328C16

We resolved the issue for now with a global search & replace in the code base that prefixes the functions (twig_cycle --> some_prefix_twig_cycle), but it would be great to have a better solution included in this package.

@BrianHenryIE
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants