Symfony Integration
Import the package configuration to register the manager, data provider, default repository factory, criteria evaluator, source resolvers, file readers and translation resource provider. Symfony DependencyInjection, Config and YAML must be installed by the consuming application.
Import and Configure
imports:
- { resource: '../vendor/derafu/repository/resources/config/repository-services.yaml' }
parameters:
derafu.repository.sources:
App\Entity\Country: '%kernel.project_dir%/resources/data/countries.php'
App\Contract\DocumentTypeInterface: '%kernel.project_dir%/resources/data/document_types.php'
derafu.repository.data_provider.config:
normalization:
idAttribute: codigo
nameAttribute: glosa
derafu.repository.repository_factory.config:
entityClass: Derafu\Repository\Entity
repositoryClass: Derafu\Repository\Repository
All three parameters default to empty maps. The provider defaults to id and name normalization attributes; the factory defaults to Entity and Repository. Configure only the values your application needs. Inject RepositoryManagerInterface into application services and call getRepository() with a source identifier. The interface identifier convention is described in entities and mapping.
Accumulate Sources from Independent Packages
A parameter map is convenient for one application’s sources. When several packages contribute sources, each package declares a tagged RepositorySourceProviderInterface implementation. Their maps are accumulated, so an extension package does not need to copy its dependency’s entries.
namespace App\Repository;
use Derafu\Repository\Contract\RepositorySourceProviderInterface;
final readonly class CoreRepositorySourceProvider implements RepositorySourceProviderInterface
{
public function __construct(private string $dataDirectory)
{
}
public function getSources(): iterable
{
return [
\App\Entity\Country::class => $this->dataDirectory . '/countries.php',
'statuses' => 'enum:App\\Enum\\Status',
];
}
}
services:
App\Repository\CoreRepositorySourceProvider:
arguments:
$dataDirectory: '%kernel.project_dir%/resources/data/repository'
tags: ['derafu_repository.source_provider']
Pro\Repository\ProRepositorySourceProvider:
arguments:
$dataDirectory: '%pro.project_dir%/resources/data/repository'
tags: ['derafu_repository.source_provider']
ProRepositorySourceProvider implements the same contract and returns only Pro’s entries; %pro.project_dir% is supplied by that package or application. The built-in ArrayRepositorySourceProvider contributes derafu.repository.sources through the same tag. Duplicate identifiers across providers, including that parameter map, raise a translatable DataProviderException; providers do not silently override each other.
Values may be file paths, enum: strings, other strings handled by a registered resolver, or DataSourceInterface objects injected into the provider. Providers declare origins without reading them. A data source reads a complete dataset; use a repository factory for queries executed directly against a database or remote service.
Add a Repository Factory
services:
App\Repository\ProductRepositoryFactory:
arguments:
$connection: '@app.database_connection'
tags:
- { name: derafu_repository.repository_factory, repository: 'App\Entity\Product' }
The factory implements RepositoryFactoryInterface. Its tag’s repository attribute selects the identifier passed to getRepository(). Add several tags to register the same factory for several identifiers. An explicitly tagged factory takes precedence over the default data-provider factory, even if a source with the same identifier exists. The manager reuses each successfully created repository.
Extend Resolvers and File Readers
services:
App\Repository\ApiSourceResolver:
arguments:
$client: '@app.api_client'
tags:
- { name: derafu_repository.source_resolver, priority: 10 }
App\Repository\CsvFileFormatReader:
tags: ['derafu_repository.file_format_reader']
Resolvers implement DataSourceResolverInterface; readers implement FileFormatReaderInterface. Higher tag priorities run first. The built-in enum resolver has priority 100; the catch-all file resolver has priority -1000. An ordinary custom resolver therefore runs before the file fallback. Readers select the first service that supports the extension; their tag also supports priority.
Criteria Evaluator and Cache
The default factory receives CriteriaEvaluatorInterface from the container and passes it to each in-memory repository. A deliberate application alias or decorator can customize that dependency. Runtime repositories and entities are constructed on demand, rather than registered as shared services.
The data provider’s optional PSR-16 cache defaults to null. To enable it, override its $cache argument with the application’s cache service while retaining the remaining package wiring.
Translated Exception Messages
The import tags RepositoryTranslationResourceProvider as derafu_translation.resource_provider. The application’s translator must consume that collection, as described in the translation documentation. For example, when the application provides its own translator:
services:
app.translator:
class: Symfony\Component\Translation\Translator
factory: ['Derafu\Translation\TranslatorFactory', 'create']
arguments:
$defaultLocale: es
$resourceProviders: !tagged_iterator derafu_translation.resource_provider
Without a service container, provide the resource provider directly:
use Derafu\Repository\Translation\RepositoryTranslationResourceProvider;
use Derafu\Translation\TranslatorFactory;
$translator = TranslatorFactory::create(
'es',
resourceProviders: [new RepositoryTranslationResourceProvider()],
);
Package exceptions expose trans($translator). Their original messages are in English, and the package includes a Spanish catalogue.