Skip to content

Commit cf1c1e5

Browse files
authored
Merge pull request #15 from yaza-putu/feature/support-laravel-11
Feature/support laravel 11
2 parents 6fa6aec + 2100604 commit cf1c1e5

File tree

10 files changed

+136
-129
lines changed

10 files changed

+136
-129
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ With easy repository, you can have the power of the repository pattern, without
44

55
## Requirement
66

7-
- Minimum PHP ^8.1
7+
- Minimum PHP ^8.2
88

99
## Installation
1010

@@ -17,9 +17,13 @@ Specific Version :
1717

1818
| Laravel Version | Package Version |
1919
|:---------------:|:------------------:|
20-
| 10 | 4.x |
20+
| 11 | 5.x |
21+
| 10 | 4.0 |
2122
| 9 | 3.2 |
2223
```bash
24+
# for laravel 10
25+
$ composer require yaza/laravel-repository-service:"^4.0"
26+
# for laravel 9
2327
$ composer require yaza/laravel-repository-service:"^3.2"
2428
```
2529

@@ -50,8 +54,8 @@ php artisan make:service UserService
5054
// or
5155
php artisan make:service UserService --repository
5256

53-
// create service for api template
54-
php artisan make:service UserService --api
57+
// create service with blank template
58+
php artisan make:service UserService --blank
5559

