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: about 1 month ago
JSON representation
Symfony bundle for https://github.com/highcoreorg/regitry
- Host: GitHub
- URL: https://github.com/highcoreorg/registry-bundle
- Owner: highcoreorg
- License: mit
- Created: 2024-03-03T13:09:59.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-09-17T15:46:15.000Z (about 1 year ago)
- Last Synced: 2025-08-10T16:46:55.346Z (3 months ago)
- Topics: registry, registry-pattern, symfony-bundle, symfony-registry
- Language: PHP
- Homepage:
- Size: 55.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
}
```