me-arif-dewan/laravel-repository-generator
最新稳定版本:v1.0.1
Composer 安装命令:
composer require me-arif-dewan/laravel-repository-generator
包简介
Introduces a new Artisan command to generate repository classes with support for: - Multiple interface implementation with automatic method stub generation. - Single class extension with validation. - Model dependency injection via constructor. - Automatic service provider binding registration. - Pi
关键字:
README 文档
README
A powerful, type-safe repository pattern generator for Laravel applications with advanced features including interface implementation, model injection, and automatic service provider binding.
Features
- Multiple Interface Implementation - Implement multiple interfaces with automatic method stub generation
- Class Extension - Extend base repository classes with validation
- Model Dependency Injection - Auto-inject Eloquent models via constructor
- Auto-Binding - Automatically register bindings in service providers
- Smart Type Resolution - Intelligent type hint resolution and use statement generation
- Pipeline Architecture - Clean, maintainable code using Laravel Pipeline
- Comprehensive Validation - Robust error handling with custom exceptions
- Fully Tested - 100% test coverage with Pest
Requirements
- PHP 8.1 or higher
- Laravel 10.x or 11.x
Installation
Install the package via Composer:
composer require me-arif-dewan/laravel-repository-generator --dev
Quick Start
Basic Repository
php artisan make:repository UserRepository
Generates:
<?php namespace App\Repositories; class UserRepository { }
With Model Injection
php artisan make:repository UserRepository --model=User
Generates:
<?php namespace App\Repositories; use App\Models\User; class UserRepository { /** * Create a new repository instance. */ public function __construct( protected User $model ) {} }
With Interface Implementation
php artisan make:repository UserRepository --interface=UserRepositoryInterface
Generates:
<?php namespace App\Repositories; use App\Contracts\UserRepositoryInterface; use Illuminate\Support\Collection; class UserRepository implements UserRepositoryInterface { public function find(int $id): ?User { // TODO: Implement find() method. } public function all(): Collection { // TODO: Implement all() method. } }
Complete Example
php artisan make:repository UserRepository \ --model=User \ --extends=Repositories/BaseRepository \ --interface=UserInterface,PaymentInterface \ --bind \ --force
Generates:
<?php namespace App\Repositories; use App\Models\User; use App\PaymentInterface; use App\Repositories\BaseRepository; use App\UserInterface; class UserRepository extends BaseRepository implements UserInterface, PaymentInterface { /** * Create a new repository instance. */ public function __construct( protected User $model ) {} public function find(int $id): ?User { // TODO: Implement find() method. } public function processPayment(array $data): bool { // TODO: Implement processPayment() method. } }
And automatically updates app/Providers/AppServiceProvider.php:
public function register(): void { $this->app->bind(UserInterface::class, UserRepository::class); $this->app->bind(PaymentInterface::class, UserRepository::class); }
Available Options
| Option | Short | Description |
|---|---|---|
--interface |
-i |
Implement interface(s), comma-separated |
--extends |
-e |
Extend parent class |
--model |
-m |
Inject model dependency |
--bind |
-b |
Auto-register bindings |
--force |
-f |
Overwrite existing file |
Advanced Usage
Multiple Interfaces
php artisan make:repository PaymentRepository \ --interface=PaymentInterface,LoggableInterface,AuditableInterface
Nested Namespaces
php artisan make:repository UserRepository \ --interface=Contracts/User/UserRepositoryInterface \ --extends=Domain/Repositories/BaseRepository
Custom Stub
Publish the stub for customization:
php artisan vendor:publish --tag="laravel-repository-generator-stubs"
Edit stubs/repository.stub to match your preferences.
Testing
# Run tests composer test # Run tests with coverage composer test-coverage # Run static analysis composer analyse # Fix code style composer format
Error Handling
The package provides comprehensive error messages:
# Multiple inheritance php artisan make:repository UserRepository --extends=Base,Another # Error: PHP does not support multiple inheritance. You can only extend one class. # Non-existent interface php artisan make:repository UserRepository --interface=NonExistent # Error: Interface [App\NonExistent] does not exist. # Extending an interface php artisan make:repository UserRepository --extends=UserInterface # Error: [App\UserInterface] is an interface. Use --interface instead of --extends.
Architecture
Clean Architecture
src/
├── Commands/ → Artisan commands
├── Services/ → Business logic
├── Support/ → Utility classes
├── Contracts/ → Interfaces
├── Exceptions/ → Custom exceptions
├── Enums/ → Enumerations
└── Concerns/ → Reusable traits
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security-related issues, please email cseknowledge@gmail.com instead of using the issue tracker.
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.
Credits
Acknowledgments
Built with Spatie's Package Skeleton
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-26
