├── .gitignore ├── config └── laravel-sarp.php ├── stubs ├── service-interface.stub ├── repository-interface.stub ├── service.stub └── repository.stub ├── composer.lock ├── README.md ├── composer.json ├── LICENSE.md └── src ├── Service ├── ServiceInterface.php └── ServiceImplementation.php ├── Repository ├── RepositoryInterface.php └── RepositoryModel.php ├── Commands ├── MakeServiceInterface.php ├── MakeRepositoryInterface.php ├── MakeService.php └── MakeRepository.php └── Providers └── SarpServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor -------------------------------------------------------------------------------- /config/laravel-sarp.php: -------------------------------------------------------------------------------- 1 | true, 10 | ]; 11 | -------------------------------------------------------------------------------- /stubs/service-interface.stub: -------------------------------------------------------------------------------- 1 | repository = $repository; 24 | } 25 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Sarp 2 | 3 | ### Installation 4 | 5 | - Require the Laravel Sarp with composer. 6 | 7 | ``` 8 | composer require rakhiazfa/laravel-sarp 9 | ``` 10 | 11 | - Publish package configuration ( important ). 12 | 13 | ``` 14 | php artisan vendor:publish --provider="Rakhiazfa\LaravelSarp\Providers\SarpServiceProvider" --tag="config" 15 | ``` 16 | 17 | ### Usage 18 | 19 | - Create a new repository. 20 | 21 | ``` 22 | php artisan make:repository UserRepository 23 | ``` 24 | 25 | 26 | - Create a new service. 27 | 28 | ``` 29 | php artisan make:service UserService 30 | ``` 31 | 32 | ### License 33 | 34 | The MIT License (MIT). Please see [License File](LICENSE.md) for more info 35 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rakhiazfa/laravel-sarp", 3 | "description": "Helps you in creating services and repository in laravel.", 4 | "homepage": "https://github.com/rakhiazfa/laravel-sarp", 5 | "license": "MIT", 6 | "autoload": { 7 | "psr-4": { 8 | "Rakhiazfa\\LaravelSarp\\": "src/" 9 | } 10 | }, 11 | "authors": [ 12 | { 13 | "name": "Rakhi Azfa Rifansya", 14 | "email": "rakhiazfa0421@gmail.com" 15 | } 16 | ], 17 | "require": {}, 18 | "extra": { 19 | "laravel": { 20 | "providers": [ 21 | "Rakhiazfa\\LaravelSarp\\Providers\\SarpServiceProvider" 22 | ] 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stubs/repository.stub: -------------------------------------------------------------------------------- 1 | model = $model; 24 | } 25 | 26 | /** 27 | * @param array $attributes 28 | * 29 | * @return Model 30 | */ 31 | public function new(array $attributes = []): $MODEL$ 32 | { 33 | return new $MODEL$($attributes); 34 | } 35 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright 2023 (c) Rakhi Azfa Rifansya 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /src/Service/ServiceInterface.php: -------------------------------------------------------------------------------- 1 | getNameInput()); 49 | 50 | return $rootNamespace . '\Services\\' . $model; 51 | } 52 | 53 | /** 54 | * Replace the namespace for the given stub. 55 | * 56 | * @param string $stub 57 | * @param string $name 58 | * @return $this 59 | */ 60 | protected function replaceNamespace(&$stub, $name) 61 | { 62 | $classname = Str::replace('Interface', '', $this->getNameInput()); 63 | $model = Str::replace('Service', '', $this->getNameInput()); 64 | 65 | $stub = Str::replace('$NAMESPACE$', 'App\Services\\' . $model, $stub); 66 | 67 | $stub = Str::replace('$CLASSNAME$', $classname, $stub); 68 | 69 | return parent::replaceNamespace($stub, $name); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Commands/MakeRepositoryInterface.php: -------------------------------------------------------------------------------- 1 | getNameInput()); 49 | 50 | return $rootNamespace . '\Repositories\\' . $model; 51 | } 52 | 53 | /** 54 | * Replace the namespace for the given stub. 55 | * 56 | * @param string $stub 57 | * @param string $name 58 | * @return $this 59 | */ 60 | protected function replaceNamespace(&$stub, $name) 61 | { 62 | $classname = Str::replace('Interface', '', $this->getNameInput()); 63 | $model = Str::replace('Repository', '', $this->getNameInput()); 64 | 65 | $stub = Str::replace('$NAMESPACE$', 'App\Repositories\\' . $model, $stub); 66 | 67 | $stub = Str::replace('$CLASSNAME$', $classname, $stub); 68 | 69 | return parent::replaceNamespace($stub, $name); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Commands/MakeService.php: -------------------------------------------------------------------------------- 1 | input->setArgument( 37 | 'name', 38 | str_replace('Service', '', $this->argument('name')) . 'Service' 39 | ); 40 | 41 | $this->call('make:service-interface', ['name' => $this->getNameInput()]); 42 | 43 | $this->input->setArgument('name', $this->argument('name') . 'Implementation'); 44 | 45 | return parent::handle(); 46 | } 47 | 48 | /** 49 | * Get the default namespace for the class. 50 | * 51 | * @param string $rootNamespace 52 | * @return string 53 | */ 54 | protected function getDefaultNamespace($rootNamespace) 55 | { 56 | $model = Str::replace('ServiceImplementation', '', $this->getNameInput()); 57 | 58 | return $rootNamespace . '\Services\\' . $model; 59 | } 60 | 61 | /** 62 | * Replace the namespace for the given stub. 63 | * 64 | * @param string $stub 65 | * @param string $name 66 | * @return $this 67 | */ 68 | protected function replaceNamespace(&$stub, $name) 69 | { 70 | $model = Str::replace('ServiceImplementation', '', $this->getNameInput()); 71 | 72 | $classname = $model . 'Service'; 73 | 74 | $stub = Str::replace('$NAMESPACE$', 'App\Services\\' . $model, $stub); 75 | 76 | $stub = Str::replace('$CLASSNAME$', $classname, $stub); 77 | 78 | $stub = Str::replace('$MODEL$', $model, $stub); 79 | 80 | return parent::replaceNamespace($stub, $name); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/Commands/MakeRepository.php: -------------------------------------------------------------------------------- 1 | input->setArgument( 37 | 'name', 38 | str_replace('Repository', '', $this->argument('name')) . 'Repository' 39 | ); 40 | 41 | $this->call('make:repository-interface', ['name' => $this->getNameInput()]); 42 | 43 | $this->input->setArgument('name', $this->argument('name') . 'Model'); 44 | 45 | return parent::handle(); 46 | } 47 | 48 | /** 49 | * Get the default namespace for the class. 50 | * 51 | * @param string $rootNamespace 52 | * @return string 53 | */ 54 | protected function getDefaultNamespace($rootNamespace) 55 | { 56 | $model = Str::replace('RepositoryModel', '', $this->getNameInput()); 57 | 58 | return $rootNamespace . '\Repositories\\' . $model; 59 | } 60 | 61 | /** 62 | * Replace the namespace for the given stub. 63 | * 64 | * @param string $stub 65 | * @param string $name 66 | * @return $this 67 | */ 68 | protected function replaceNamespace(&$stub, $name) 69 | { 70 | $model = Str::replace('RepositoryModel', '', $this->getNameInput()); 71 | 72 | $classname = $model . 'Repository'; 73 | 74 | $stub = Str::replace('$NAMESPACE$', 'App\Repositories\\' . $model, $stub); 75 | 76 | $stub = Str::replace('$CLASSNAME$', $classname, $stub); 77 | 78 | $stub = Str::replace('$MODEL$', $model, $stub); 79 | 80 | return parent::replaceNamespace($stub, $name); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/Repository/RepositoryModel.php: -------------------------------------------------------------------------------- 1 | model = $model; 24 | } 25 | 26 | /** 27 | * @param array $with 28 | * 29 | * @return Collection 30 | */ 31 | public function all(array $with = []): Collection 32 | { 33 | return $this->model->with($with)->get(); 34 | } 35 | 36 | /** 37 | * @param array $with 38 | * 39 | * @return Collection 40 | */ 41 | public function orderByIdDesc(array $with = []): Collection 42 | { 43 | return $this->model->with($with)->orderBy('id', 'DESC')->get(); 44 | } 45 | 46 | /** 47 | * @param int $id 48 | * @param array $with 49 | * @param array $params 50 | * 51 | * @return Model|null 52 | */ 53 | public function find(int $id, array $with = [], array $params = []): ?Model 54 | { 55 | return $this->model->with($with)->findOrFail($id); 56 | } 57 | 58 | /** 59 | * @param array $attributes 60 | * 61 | * @return Model 62 | */ 63 | public function new(array $attributes = []): Model 64 | { 65 | return new Model($attributes); 66 | } 67 | 68 | /** 69 | * @param array $attributes 70 | * 71 | * @return Model 72 | */ 73 | public function create(array $attributes = []): Model 74 | { 75 | return $this->model->create($attributes); 76 | } 77 | 78 | /** 79 | * @param int $id 80 | * @param array $attributes 81 | * 82 | * @return int 83 | */ 84 | public function update(int $id, array $attributes): int|bool 85 | { 86 | return $this->find($id)->update($attributes); 87 | } 88 | 89 | /** 90 | * @param int $id 91 | * 92 | * @return int|bool 93 | */ 94 | public function delete(int $id): int|bool 95 | { 96 | return $this->model::query()->find($id)->delete(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/Service/ServiceImplementation.php: -------------------------------------------------------------------------------- 1 | repository = $repository; 22 | } 23 | 24 | /** 25 | * @param array $with 26 | * 27 | * @return Collection 28 | */ 29 | public function all(array $with = []): Collection 30 | { 31 | return $this->repository->all(); 32 | } 33 | 34 | /** 35 | * @param array $with 36 | * 37 | * @return Collection 38 | */ 39 | public function orderByIdDesc(array $with = []): Collection 40 | { 41 | return $this->repository->orderByIdDesc(); 42 | } 43 | 44 | /** 45 | * @param int $id 46 | * @param array $with 47 | * @param array $params 48 | * 49 | * @return Model|null 50 | */ 51 | public function find(int $id, array $with = [], array $params = []): ?Model 52 | { 53 | return $this->find($id, $with, $params); 54 | } 55 | 56 | /** 57 | * @param array $attributes 58 | * 59 | * @return Model 60 | */ 61 | public function new(array $attributes = []): Model 62 | { 63 | return new Model($attributes); 64 | } 65 | 66 | /** 67 | * @param array $attributes 68 | * 69 | * @return Model 70 | */ 71 | public function create(array $attributes = []): Model 72 | { 73 | return $this->repository->create($attributes); 74 | } 75 | 76 | /** 77 | * @param int $id 78 | * @param array $attributes 79 | * 80 | * @return int 81 | */ 82 | public function update(int $id, array $attributes): int|bool 83 | { 84 | return $this->update($id, $attributes); 85 | } 86 | 87 | /** 88 | * @param int $id 89 | * 90 | * @return int|bool 91 | */ 92 | public function delete(int $id): int|bool 93 | { 94 | return $this->repository->delete($id); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/Providers/SarpServiceProvider.php: -------------------------------------------------------------------------------- 1 | bindAllRepositories(); 23 | $this->bindAllServices(); 24 | } 25 | 26 | $this->mergeConfigFrom(__DIR__ . '/../../config/laravel-sarp.php', 'laravel-sarp'); 27 | } 28 | 29 | /** 30 | * Bootstrap any application services. 31 | * 32 | * @return void 33 | */ 34 | public function boot() 35 | { 36 | if ($this->app->runningInConsole()) { 37 | $this->commands([ 38 | MakeRepositoryInterface::class, 39 | MakeRepository::class, 40 | MakeServiceInterface::class, 41 | MakeService::class, 42 | ]); 43 | 44 | $this->publishes([ 45 | __DIR__ . '/../../config/laravel-sarp.php' => config_path('laravel-sarp.php'), 46 | ], 'config'); 47 | } 48 | } 49 | 50 | /** 51 | * Binds the repository with its interface. 52 | * 53 | * @return void 54 | */ 55 | protected function bindAllRepositories() 56 | { 57 | /** 58 | * Auto bind the repositories. 59 | * 60 | */ 61 | 62 | $path = app_path('Repositories'); 63 | $directories = (file_exists($path)) ? File::directories($path) : []; 64 | 65 | foreach ($directories as $directory) { 66 | 67 | $files = (file_exists($directory)) ? File::files($directory) : []; 68 | 69 | $classes = []; 70 | 71 | foreach ($files as $file) { 72 | 73 | $directory = basename($directory); 74 | 75 | $classes[] = 'App\Repositories\\' . $directory . '\\' . $file->getFilenameWithoutExtension();; 76 | } 77 | 78 | (count($classes) > 0) && $this->app->bind($classes[0], $classes[1]); 79 | } 80 | } 81 | 82 | /** 83 | * Binds the service with its interface. 84 | * 85 | * @return void 86 | */ 87 | public function bindAllServices() 88 | { 89 | /** 90 | * Auto the bind services. 91 | * 92 | */ 93 | 94 | $path = app_path('Services'); 95 | $directories = (file_exists($path)) ? File::directories($path) : []; 96 | 97 | foreach ($directories as $directory) { 98 | 99 | $files = (file_exists($directory)) ? File::files($directory) : []; 100 | 101 | $classes = []; 102 | 103 | foreach ($files as $file) { 104 | 105 | $directory = basename($directory); 106 | 107 | $classes[] = 'App\Services\\' . $directory . '\\' . $file->getFilenameWithoutExtension();; 108 | } 109 | 110 | (count($classes) > 0) && $this->app->bind($classes[0], $classes[1]); 111 | } 112 | } 113 | } 114 | --------------------------------------------------------------------------------