zicht / service-wrapper
Service Wrapper classes
Installs: 14 120
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
- dev-release/5.x
- dev-release/4.x
- 5.0.0
- 4.0.2
- 4.0.1
- 4.0.0
- 4.0.0-rc.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.3.6
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.1-rc.1
- 2.3.0
- 2.3.0-rc.1
- 2.1.1
- 2.1.0
- 2.1.0-rc.3
- 2.1.0-rc.2
- 2.1.0-rc.1
- 2.1.0-beta.1
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-beta.1
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.6.0-beta.1
- 1.5.0
- 1.5.0-beta.1
- 1.4.0
- 1.4.0-beta.4
- 1.4.0-beta.3
- 1.4.0-beta.2
- 1.4.0-beta.1
- 1.3.2
- 1.3.2-rc.3
- 1.3.2-rc.2
- 1.3.2-rc.1
- 1.3.1
- 1.3.1-rc.1
- 1.3.0
- 1.3.0-rc.1
- 1.3.0-beta.1
- 1.2.0
- 1.2.0-beta.1
- 1.1.0
- 1.1.0-beta.1
- 1.0.0
- 1.0.0-rc2
- 1.0.0-rc.1
- 1.0.0-beta.3
- 1.0.0-beta.2
- 1.0.0-beta.1
- dev-dependabot/github_actions/actions/checkout-4
- dev-release/3.x
- dev-release/2.x
- dev-feature/production-environment-test
This package is auto-updated.
Last update: 2024-10-30 01:32:56 UTC
README
Provides a wrapper to easily allow for an aspect-oriented approach of influencing response and requests to the service.
Scripts
- unit test:
composer test
- lint test:
composer lint
General approach
All calls to the service are wrapped in a call that notifies all observers of the call. The observers get a change to do their own housekeeping, or even alter the request or the response.
Any observer must implement the notifyBefore
, alterRequest
, alterResponse
and
notifyAfter
methods. Each observer will get an instance of a ServiceCall
object, which
contains the request and the response objects. This more or less works the same as an event
loop, but is intentionally not implemented as such to avoid the overhead of having a
dispatcher and listener structure in place.
Common observers
For two very common practices, a logger and a cache observer are available.
Example
class MyService
{
public function doIt($name)
{
return sprintf("Hi, %s, you have %d marbles in your pocket!", $name, rand());
}
}
class MyObserver implements ServiceObserverInterface
{
public function notifyBefore(ServiceCallInterface $call) {}
public function notifyAfter(ServiceCallInterface $call) {}
public function alterRequest(ServiceCallInterface $call) {}
public function alterResponse(ServiceCallInterface $call)
{
$call->setResponse(strrev($call->getResponse()->getResponse()));
}
}
$wrapper = new ServiceWrapper(new MyService());
$wrapper->registerObserver(new MyObserver());
echo $wrapper->doIt("Bart");
echo $wrapper->doIt("Lisa");
See doc/example.php for a more thorough example.
Applications
You can implement observer that:
- Add more data to responses, i.e. to enrich data
- Add basic logging
- Extensively monitor requests and responses with your own observers
- Add sanity checks and such that are costly in development, but can now easily be isolated in one object.
- Add caching
Maintainer
- Boudewijn Schoon boudewijn@zicht.nl
- Erik Trapman erik@zicht.nl