Search code examples
shopwareshopware6

Adding a namespace to a shopware 6 plugin afterwards


If you add a namespace to the autoload configuration of composer.json and use this namespace in the plugin, an error is thrown because the namespace is not known.

This is due to the fact that Shopware stores the autoload configuration in the plugin database table, which of course is not updated when the file is changed.

Our current workaround is to disable the plugin in the database, then run the plugin:refresh command and re-enable the plugin. But you can't expect customers and especially store customers to do that.

Is there perhaps another solution to solve this without manual intervention?

Example of the autoload configuration change:

Before:

"autoload": {
    "psr-4": {
        "MainPluginNamespace\\": "src/"
    }
}

After:

"autoload": {
    "psr-4": {
        "MainPluginNamespace\\": "src/",
        "AdditionalPluginNamespace\\": "additional/"
    }
}

Solution

  • I feel like the composer.json should not be a subject to change at any time. Meaning you should only change it with updates of your plugins. This obviously still requires the user to actively update the plugin for example through the administration. And you'd also need to be aware that unless the user updated the plugin, dependencies to the added namespace might not be resolvable. The alternative would be to read the composer.json on every kernel boot, since you wouldn't know if the composer.json had changed, and possibly then update the plugin table.

    If you still want to follow up on that idea you could have a look at PluginService::refreshPlugins, replicate it specifically for your plugin and run it on boot. If you detect namespaces changes you might also have to reboot the kernel in the same process. This all seems like quite a bit of overhead however so I'd still recommend relying on the user having to update the plugin first.