Download the PHP package joe.szeto/capsule without Composer
On this page you can find all versions of the php package joe.szeto/capsule. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package capsule
Install
Basic usage
we can use attribute to tell the code the following Closure is a setter, and the value of the setter is the return value of the closure
we can also use method to set the value of the setter
Auto resolve params
if the closure has type hints and the type hint is the same as the value of the setter, the value will be resolved automatically even if the params name is not 100% match, it will still work
we can resolve parameters by the application container leverages the underlying container's reflection capabilities. This means that when a closure or callable object is executed, the Capsule library can automatically inject the required dependencies as defined by the type hints of the parameters.
OnBlank
it will only call when the value of the setter is null
we can also use attribute to set and detect the OnBlank at the same time
we can also use method to set the OnBlank
Closure
if the set value is a closure, when the type hint of the param of using is Closure the original closure will be passed to the param
when the type hint of the params is a NOT closure, the value will be return
Evaluable
if the type is closure, and call it we have to pass the params manually
but if we pass the param one by one manually it will be very tedious now Evaluable become handy
Each
for each value, the closure will be called
Only
Skip
Mocking
The Capsule::mock method allows developers to replace parts of their application's behavior with predetermined responses or operations. This is particularly useful in testing, where you want to isolate the part of the application you are testing and control its interactions with external dependencies.
To replace a string value within the capsule, simply pass the key and the new string value to the mock method.
If you need to mock the behavior of a function, provide a closure as the second argument. This closure will be executed in place of the original function associated with the given key.
To mock an object, pass an instance of the class as the second argument. This instance will replace any existing instances bound to the specified key within the capsule.
Mock with sequence
Catch
Namespace
it can resolve params from namespace, capsules under same namespace can share the value
Append
append function will append the callable to the end of the capsule
use append combined with namespace
massage
when we want to change the value of some variable we will always do something like the following
so I come up with the idea of massage
or
but if we want to use this approach, we have to follow some conventions
=
and the must be the key of the data
for example
this is equivalent to