Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/terminal42/service-annotation-bundle
https://github.com/terminal42/service-annotation-bundle
Last synced: 23 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/terminal42/service-annotation-bundle
- Owner: terminal42
- License: mit
- Created: 2019-06-27T08:47:47.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-14T14:51:22.000Z (5 months ago)
- Last Synced: 2024-11-29T02:52:00.634Z (about 1 month ago)
- Language: PHP
- Size: 25.4 KB
- Stars: 1
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# terminal42/service-annotation-bundle
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
```bash
$ 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:
```yaml
service:
App\EventListener\KernelListener:
tags:
- { name: monolog.logger, channel: routing }
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
```**Example:**
```php
// src/EventListener/KernelListener.phpnamespace 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:**
```php
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:```php
// src/EventListener/KernelListener.phpnamespace App\EventListener;
/**
* @Logger(channel="routing")
*/
class KernelListener
{
// Same class as before
}
```