An open API service indexing awesome lists of open source software.

https://github.com/highcoreorg/registry-bundle

Symfony bundle for https://github.com/highcoreorg/regitry
https://github.com/highcoreorg/registry-bundle

registry registry-pattern symfony-bundle symfony-registry

Last synced: 3 months ago
JSON representation

Symfony bundle for https://github.com/highcoreorg/regitry

Awesome Lists containing this project

README

        

# Symfony Registry Bundle

**RegistryBundle** is a Symfony bundle that provides a convenient mechanism for working with registries.
This package allows you to automatically register services with specific attributes and interfaces in registries.

## Installation
To install this package, use Composer:
```bash
composer require highcore/registry-bundle
```

## Configuration
After installation, add RegistryBundle to your Symfony configuration file (config/bundles.php):
```php
return [
// ...
Highcore\Bundle\RegistryBundle\RegistryBundle::class => ['all' => true],
];
```

## Usage
### Registering Registries
Registries are registered in the bundle class using a Compiler Pass. This allows services marked with attributes to be automatically registered in the appropriate registries during the container compilation phase.

Example of Registering a Registry
```php
addCompilerPass(new ServiceAttributeRegistryPass(
definitionId: 'some.your.project.namespace.first.resource.registry',
definitionClass: IdentityServiceRegistry::class,
targetClassAttribute: \App\YourBundle\Attribute\AsYourResourceAttribute::class, // your attribute class
interface: \App\YourBundle\YourServiceInterface::class, // your interface class (interface is optional, if passed, CompilerPass will check your service for an implementation of that interface)
));

$container->addCompilerPass(new ServiceAttributeRegistryPass(
definitionId: 'some.your.project.namespace.second.resource.registry',
definitionClass: ServiceRegistry::class,
targetClassAttribute: \App\YourBundle\Attribute\AsYourSecondResourceAttribute::class,
// register registry without interface
));
}
}
```

In this example, two registries are registered using CompilerPass:
### Registry for your first resource:
Registers all services marked with the \App\AsYourResourceAttribute attribute, and each service must implement the \App\YourServiceInterface interface.
1. **Example Interface for your first registry:**
```php
identifier;
}

public function hasIdentifier(): bool
{
return null !== $this->identifier;
}
}
```

### Registry for your second resource
Registers all services marked with the \App\AsYourSecondResourceAttribute attribute.

**For example, for second registry we will create only attribute**
```php
services();
$defaults = $services->defaults();
$defaults->autowire();
$defaults->autoconfigure();

// that's all you need to register your service in the registry
$services->set(\App\YourBundle\Service\MyService::class);
};
```

## Using the registry
To get started, register your service and pass registry "some.your.project.namespace.first.resource.registry" to arguments
Take the ServiceRegistry service identifier from the definitionId used earlier in \App\YourBundle::build()
```php
# src/YourBundle/Resources/config/services.php
set(\App\YourBundle\Service\SomeYourServiceUsingRegistry::class)
->args([service('some.your.project.namespace.first.resource.registry')]);
};
```

Declare your service
```php
registry->all();

// Use the services
}
```