diff --git a/README.md b/README.md index 778720b..6195ac3 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ With easy repository, you can have the power of the repository pattern, without ## Requirement -- Minimum PHP ^8.1 +- Minimum PHP ^8.2 ## Installation @@ -17,9 +17,13 @@ Specific Version : | Laravel Version | Package Version | |:---------------:|:------------------:| -| 10 | 4.x | +| 11 | 5.x | +| 10 | 4.0 | | 9 | 3.2 | ```bash +# for laravel 10 +$ composer require yaza/laravel-repository-service:"^4.0" +# for laravel 9 $ composer require yaza/laravel-repository-service:"^3.2" ``` @@ -50,8 +54,8 @@ php artisan make:service UserService // or php artisan make:service UserService --repository -// create service for api template -php artisan make:service UserService --api +// create service with blank template +php artisan make:service UserService --blank ``` diff --git a/composer.json b/composer.json index 53390e5..a072790 100644 --- a/composer.json +++ b/composer.json @@ -9,22 +9,22 @@ "homepage": "https://github.com/yaza-putu/laravel-repository-with-service.git", "license": "MIT", "require": { - "php": "^8.1", - "spatie/laravel-package-tools": "^1.14.0", - "illuminate/contracts": "^10.0" + "php": "^8.2", + "spatie/laravel-package-tools": "^1.16", + "illuminate/contracts": "^10.0||^11.0" }, "require-dev": { - "laravel/pint": "^1.0", - "nunomaduro/collision": "^7.9", - "nunomaduro/larastan": "^2.0.1", - "orchestra/testbench": "^8.0", - "pestphp/pest": "^2.0", - "pestphp/pest-plugin-arch": "^2.0", - "pestphp/pest-plugin-laravel": "^2.0", - "phpstan/extension-installer": "^1.1", - "phpstan/phpstan-deprecation-rules": "^1.0", - "phpstan/phpstan-phpunit": "^1.0", - "spatie/laravel-ray": "^1.26" + "laravel/pint": "^1.14", + "nunomaduro/collision": "^8.1.1||^7.10.0", + "larastan/larastan": "^2.9", + "orchestra/testbench": "^9.0.0||^8.22.0", + "pestphp/pest": "^2.34", + "pestphp/pest-plugin-arch": "^2.7", + "pestphp/pest-plugin-laravel": "^2.3", + "phpstan/extension-installer": "^1.3", + "phpstan/phpstan-deprecation-rules": "^1.1", + "phpstan/phpstan-phpunit": "^1.3", + "spatie/laravel-ray": "^1.35" }, "autoload": { "psr-4": { diff --git a/src/Commands/MakeRepository.php b/src/Commands/MakeRepository.php index 9665691..c76c0c0 100644 --- a/src/Commands/MakeRepository.php +++ b/src/Commands/MakeRepository.php @@ -82,13 +82,16 @@ public function createRepositoryInterface(string $className) ]; $repositoryInterfacePath = $this->getRepositoryInterfacePath($className); + if (file_exists($repositoryInterfacePath)) { + $this->error("file $className repository interface already exist"); + return ; + } new CreateFile( $stubProperties, $repositoryInterfacePath, __DIR__ . "/stubs/repository-interface.stub" ); - $this->line("Created $className repository interface: " . $repositoryInterfaceName); return $repositoryInterfaceNamespace . "\\" . $className; @@ -114,6 +117,10 @@ public function createRepository(string $className, $isDefault = true) $stubName = $isDefault ? "eloquent-repository.stub" : "custom-repository.stub"; $repositoryPath = $this->getRepositoryPath($className, $isDefault); + if (file_exists($repositoryPath)) { + $this->error("file $className repository already exist"); + return ; + } new CreateFile( $stubProperties, $repositoryPath, @@ -170,6 +177,5 @@ private function checkIfRequiredDirectoriesExist(string $className) { $this->ensureDirectoryExists(config("easy-repository.repository_directory")); $this->ensureDirectoryExists(config("easy-repository.repository_directory") . "/". $className); - $this->ensureDirectoryExists(config("easy-repository.repository_directory") . "/" . $className); } } diff --git a/src/Commands/MakeService.php b/src/Commands/MakeService.php index 3dbde54..4d4a760 100644 --- a/src/Commands/MakeService.php +++ b/src/Commands/MakeService.php @@ -15,7 +15,7 @@ class MakeService extends Command public $signature = 'make:service {name : The name of the service } {--repository : Create a repository along with the service}? - {--api : Create a service with the api template}?'; + {--blank : Create a service with the blank template}?'; public $description = 'Create a new service class'; @@ -24,7 +24,7 @@ public function handle() $name = str_replace(config("easy-repository.service_interface_suffix"), "", $this->argument("name")); $className = Str::studly($name); - $this->checkIfRequiredDirectoriesExist(); + $this->checkIfRequiredDirectoriesExist($className); $this->createServiceInterface($className); @@ -54,17 +54,17 @@ public function createService(string $className) "{repositoryInterfaceName}" => $this->getRepositoryInterfaceName($nameOfService), "{repositoryInterfaceNamespace}" => $this->getRepositoryInterfaceNamespace($nameOfService), ]; - // check folder exist - $folder = str_replace('\\','/', $namespace); - if (!file_exists($folder)) { - File::makeDirectory($folder, 0775, true, true); + // cek file exist + if(file_exists($this->getServicePath($className,$nameOfService))) { + $this->error("file $className repository already exist"); + return ; } - // check command api - if($this->option("api")) { - $stubPath = __DIR__ . "/stubs/service-api.stub"; - } else { + // check command blank + if($this->option("blank")) { $stubPath = __DIR__ . "/stubs/service.stub"; + } else { + $stubPath = __DIR__ . "/stubs/service-api.stub"; } // create file @@ -92,11 +92,12 @@ public function createServiceInterface(string $className) "{namespace}" => $namespace, "{serviceInterface}" => $serviceName, ]; - // check folder exist - $folder = str_replace('\\','/', $namespace); - if (!file_exists($folder)) { - File::makeDirectory($folder, 0775, true, true); + // cek file exist + if(file_exists($this->getServiceInterfacePath($className,$serviceName))) { + $this->error("file $className repository interface already exist"); + return ; } + // create file new CreateFile( $stubProperties, @@ -174,9 +175,10 @@ private function getRepositoryName(string $className) { * * @return void */ - private function checkIfRequiredDirectoriesExist() + private function checkIfRequiredDirectoriesExist($className) { $this->ensureDirectoryExists(config("easy-repository.service_directory")); + $this->ensureDirectoryExists(config("easy-repository.service_directory").'/'.$className); } /** @@ -221,4 +223,4 @@ private function createRepository() "name" => $name, ]); } -} +} \ No newline at end of file diff --git a/src/Commands/stubs/service-api.stub b/src/Commands/stubs/service-api.stub index b377513..9fe3736 100644 --- a/src/Commands/stubs/service-api.stub +++ b/src/Commands/stubs/service-api.stub @@ -8,16 +8,16 @@ use {repositoryInterfaceNamespace}\{repositoryInterfaceName}; class {serviceName} extends ServiceApi implements {serviceInterface}{ /** - * set message api for CRUD + * set title message api for CRUD * @param string $title - * @param string $create_message - * @param string $update_message - * @param string $delete_message */ protected $title = ""; - protected $create_message = ""; - protected $update_message = ""; - protected $delete_message = ""; + /** + * uncomment this to override the default message + * protected $create_message = ""; + * protected $update_message = ""; + * protected $delete_message = ""; + */ /** * don't change $this->mainRepository variable name diff --git a/src/Service.php b/src/Service.php index 59c3d36..77b5881 100644 --- a/src/Service.php +++ b/src/Service.php @@ -39,22 +39,22 @@ public function all() /** * Create an item * @param array|mixed $data - * @return void + * @return model|mixed */ public function create($data) { - $this->mainRepository->create($data); + return $this->mainRepository->create($data); } /** * Update a model * @param int|mixed $id * @param array|mixed $data - * @return void + * @return bool|mixed */ public function update($id, array $data) { - $this->mainRepository->update($id, $data); + return $this->mainRepository->update($id, $data); } /** diff --git a/src/ServiceApi.php b/src/ServiceApi.php index 573a300..9de0270 100644 --- a/src/ServiceApi.php +++ b/src/ServiceApi.php @@ -10,79 +10,80 @@ class ServiceApi implements BaseService use ResultService; protected $title = ""; - protected $create_message = ""; - protected $update_message = ""; - protected $delete_message = ""; + protected $create_message = "created successfully"; + protected $update_message = "updated successfully"; + protected $delete_message = "deleted successfully"; + /** - * Find an item by id - * @param mixed $id - * @return Model|null + * find by id + * @param $id + * @return \Illuminate\Database\Eloquent\Model|ServiceApi|ResultService|null */ public function find($id) { try { $result = $this->mainRepository->find($id); - return $this->setResult($result) - ->setCode(200) - ->setStatus(true); + return $this->setData($result) + ->setCode(200); } catch (\Exception $exception) { return $this->exceptionResponse($exception); } } + /** - * Find an item by id or fail - * @param mixed $id - * @return Model|null + * find or fail by id + * @param $id + * @return ServiceApi|ResultService|mixed */ public function findOrFail($id) { try { $result = $this->mainRepository->findOrFail($id); - return $this->setResult($result) - ->setCode(200) - ->setStatus(true); + return $this->setData($result) + ->setCode(200); } catch (\Exception $exception) { return $this->exceptionResponse($exception); } } + /** - * Return all items - * @return Collection|null + * all data + * @return \Illuminate\Database\Eloquent\Collection|ServiceApi|ResultService|null */ public function all() { try { $result = $this->mainRepository->all();; - return $this->setResult($result) - ->setCode(200) - ->setStatus(true); + return $this->setData($result) + ->setCode(200); } catch (\Exception $exception) { return $this->exceptionResponse($exception); } } + /** - * Create an item - * @param array|mixed $data - * @return Model|null + * create data + * @param $data + * @return \Illuminate\Database\Eloquent\Model|ServiceApi|ResultService|null */ public function create($data) { try { - $this->mainRepository->create($data); + $data = $this->mainRepository->create($data); return $this->setMessage($this->title." ".$this->create_message) ->setCode(200) - ->setStatus(true); + ->setData($data); } catch (\Exception $exception) { return $this->exceptionResponse($exception); } } /** - * Update a model + * Update data * @param int|mixed $id * @param array|mixed $data * @return bool|mixed @@ -92,15 +93,14 @@ public function update($id, array $data) try { $this->mainRepository->update($id, $data); return $this->setMessage($this->title." ".$this->update_message) - ->setCode(200) - ->setStatus(true); + ->setCode(200); } catch (\Exception $exception) { return $this->exceptionResponse($exception); } } /** - * Delete a model + * Delete data by id * @param int|Model $id */ public function delete($id) @@ -108,8 +108,7 @@ public function delete($id) try { $this->mainRepository->delete($id); return $this->setMessage($this->title." ".$this->delete_message) - ->setCode(200) - ->setStatus(true); + ->setCode(200); } catch (\Exception $exception) { return $this->exceptionResponse($exception); } @@ -125,8 +124,7 @@ public function destroy(array $id) try { $this->mainRepository->destroy($id); return $this->setMessage($this->title." ".$this->delete_message) - ->setCode(200) - ->setStatus(true); + ->setCode(200); } catch (\Exception $exception) { return $this->exceptionResponse($exception); } diff --git a/src/Traits/JsonValidateResponse.php b/src/Traits/JsonValidateResponse.php index 9d6d496..a2dbdc2 100644 --- a/src/Traits/JsonValidateResponse.php +++ b/src/Traits/JsonValidateResponse.php @@ -22,7 +22,7 @@ public function failedValidation(Validator $validator) $errors = (new ValidationException($validator))->errors(); throw new HttpResponseException( - response()->json(['errors' => $errors, 'success' => false, 'code' => 422, 'message' => 'validation error messages'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY) + response()->json(['errors' => $errors, 'code' => 422, 'message' => 'Unprocessable Entity'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY) ); } diff --git a/src/Traits/Response.php b/src/Traits/Response.php index 6acb0e3..3a60e72 100644 --- a/src/Traits/Response.php +++ b/src/Traits/Response.php @@ -17,19 +17,19 @@ trait Response * @param null $code * @return \Illuminate\Http\JsonResponse */ - public function responseJson($status = true, $msg = '', $data = [], $code = null) + public function responseJson($msg = '', $data = [], $code = null, $errors = null) { if(is_null($code)){ - $http_code = $status ? 200 : 400; + $http_code = 200; }else{ $http_code = $code; } - return response()->json([ - 'success' => $status, + return response()->json(array_filter([ 'code' => $http_code, 'message' => $msg, 'data' => $data, - ], $http_code); + 'errors' => $errors + ]), $http_code); } } diff --git a/src/Traits/ResultService.php b/src/Traits/ResultService.php index 9aef854..eced6b6 100644 --- a/src/Traits/ResultService.php +++ b/src/Traits/ResultService.php @@ -10,51 +10,30 @@ trait ResultService { - private $result = null; - private $status = false; + private $data = null; private $message = null; private $code = null; + private $errors = null; /** - * set result output - * @param $result + * set data output + * @param $data * @return $this */ - public function setResult($result) + public function setData($data) { - $this->result = $result; + $this->data = $data; return $this; } /** - * get result + * get data * @return null */ - public function getResult() + public function getData() { - return $this->result; - } - - /** - * set status - * @param $status - * @return $this - */ - public function setStatus($status) - { - $this->status = $status; - - return $this; - } - - /** - * get status - * @return bool - */ - public function getStatus() - { - return $this->status; + return $this->data; } /** @@ -99,6 +78,27 @@ public function getCode() return $this->code; } + + /** + * set errors + * @param $error + * @return $this + */ + public function setError($error) + { + $this->errors = $error; + return $this; + } + + /** + * get errors + * @return array + */ + public function getError() + { + return $this->errors; + } + /** * Exception Response * @@ -109,8 +109,7 @@ public function exceptionResponse(Exception $exception) { if ($exception instanceof QueryException) { if ($exception->errorInfo[1] == 1451) { - return $this->setStatus(false) - ->setMessage('Data masih terpakai di Data Lain!') + return $this->setMessage('This data cannot be removed because it is still in use.') ->setCode(400); } } @@ -118,8 +117,7 @@ public function exceptionResponse(Exception $exception) if (!request()->expectsJson()) { return abort(404); } - return $this->setStatus(false) - ->setMessage('Data tidak ditemukan!') + return $this->setMessage('Data not found') ->setCode(404); } if (config('app.debug')) { @@ -130,13 +128,12 @@ public function exceptionResponse(Exception $exception) 'line' => $exception->getLine(), 'trace' => $exception->getTrace() ]; - return $this->setStatus(false) - ->setMessage($message) + return $this->setError($message) + ->setMessage("internal server error") ->setCode(500); } - return $this->setStatus(false) - ->setMessage('Terjadi suatu kesalahan!') + return $this->setMessage('internal server error') ->setCode(500); } @@ -147,16 +144,16 @@ public function exceptionResponse(Exception $exception) public function toJson() { if(is_null($this->getCode())){ - $http_code = $this->getStatus() ? 200 : 400; + $http_code = 200; }else{ $http_code = $this->getCode(); } - return response()->json([ - 'success' => $this->getStatus(), + return response()->json(array_filter([ 'code' => $http_code, 'message' => $this->getMessage(), - 'data' => $this->getResult(), - ], $http_code); + 'data' => $this->getData(), + 'errors' => $this->getError(), + ]), $http_code); } }