Looking to hire Laravel developers? Try LaraJobs

laravel-domain-repositories maintained by stemizer

Description
Convention-driven repository binding for domain-oriented Laravel architectures.
Author
Last update
2026/11/01 12:10 (dev-main)
License
Links
Downloads
49

Comments
comments powered by Disqus

laravel-domain-repositories

Automatic repository discovery and container binding for domain-driven Laravel applications.

This package eliminates manual container bindings for repository pairs. It scans app/Domains, applies a strict naming convention, and registers contracts to their concrete implementations automatically — with optional caching for production.

Installation

composer require stemizer/laravel-domain-repositories

Optionally publish the config:

php artisan vendor:publish --tag=domain-repositories-config

Naming Convention

The package expects the following structure for each domain:

Role Class
Contract App\Domains\{Domain}\RepositoryContract\{Domain}Repository
Concrete App\Domains\{Domain}\Repository\{Domain}{Driver}

Example with Foo domain and default Eloquent driver:

App\Domains\Foo\RepositoryContract\FooRepository   ← interface
App\Domains\Foo\Repository\FooEloquent              ← implementation

If a contract or concrete class does not exist, that domain is silently skipped.

Configuration

// config/domain_repositories.php

'default_driver'  => 'Eloquent',
'default_binding' => 'bind',   // or 'singleton'
'binding_options' => ['bind', 'singleton'],

'domains' => [
    'Foo' => ['driver' => 'Eloquent', 'binding' => 'bind'],
    'Bar' => ['driver' => 'Eloquent', 'binding' => 'singleton'],
],

Per-domain overrides take precedence over defaults.

Directory Scaffolding

RepositoryHelper::ensureDomainDirectories($domainPath) creates the standard domain subdirectories:

Console, Enums, Events, Exceptions, Helpers, Http, Jobs,
Listeners, Models, Observers, Providers, Services,
Repository, RepositoryContract

Caching

In local and testing, bindings are discovered on every application boot; no binding cache is written or read. In other environments, bindings are written to bootstrap/cache/domain_repository_binds.php and reused on subsequent boots.

After changing a domain, driver or binding override, clear the old bindings:

php artisan optimize:clear

This includes the package's domain-repositories:clear command. To clear only repository bindings (without clearing Laravel's configuration cache):

php artisan domain-repositories:clear

The next application boot outside local/testing regenerates the file from the effective configuration. If configuration is cached, run php artisan config:cache followed by php artisan domain-repositories:clear so the next boot uses the rebuilt configuration. Restart long-running workers to replace already-resolved repository instances.

Features

  • Automatic contract-to-concrete binding via directory scan
  • Config-driven driver and binding strategy per domain
  • Cache file generation for staging/production
  • Domain directory scaffolding helper

Out of Scope

This package handles repository registration only. Event listeners, service registrations, and other domain concerns are intentionally left out.

License

MIT