terminal42/service-annotation-bundle
Composer 安装命令:
composer require terminal42/service-annotation-bundle
包简介
Add service tags from class annotations
README 文档
README
This bundle allows to add tags to container services using annotations. Similar to services subscribers for events, this allows the class to contain all necessary information within the same file.
This is most helpful if you use autowiring and autoconfiguration in your service definitions, but it works without it (e.g. for bundles) nonetheless.
Installation
$ composer.phar require terminal42/service-annotation-bundle ^1.0
Afterwards, make sure to enable the
Terminal42\ServiceAnnotation\Terminal42ServiceAnnotationBundle in your
kernel.
Configuration
The bundle currently does not provide any service configuration.
How to use
Annotations can be used on any service which is registered in the container.
The example annotations below equal to the following tags:
service: App\EventListener\KernelListener: tags: - { name: monolog.logger, channel: routing } - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Example:
// src/EventListener/KernelListener.php namespace App\EventListener; use Psr\Log\LoggerInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Terminal42\ServiceAnnotationBundle\Annotation\ServiceTag; /** * @ServiceTag("monolog.logger", channel="routing") */ class KernelListener { private $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * @ServiceTag("kernel.event_listener", event="kernel.request") */ public function onKernelRequest(GetResponseEvent $event) { $this->logger->debug('Request for '.$event->getRequest()->getRequestUri()); } }
If an annotation is added to a method instead of the class, the method name is automatically added to the service tag "method" argument.
Extending the annotations
If your bundle provides new tags to other services, you can improve DX by providing your own annotations. Good IDEs like PhpStorm can then provide autocomplete support.
Example:
use Doctrine\Common\Annotations\Annotation\Attribute; use Doctrine\Common\Annotations\Annotation\Attributes; use Doctrine\Common\Annotations\Annotation\Target; use Terminal42\ServiceAnnotationBundle\Annotation\ServiceTagInterface; /** * @Annotation * @Target("CLASS") * @Attributes({ * @Attribute("channel", type = "string", required = true), * }) */ class Logger implements ServiceTagInterface { public $channel; public function getName(): string { return 'monolog.logger'; } public function getAttributes(): array { return ['channel' => $this->channel]; } }
Applying this to the example above, the class annotation can be simplified like this:
// src/EventListener/KernelListener.php namespace App\EventListener; /** * @Logger(channel="routing") */ class KernelListener { // Same class as before }
terminal42/service-annotation-bundle 适用场景与选型建议
terminal42/service-annotation-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.46M 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 07 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 terminal42/service-annotation-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 terminal42/service-annotation-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.46M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 9
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-02