propaysystems / laravel-base-repositories
Base elequent repositories with interface for common queries.
Requires
- php: ^8.0|^8.1|^8.2|^8.3
- illuminate/contracts: ^11.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^8.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^9.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.2
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-11-05 13:06:32 UTC
README
Base elequent repositories with interfaces for common queries.
UNDER DEVELOPMENT AND TESTING - WE WILL ADD PROPER DOCUMENTATION WITH v1 STABLE RELEASE
This is the base classes & helper commands for the repository pattern we use. In your own repository classes you have to extend the BaseRepository and implement your custom interface. In your custom interface you have to extend the BaseRepositoryInterface. This will give you a collection of commonly used queries and function that you don't need to duplicate each time.
class AddressRepository extends BaseRepository implements AddressRepositoryInterface
interface AddressRepositoryInterface extends BaseRepositoryInterface
Requirements
PHP 8.0+
Laravel 8+
Version Compatibility
Installation
You can install the package via composer:
composer require propaysystems/laravel-base-repositories
You can publish the config file with:
php artisan vendor:publish --tag="laravel-base-repositories-config"
Configuration
We create the service and repositories in the app folder of laravel to make use of laravels auto loading functionality.
Service
The default path for createing services will be in the App\Services
folder. If the folder does not exist it will be created.
If you want to overwrite default path for the service you can add a ENV
variable and specify your custom path.
BASE_SERVICE_PATH="App\MyServiceFolder"
The base name that will be appended to the class that is created by default, it will append Service
. So if you create a User -s
service the file that gets created will be UserService
APPEND_SERVICE_NAME="Service"
Repository
The default path for createing repositories will be in the App\Repositories
folder. If the folder does not exist it will be created.
If you want to overwrite default path for the repository you can add a ENV
variable and specify your custom path.
BASE_REPOSITORY_PATH="App\MyRepositoryFolder"
The base name that will be appended to the class that is created by default, it will append Repository
. So if you create a User -r
service the file that gets created will be UserRepository
& UserRepositoryInterface
APPEND_REPOSITORY_NAME="Repository"
Usage
Creating a Service
The service class is where all the domain logic will be created. This makes it easier to reuse business logic in the controllers, API, commands etc
- Creating a service class:
php artisan base:create Users/User --service or php artisan base:create Users/User -s
Running this command will create a new service class in the App\Services\
folders.
The rule is to split every service into its own related folders so the above command will actually create a file in:
App\Services\Users\UserService.php
Injecting a Repository
❕ Remember to dependancy inject your repositories you want to use into the
__construct
method of your service like the example below.
PHP 8.1 & above using constructor promotion public function __construct( protected UserRepositoryInterface $userRepository, ) {} or PHP 8.0 & below protected UserRepositoryInterface $userRepository; public function __construct( UserRepositoryInterface $userRepository, ) { $this->userRepository = $userRepository; }
Creating a Repository
- Creating a repository class:
php artisan base:create Users/User --repository --model=App/Models/Users/User or php artisan base:create Users/User -r --model=App/Models/Users/User
Running this command will create a repository class with its related interface in the App\Repositories
& App\Repository\UserRepository\Interfaces
folder.
Specifying the model with auto link the relevant model to the repository. In this case the User model will be added to the construct methof of the repository.
Same as the service class the rule is to split each repository into its own related folders so the above command will create files in:
App\Repositories\Users\UserRepositories.php
App\Repositories\Users\Interfaces\UserRepositoriesInterface.php
This will also try create and register the class and interface in laravel.
❕ These command flags can be combined
-s -r
so that the related service and repository classes be created in one command.
First Repository
After creating your first repository a RepositoryServiceProvider.php
file will automatically be created in your App\Providers
folder. You will need to add
the repository provider file to your config/app.php
under the providers
section. This will tell laravel that you are adding repositories and linking interfaces
to them and they be autoloaded.
... /* * Package Service Providers... */ App\Providers\RepositoryServiceProvider::class, ...
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.