Download the PHP package waponix/pocket without Composer
On this page you can find all versions of the php package waponix/pocket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download waponix/pocket
More information about waponix/pocket
Files in waponix/pocket
Package pocket
Short Description Is a service container that considers all loadable classes inside a project as a service.
License GPL-3.0
Informations about the package pocket
pocket (v2.2.0)
Is basically a service container.
Pocket is currently being developed in PHP 8
, and will take advantage of it's new features as much as possible.
Installation
Introduction
Pocket considers all classes as service, you can pass any class name to pocket's get() method without doing any registration and it will try to load the object for you.
Note: you need to setup your own autoloading because pocket won't handle that for you, pocket also only works with PSR-4 autoloading
Example:
Pocket will also automatically load any arguments that are expected to be an instance of a class in the constructor.
Example:
In the example above, the argument $owner
will be injected with an instance of class Person. In any case that class Person also has any argument that is excpected to be class instance, those will automatically be injected as well.
But what about other argument types?
For other arguments, pocket cannot automatically inject its values and will throw an exception, but this problem can be solved by using Attribute.
Using the Service Attribute
Pocket will try to load any object as long as it's arguments are loadable or are explicitly defined. Arguments that expects instance of a class are considered explicit because they can be autoloaded, but for other arguments like string, integer, etc. we will need to find a way how to define them, we will need to use the Service Attribute (Waponix\Pocket\Attribute\Service
).
Attribute class is a new feature in PHP 8 which is a native way of adding Annotations in the code
Example:
Take note that to make this work we should make the key inside args:
match the variable name in the constructor arguments ($name and $url), this way we are telling pocket where to exactly get the values for them.
It is also possible to override the value for an argument that expects a class instance, as long as it is a child of the expected class (e.g class John instance of class Person)
Parameters
The previous example shows how to define the argument value the directly in the code, but it is also possible to have a dataset of parameters and pocket will take the value from there.
First we need to set the parameters:
Then we can target these values in the Service Attribute:
In the new example we are replacing the values with a parameter id indicated by @
, this will then tell pocket to get the values from the parameter based on the id
Notice that the id
@post.method
is equivalent to $parameters['post']['method']Invoke Methods
Pocket also allows to invoke class methods and automatically injects dependencies.
Let's say you have a controller class that has method that represents an action:
you can invoke this method by doing the example below:
Factory
If somehow your requirement needs to use factories for creating objects, then you can do this by defining a factory in the Service Attribute of the target class.
Example:
and your factory class would look something like:
Service Group
Another helpful tool is the ability to group services, these can be achieved by defining a tag value in the Service Attribute.
Example:
Now John is will be grouped under the tag person and can be loadable by accessing the tag name #person
:
Note: that the returned value will be an array or null if that tag is not used
You can also define multiple tags for a service by just passing an array of string into the tag
argument: