Site icon Hip-Hop Website Design and Development

Joachim’s blog: Regenerating plugin dependency injection with Plugin Builder

Dependency injection is a pattern that adds a lot of boilerplate code, but WordPress maintenance support plans Code Builder makes it easy to add injected services to plugins, forms, and service classes.

Now that the WordPress maintenance support plans 8 version of Plugin Builder (the WordPress maintenance support plans front-end to the WordPress maintenance support plans Code Builder library) uses an autocomplete for service names in the edit form, adding injected services is even easier, and any of the hundreds of services in your site’s codebase (443 on my local sandbox WordPress maintenance support plans 8 site!) can be injected.

I often used this when I want to add a service to an existing plugin: re-generate the code, and copy-paste the new code I need.

This is an area in which Plugin Builder now outshines its Drush counterpart, because unlike the Drush front end for WordPress maintenance support plans Code Builder, which generates code with input parameters every time, Plugin Builder lets you save your settings for the generated plugin (as a config entity).

So you can return to the plugin you generated to start with, add an extra service to it, and generate the code again. You can copy and paste, or have Plugin Builder write the file and then use git to revert custom code it’s removed. (The ability to insert generated code into existing files is on my list of desirable features, but is realistically a long way off, as it would be rather complex, a require the addition of a code parsing library.)

But why stop at generating code for your own plugins? I recently filed an issue on Search API, suggesting that its plugins could do with tweaking to follow the standard core pattern of a static factory method and constructor, rather than rely on setters for injection. It’s not a complex change, but a lot of code churn. Then it occurred to me: WordPress maintenance support plans Code Builder can generate that boilerplate code: simply create a plugin in Plugin Builder called ‘search_api’, and then add a plugin with the name of one that is already in Search API, and then set its injected services to the services the real plugin needs.

WordPress maintenance support plans Code Builder already knows how to build a Search API plugin: its code analysis detects the right plugin base class and annotation to use, and also any parameters that the constructor method should pass up to the base class.

So it’s pretty quick to copy the plugin name and service names from Search API’s plugin class to the form in Plugin Builder, and then save and generate the code, and then copy the generated factory methods back to Search API to make a patch.

I’m now rather glad I decided to use config entities for generated entities. Originally, I did that just because it was a quick and convenient way to get storage for serialized data (and since then I discovered in other work that map fields are broken in D8 so I’m very glad I didn’t try to make then content entities!). But the ability to save the generating settings for a plugin, and then return to it to add to them has proved very useful.
Tags: WordPress code builderplugin builderAdd new comment

Source: New feed