定制 pioniro/wrapper-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pioniro/wrapper-bundle

最新稳定版本:1.1.1

Composer 安装命令:

composer require pioniro/wrapper-bundle

包简介

Wraps your symfony services

README 文档

README

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.

Installation

composer require pioniro/wrapper-bundle

Usage

You can see the example of usage in the example directory.

Create an annotation

<?php

/**
* @Annotation
 */
class LogException implements \Pioniro\WrapperBundle\AnnotationInterface
{
}

Create a handler

<?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;
    }
}

Annotate a service

<?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');
    }
}

Register the handler

services:
    App\Handler\LogException:
        tags:
            - { name: wrapper.handler }

OR

services:
    _instanceof:
        Pioniro\WrapperBundle\HandlerInterface:
            tags: ['wrapper.handler']

Enjoy

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.

Limitations

  • 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 static keyword (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 @Route or @Template annotations in the same class, these annotations may not work as expected)

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-14