dev-with-ioc
dev-with-iocCreate WP Plugins without creating conflicts with other third party libs
MIT
The Development Requires
by alpipego
Create WP Plugins without creating conflicts with other third party libs
WP Plugin Boilerplate for building plugins with composer and keeping third-party dependencies in repository, (*1)
Yes! This one aims at preventing conflicts caused by third-party libraries by renaming their namespaces. Compared to other boilerplates out there this one does not provide you with WordPress-specific tools or reusable code., (*2)
Grunt Composer No Conflict Namespaces, (*3)
Create a new plugin by running composer create-project alpipego/gcnsnc ./PLUGIN_NAME
., (*4)
You can then update the composer.json
file with your plugin details (or remove it altogether)., (*5)
Install node packages by running npm install
(or yarn or whatever you want to use)., (*6)
Update Gruntfile.js
with your plugins namespace, i.e. replace MyNamespace\\MySubNamespace
with your namespace (and subnamespace). By default all third-party code will be copied to src/Common
and the namespace will be prefixed with the values in the namespaces
object., (*7)
namespace GuzzleHttp;
will be replaced with, (*8)
namespace MyNamespace\MySubNamespace\Common\GuzzleHttp;
Besides adding your required packages to composer, you will also need to add them to the task in grunt/copy.js
. (Note the usage of the namespaces.thirdParty
variable)., (*9)
The simplest form is a JavaScript object with src
and dest
properties:, (*10)
{ src: 'vendor/composer/ClassLoader.php', dest: 'src/<% namespaces.thirdParty %>/Composer/Autoload/ClassLoader.php' }
This will copy a single .php
file to the src directory. Copying all contents from a directory is as simple:, (*11)
{ expand: true, cwd: 'vendor/psr/container/src', src: '**', dest: 'src/<% namespaces.thirdParty %>/Psr/Container' }
If you want to copy some files but not others this can be achieved as well:, (*12)
{ expand: true, cwd: 'vendor/pimple/pimple/src/Pimple', src: ['**', '!**/Tests/**'], dest: 'src/<% namespaces.thirdParty %>/Pimple' }
Read more about grunt globbing here., (*13)
Instead of including composers autoload.php
use the ClassLoader
class it provides. Add this at the top of your plugin (or in a bootstrap file):, (*14)
use MyNamespace\MySubNamespace\Common\Composer\Autoload\ClassLoader; require_once __DIR__ . '/src/Common/Composer/Autoload/ClassLoader.php'; $loader = new ClassLoader(); $loader->setPsr4('MyNamespace\\MySubNamespace\\', realpath(__DIR__ . '/src/')); $loader->register();
You will have to change the namespace and directories to fit your setup, find the documentation for the ClassLoader
here., (*15)
Running it is as simple as grunt thirdParty
after packages have been installed. This could also go into a composer script, e.g., post-update-cmd
., (*16)
Create WP Plugins without creating conflicts with other third party libs
MIT