This is a wrapper bundle for the Symfony framework.
The main goal of this bundle is to provide a simple way to create a wrappers for the services.
Under the hood, this bundle performs code generation similar to how Doctrine does for entity proxying.
composer require pioniro/wrapper-bundleYou can see the example of usage in the example directory.
<?php
/**
* @Annotation
*/
class LogException implements \Pioniro\WrapperBundle\AnnotationInterface
{
}<?php
class LogExceptionHandler implements \Pioniro\WrapperBundle\HandlerInterface
{
public function __construct(private \Psr\Log\LoggerInterface $logger) {}
public function handle(callable $next, string $method, array $args, AnnotationInterface $annotation): callable
{
return function ($input) use ($next) {
try{
return $next($input);
} catch (\Throwable $exception) {
$this->logger->error($exception->getMessage(), compact('exception'));
throw $exception;
}
}
public static function handledClass(): string
{
return LogException::class;
}
}<?php
class MyService
{
#[LogException]
public function doSomethingWithPHP8(): void
{
throw new \Exception('Something went wrong');
}
/**
* @LogException
*/
protected function doSomethingWithPHP7(): void
{
throw new \Exception('Something went wrong');
}
}services:
App\Handler\LogException:
tags:
- { name: wrapper.handler }OR
services:
_instanceof:
Pioniro\WrapperBundle\HandlerInterface:
tags: ['wrapper.handler']And now, when you call doSomethingWithPHP8 or doSomethingWithPHP7 method, the exception will be logged.
You can create as many handlers as you want and use them in your services.
- Handlers are not called for private, static or final methods.
- Handlers are not called for methods of the final classes.
- Handlers are not called for methods of the classes that are not in the container.
- May occur strange errors if you use
statickeyword (for example in the Command) in the annotated hierarchy. - Using other annotations in the same class may lead to unexpected behavior (e.g., if you use
@Routeor@Templateannotations in the same class, these annotations may not work as expected)