I'd like to create a generic logging decorator that is type safe.
I have a number of repositories (interfaces), and need a decorator for each that catches the exceptions they might throw, passes those to an instance of LoggerInterface and then rethrows them. It's possible to create a dedicated decorator and test for each, though this is quite cumbersome (especially doing the test nicely), and something I rather avoid.
Using __call
to create a more generic decorator is the first approach that comes to mind. This however results in an object instance that does not implement the repository interface it decorates. That is a no go in my project. Is there some way to tell PHP that it does implement this interface, such as by use of some magic reflection?
I've read "how to implement a decorator in PHP?" and "Best way to implement a decorator pattern for method result caching in PHP" here on stackoverflow, both which outline the dedicated and the generic approach, though neither offer an indication of doing the generic approach in a type safe manner. Some time has passed since those questions where posted, so perhaps things have changed. I'm using PHP 7 and can use PHP 7.1 if needed.
Aucun commentaire:
Enregistrer un commentaire