5660
```
5761

composer.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
"homepage": "https://github.com/yaza-putu/laravel-repository-with-service.git",
1010
"license": "MIT",
1111
"require": {
12-
"php": "^8.1",
13-
"spatie/laravel-package-tools": "^1.14.0",
14-
"illuminate/contracts": "^10.0"
12+
"php": "^8.2",
13+
"spatie/laravel-package-tools": "^1.16",
14+
"illuminate/contracts": "^10.0||^11.0"
1515
},
1616
"require-dev": {
17-
"laravel/pint": "^1.0",
18-
"nunomaduro/collision": "^7.9",
19-
"nunomaduro/larastan": "^2.0.1",
20-
"orchestra/testbench": "^8.0",
21-
"pestphp/pest": "^2.0",
22-
"pestphp/pest-plugin-arch": "^2.0",
23-
"pestphp/pest-plugin-laravel": "^2.0",
24-
"phpstan/extension-installer": "^1.1",
25-
"phpstan/phpstan-deprecation-rules": "^1.0",
26-
"phpstan/phpstan-phpunit": "^1.0",
27-
"spatie/laravel-ray": "^1.26"
17+
"laravel/pint": "^1.14",
18+
"nunomaduro/collision": "^8.1.1||^7.10.0",
19+
"larastan/larastan": "^2.9",
20+
"orchestra/testbench": "^9.0.0||^8.22.0",
21+
"pestphp/pest": "^2.34",
22+
"pestphp/pest-plugin-arch": "^2.7",
23+
"pestphp/pest-plugin-laravel": "^2.3",
24+
"phpstan/extension-installer": "^1.3",
25+
"phpstan/phpstan-deprecation-rules": "^1.1",
26+
"phpstan/phpstan-phpunit": "^1.3",
27+
"spatie/laravel-ray": "^1.35"
2828
},
2929
"autoload": {
3030
"psr-4": {

src/Commands/MakeRepository.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,16 @@ public function createRepositoryInterface(string $className)
8282
];
8383

8484
$repositoryInterfacePath = $this->getRepositoryInterfacePath($className);
85+
if (file_exists($repositoryInterfacePath)) {
86+
$this->error("file $className repository interface already exist");
87+
return ;
88+
}
8589

8690
new CreateFile(
8791
$stubProperties,
8892
$repositoryInterfacePath,
8993
__DIR__ . "/stubs/repository-interface.stub"
9094
);
91-
9295
$this->line("<info>Created $className repository interface:</info> " . $repositoryInterfaceName);
9396

9497
return $repositoryInterfaceNamespace . "\\" . $className;
@@ -114,6 +117,10 @@ public function createRepository(string $className, $isDefault = true)
114117

115118
$stubName = $isDefault ? "eloquent-repository.stub" : "custom-repository.stub";
116119
$repositoryPath = $this->getRepositoryPath($className, $isDefault);
120+
if (file_exists($repositoryPath)) {
121+
$this->error("file $className repository already exist");
122+
return ;
123+
}
117124
new CreateFile(
118125
$stubProperties,
119126
$repositoryPath,
@@ -170,6 +177,5 @@ private function checkIfRequiredDirectoriesExist(string $className)
170177
{
171178
$this->ensureDirectoryExists(config("easy-repository.repository_directory"));
172179
$this->ensureDirectoryExists(config("easy-repository.repository_directory") . "/". $className);
173-
$this->ensureDirectoryExists(config("easy-repository.repository_directory") . "/" . $className);
174180
}
175181
}

src/Commands/MakeService.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MakeService extends Command
1515
public $signature = 'make:service
1616
{name : The name of the service }
1717
{--repository : Create a repository along with the service}?
18-
{--api : Create a service with the api template}?';
18+
{--blank : Create a service with the blank template}?';
1919

2020
public $description = 'Create a new service class';
2121

@@ -24,7 +24,7 @@ public function handle()
2424
$name = str_replace(config("easy-repository.service_interface_suffix"), "", $this->argument("name"));
2525
$className = Str::studly($name);
2626

27-
$this->checkIfRequiredDirectoriesExist();
27+
$this->checkIfRequiredDirectoriesExist($className);
2828

2929
$this->createServiceInterface($className);
3030

@@ -54,17 +54,17 @@ public function createService(string $className)
5454
"{repositoryInterfaceName}" => $this->getRepositoryInterfaceName($nameOfService),
5555
"{repositoryInterfaceNamespace}" => $this->getRepositoryInterfaceNamespace($nameOfService),
5656
];
57-
// check folder exist
58-
$folder = str_replace('\\','/', $namespace);
59-
if (!file_exists($folder)) {
60-
File::makeDirectory($folder, 0775, true, true);
57+
// cek file exist
58+
if(file_exists($this->getServicePath($className,$nameOfService))) {
59+
$this->error("file $className repository already exist");
60+
return ;
6161
}
6262

63-
// check command api
64-
if($this->option("api")) {
65-
$stubPath = __DIR__ . "/stubs/service-api.stub";
66-
} else {
63+
// check command blank
64+
if($this->option("blank")) {
6765
$stubPath = __DIR__ . "/stubs/service.stub";
66+
} else {
67+
$stubPath = __DIR__ . "/stubs/service-api.stub";
6868
}
6969

7070
// create file
@@ -92,11 +92,12 @@ public function createServiceInterface(string $className)
9292
"{namespace}" => $namespace,
9393
"{serviceInterface}" => $serviceName,
9494
];
95-
// check folder exist
96-
$folder = str_replace('\\','/', $namespace);
97-
if (!file_exists($folder)) {
98-
File::makeDirectory($folder, 0775, true, true);
95+
// cek file exist
96+
if(file_exists($this->getServiceInterfacePath($className,$serviceName))) {
97+
$this->error("file $className repository interface already exist");
98+
return ;
9999
}
100+
100101
// create file
101102
new CreateFile(
102103
$stubProperties,
@@ -174,9 +175,10 @@ private function getRepositoryName(string $className) {
174175
*
175176
* @return void
176177
*/
177-
private function checkIfRequiredDirectoriesExist()
178+
private function checkIfRequiredDirectoriesExist($className)
178179
{
179180
$this->ensureDirectoryExists(config("easy-repository.service_directory"));
181+
$this->ensureDirectoryExists(config("easy-repository.service_directory").'/'.$className);
180182
}
181183

182184
/**
@@ -221,4 +223,4 @@ private function createRepository()
221223
"name" => $name,
222224
]);
223225
}
224-
}
226+
}

src/Commands/stubs/service-api.stub

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ use {repositoryInterfaceNamespace}\{repositoryInterfaceName};
88
class {serviceName} extends ServiceApi implements {serviceInterface}{
99

1010
/**
11-
* set message api for CRUD
11+
* set title message api for CRUD
1212
* @param string $title
13-
* @param string $create_message
14-
* @param string $update_message
15-
* @param string $delete_message
1613
*/
1714
protected $title = "";
18-
protected $create_message = "";
19-
protected $update_message = "";
20-
protected $delete_message = "";
15+
/**
16+
* uncomment this to override the default message
17+
* protected $create_message = "";
18+
* protected $update_message = "";
19+
* protected $delete_message = "";
20+
*/
2121

2222
/**
2323
* don't change $this->mainRepository variable name

src/Service.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ public function all()
3939
/**
4040
* Create an item
4141
* @param array|mixed $data
42-
* @return void
42+
* @return model|mixed
4343
*/
4444
public function create($data)
4545
{
46-
$this->mainRepository->create($data);
46+
return $this->mainRepository->create($data);
4747
}
4848

4949
/**
5050
* Update a model
5151
* @param int|mixed $id
5252
* @param array|mixed $data
53-
* @return void
53+
* @return bool|mixed
5454
*/
5555
public function update($id, array $data)
5656
{
57-
$this->mainRepository->update($id, $data);
57+
return $this->mainRepository->update($id, $data);
5858
}
5959

6060
/**

src/ServiceApi.php

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,79 +10,80 @@ class ServiceApi implements BaseService
1010
use ResultService;
1111

1212
protected $title = "";
13-
protected $create_message = "";
14-
protected $update_message = "";
15-
protected $delete_message = "";
13+
protected $create_message = "created successfully";
14+
protected $update_message = "updated successfully";
15+
protected $delete_message = "deleted successfully";
16+
1617

1718
/**
18-
* Find an item by id
19-
* @param mixed $id
20-
* @return Model|null
19+
* find by id
20+
* @param $id
21+
* @return \Illuminate\Database\Eloquent\Model|ServiceApi|ResultService|null
2122
*/
2223
public function find($id)
2324
{
2425
try {
2526
$result = $this->mainRepository->find($id);
26-
return $this->setResult($result)
27-
->setCode(200)
28-
->setStatus(true);
27+
return $this->setData($result)
28+
->setCode(200);
2929
} catch (\Exception $exception) {
3030
return $this->exceptionResponse($exception);
3131
}
3232
}
3333

34+
3435
/**
35-
* Find an item by id or fail
36-
* @param mixed $id
37-
* @return Model|null
36+
* find or fail by id
37+
* @param $id
38+
* @return ServiceApi|ResultService|mixed
3839
*/
3940
public function findOrFail($id)
4041
{
4142
try {
4243
$result = $this->mainRepository->findOrFail($id);
43-
return $this->setResult($result)
44-
->setCode(200)
45-
->setStatus(true);
44+
return $this->setData($result)
45+
->setCode(200);
4646
} catch (\Exception $exception) {
4747
return $this->exceptionResponse($exception);
4848
}
4949
}
5050

51+
5152
/**
52-
* Return all items
53-
* @return Collection|null
53+
* all data
54+
* @return \Illuminate\Database\Eloquent\Collection|ServiceApi|ResultService|null
5455
*/
5556
public function all()
5657
{
5758
try {
5859
$result = $this->mainRepository->all();;
59-
return $this->setResult($result)
60-
->setCode(200)
61-
->setStatus(true);
60+
return $this->setData($result)
61+
->setCode(200);
6262
} catch (\Exception $exception) {
6363
return $this->exceptionResponse($exception);
6464
}
6565
}
6666

67+
6768
/**
68-
* Create an item
69-
* @param array|mixed $data
70-
* @return Model|null
69+
* create data
70+
* @param $data
71+
* @return \Illuminate\Database\Eloquent\Model|ServiceApi|ResultService|null
7172
*/
7273
public function create($data)
7374
{
7475
try {
75-
$this->mainRepository->create($data);
76+
$data = $this->mainRepository->create($data);
7677
return $this->setMessage($this->title." ".$this->create_message)
7778
->setCode(200)
78-
->setStatus(true);
79+
->setData($data);
7980
} catch (\Exception $exception) {
8081
return $this->exceptionResponse($exception);
8182
}
8283
}
8384

8485
/**
85-
* Update a model
86+
* Update data
8687
* @param int|mixed $id
8788
* @param array|mixed $data
8889
* @return bool|mixed
@@ -92,24 +93,22 @@ public function update($id, array $data)
9293
try {
9394
$this->mainRepository->update($id, $data);
9495
return $this->setMessage($this->title." ".$this->update_message)
95-
->setCode(200)
96-
->setStatus(true);
96+
->setCode(200);
9797
} catch (\Exception $exception) {
9898
return $this->exceptionResponse($exception);
9999
}
100100
}
101101

102102
/**
103-
* Delete a model
103+
* Delete data by id
104104
* @param int|Model $id
105105
*/
106106
public function delete($id)
107107
{
108108
try {
109109
$this->mainRepository->delete($id);
110110
return $this->setMessage($this->title." ".$this->delete_message)
111-
->setCode(200)
112-
->setStatus(true);
111+
->setCode(200);
113112
} catch (\Exception $exception) {
114113
return $this->exceptionResponse($exception);
115114
}
@@ -125,8 +124,7 @@ public function destroy(array $id)
125124
try {
126125
$this->mainRepository->destroy($id);
127126
return $this->setMessage($this->title." ".$this->delete_message)
128-
->setCode(200)
129-
->setStatus(true);
127+
->setCode(200);
130128
} catch (\Exception $exception) {
131129
return $this->exceptionResponse($exception);
132130
}

src/Traits/JsonValidateResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function failedValidation(Validator $validator)
2222
$errors = (new ValidationException($validator))->errors();
2323

2424
throw new HttpResponseException(
25-
response()->json(['errors' => $errors, 'success' => false, 'code' => 422, 'message' => 'validation error messages'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)
25+
response()->json(['errors' => $errors, 'code' => 422, 'message' => 'Unprocessable Entity'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)
2626
);
2727
}
2828

0 commit comments

Comments
 (0)