In the last article on torque, we talked about the benefits of using containers in word press plug-ins or applications. I designed the container from the beginning to manage the three classes of Twitter: seed, account, and client. This week we’ll discuss very similar concepts, but we’ll cover in more detail how to configure plug-ins around containers. We will also use pimple as a container for excellent and fairly standard composer packages, rather than scrolling ourselves. View improvements: The
Class container{
\/**The
*Include tracked objects.
*The
*@Var array
*\/The
Protected $objects;
\/**The
*Receive twitter client
*The
*@return twitterapi
*\/The
Public function gettwitter API (): Twitter API
The{
If (! Isset ($this-> object [\u method\u]){
$this->objects[\u method\u]= new twitter api();
}The
Return $this->objects[\u method\u].
}The
\/**The
*Get twitter account using user name
*The
*@Param string $username
*The
*@Return twitter account
*\/The
Public function gettwitter account (string$username): Twitter account
The{
$arraykey=md5 (\u method_.$username);
If (! Isset ($this-> object [$arraykey]){
$this->objects[$arraykey] = twitteraccountfactory:: get ($username);
}The
$this-> return object [$arraykey];
}The
}This works, but adding new items to the container can be tedious. To make it easy to add new projects, I rejected the urge to add abstractions. Not only do I want to explain one at a time, but I know that pimple provides everything we need. It is a standard familiar to many PHP developers. The
If you have never used pipe before, it is a very simple dependency injection container. If plug-in shared class instances require a flexible, testable central repository, and the flexibility to mix a single instance with an object factory, many problems can be solved. At first, pimple has only one class to worry about. Pipe\container. You can instantiate or expand it to create a container. Let’s briefly introduce creating a container and adding a single \
Male factory (function ($c){
Return to the new client ();
}); This object implements arrayaccess. This is why you can use array syntax to access items. So you can now access instances of the twitter client:$ Client = $container[‘twitter.client’]; Artrayaccess is simple, but I personally like to extend the pipe and add get () and set () methods. Get() method calls offset get(), set() calls offset set(). However, I like having explicit get () and set (), which can easily redefine the arrayaccess implementation provided by pimple in subclasses without actual touch. The
Male offsetget ($id);
}The
\/**The
*Set item in container
*The
*@Param string $id
*@Param mix
$value
*\/The
Public function set (string $id, $value)
The{
Return $this->offsetset ($id, $value);
}The
}You can now set up and access items in the container using the new pivot subclass arrayaccess or a new method. The
In the previous article on the central container, in order to achieve the same goal as creating a function that maintains the default instance of the container, we looked at a better method than using monochrome mode in the container class. The following is a method to globally access the plug-in’s pipe container using the same method without forcing the container class to use monochrome mode: Male set (\
}The
}The
Now, the basic plug-in file only needs to perform the following three operations: Includes the autoloader for composer. Provides access to the default container instance. Call the plug-in class registration service (pimpleexample());
});
\/**The
*Container for importing plug-ins
*The
*@Return container
*\/The
Function pimpleexample(): container
The{
Static $container;
If (! $container){
$container= new container();
}The
Return $container.
}I use services again, but I find it confusing and difficult to use objects associated with arrays or objects similar to arrays. Technically, you can now add the following filters: The
Male filter (\
$content. = ‘ How do you do, Sean;
$content return;
}); I am not a fan of this for two reasons. The first is that there is little resistance to change. To change the items attached to the \
}The
\/**The
*Add filter through plug-in API service
*The
*@Param string $hook
*@param $function_to_add
*@Param int$priority
*@param int $acceptedargs
*The
*@return eventemitterinterface
*\/The
Public static function addfilter (string $hook, $function_to_add, int $priority = 10, int $acceptedargs = 1): eventemitterinterface
The{
Return self:: gethookmanager() – >filter ($hook, $function\u to\u add, $priority, $acceptedargs);
}The
\/**The
*@return eventemitterinterface
*\/The
Expose the static function gethookmanager(): eventemitterinterface
The{
Return to pimpleexample() – >get (\
}The
}You can then repackage the method of adding filters as follows: How do you do, Sean;
Return $content
Describes how to use. I showed the same thing in my last report, no acne. Personally, I am switching from the built-in scrolling that helps with the learning mode to using the needle. This is because it is better and more powerful than its own implementation. I often think about the microservice architecture pattern and the service provider pattern, and how they are similar but different in scale. The former connects applications and forms a modular application centered on the central application. The latter connects the rest of a single application around the central part of the application. This approach is very different from the usual WordPress approach. WordPress is simple because it doesn’t apply powerful encapsulation. Structuring plug-ins or applications around service containers helps make code more reusable, maintainable, and testable.