-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRepository.php
640 lines (549 loc) · 16.1 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<?php
/**
* Created by PhpStorm.
* User: Towfiqul Islam
* Email: towfiq.106@gmail.com
* Date: 26-10-2019
* Time: 02:40 AM
*/
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Filesystem\Filesystem;
class Repository extends GeneratorCommand
{
protected $base_path;
protected $repository_directory;
protected $model_directory;
protected $path;
protected $files;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:repository {repository} {model} {--s|service=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a repository class {repository: repository name} {model: model name} {--s|service=: service name}';
/**
* Create a new command instance.
*
* @param Filesystem $files
*/
public function __construct(Filesystem $files)
{
parent::__construct($files);
$this->files = $files;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$repository_name = $this->argument('repository');
$model = $this->argument('model');
$service = $this->option('service');
$this->repository_directory = config('repository.repository_directory');
$this->model_directory = config('repository.model_directory');
$this->base_path = base_path() . '/app/';
$this->path = $this->base_path . $this->repository_directory;
$isRepositoryDirectoryExist = $this->isRepositoryDirectoryExist($this->path);
$isContractsDirectoryExist = $isRepositoryDirectoryExist ? $this->isContractsDirectoryExist($this->path) : false;
$isModelExist = $this->isFileExist($this->base_path . $this->model_directory, $model);
if (!$isModelExist) {
$this->error("Model not found");
}
if ($isRepositoryDirectoryExist && $isModelExist) {
if ($isContractsDirectoryExist) {
$this->makeInterface();
} else {
$this->error('Contracts Directory does not exits.');
$this->error('Interface class creation failed.');
$this->error($repository_name . ' Repository class creation failed.');
}
$this->makeAbstractClass();
$this->makeRepositoryClass($repository_name, $model);
} else {
$this->error($repository_name . ' Repository class creation failed.');
}
if($service){
$this->makeService($service, $repository_name);
}
}
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
//TODO::
}
protected function isFileExist(string $path, $fileName): bool
{
$path = $path . '/' . $fileName . '.php';
return $this->files->exists($path);
}
protected function makeService($service, $repository)
{
$this->call('make:service', [
'service' => $service, 'repository' => $repository
]);
}
/**
* @param string $path
* @return bool
*/
protected function isRepositoryDirectoryExist(string $path): bool
{
$isDirectoryExist = $this->files->isDirectory($path);
if (!$isDirectoryExist) {
if ($this->confirm('Repositories directory does not exist. Shall I create directory ' . $this->repository_directory . ' ?')) {
$this->path = $this->base_path . $this->repository_directory;
$this->files->makeDirectory($this->path, 0755, true, true);
$this->info($this->path . ' directory created.');
$isDirectoryExist = true;
}
}
return $isDirectoryExist;
}
/**
* @param $path
* @return bool
*/
protected function isContractsDirectoryExist($path): bool
{
$path = $path . '/Contracts';
$isDirectoryExist = $this->files->isDirectory($path);
if (!$isDirectoryExist) {
$this->line($path . ' directory does not exist.');
$this->files->makeDirectory($path, 0755, true, true);
$this->info($path . ' directory created.');
$isDirectoryExist = true;
}
return $isDirectoryExist;
}
protected function repositoryInterfaceContent()
{
return $content = '<?php
/**
* Created by PhpStorm.
* User: Towfiqul Islam
* Email: towfiq.106@gmail.com
* Date: ' . date('d-m-Y', time()) . '
* Time: ' . date('h:i A', time()) . '
*/
namespace App\\' . $this->repository_directory . '\Contracts;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
/**
* Interface RepositoryInterface
* @package App\Repositories\Contracts
*/
interface RepositoryInterface
{
/**
* Find a resource by id
*
* @param $id
* @param $relation
* @return Model|null
*/
public function findOne($id, $relation);
/**
* Find a resource by criteria
*
* @param array $criteria
* @param $relation
* @return Model|null
*/
public function findOneBy(array $criteria, $relation);
/**
* Search All resources by criteria
*
* @param array $searchCriteria
* @param null $relation
* @param array|null $orderBy
* @return Collection
*/
public function findBy(array $searchCriteria = [], $relation = null, array $orderBy = null);
/**
* Search All resources by any values of a key
*
* @param string $key
* @param array $values
* @param null $relation
* @param array|null $orderBy
* @return Collection
*/
public function findIn($key, array $values, $relation = null, array $orderBy = null);
/**
* @param null $perPage
* @param null $relation
* @param array|null $orderBy
* @return Collection
*/
public function findAll($perPage = null, $relation = null, array $orderBy = null);
/**
* @param $id
* @param null $relation
* @param array|null $orderBy
* @return mixed
*/
public function findOrFail($id, $relation = null, array $orderBy = null);
/**
* Save a resource
*
* @param array $data
* @return Model
*/
public function save(array $data);
/**
* Update a resource
*
* @param Model $model
* @param array $data
* @return Model
*/
public function update(Model $model, array $data);
/**
* Save or Update a resource
*
* @param array $attributes
* @param array $data
* @return Model
*/
public function saveOrUpdate(array $attributes, array $data);
/**
* Delete a resource
*
* @param Model $model
* @return mixed
*/
public function delete(Model $model);
}
';
}
/**
* @return string
*/
protected function repositoryAbstractContent()
{
return $content = '<?php
/**
* Created by PhpStorm.
* User: Towfiqul Islam
* Email: towfiq.106@gmail.com
* Date: ' . date('d-m-Y', time()) . '
* Time: ' . date('h:i A', time()) . '
*/
namespace App\\' . $this->repository_directory . ';
use App\\' . $this->repository_directory . '\Contracts\RepositoryInterface;
use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* Class AbstractBaseRepository
* @package App\Repositories
*/
abstract class AbstractRepository implements RepositoryInterface
{
/**
* Name of the Model with absolute namespace
*
* @var string
*/
protected $modelName;
/**
* Instance that extends Illuminate\Database\Eloquent\Model
*
* @var Model
*/
protected $model;
/**
* Constructor
* @throws Exception
*/
public function __construct()
{
$this->setModel();
}
/**
* Instantiate Model
*
* @throws Exception
*/
public function setModel()
{
//check if the class exists
if (class_exists($this->modelName)) {
$this->model = new $this->modelName;
//check object is a instanceof Illuminate\Database\Eloquent\Model
if (!$this->model instanceof Model) {
throw new Exception("{$this->modelName} must be an instance of Illuminate\Database\Eloquent\Model");
}
} else {
throw new Exception(\'No model name defined\');
}
}
/**
* Get Model instance
*
* @return Model
*/
public function getModel()
{
return $this->model;
}
/**
* Find a resource by id
*
* @param $id
* @param null $relation
* @return Model|null
*/
public function findOne($id, $relation = null)
{
return $this->findOneBy([\'id\' => $id], $relation);
}
/**
* Find a resource by criteria
*
* @param array $criteria
* @param null $relation
* @return Model|null
*/
public function findOneBy(array $criteria, $relation = null)
{
return $this->prepareModelForRelationAndOrder($relation)->where($criteria)->first();
}
/**
* Search All resources by criteria
*
* @param array $searchCriteria
* @param null $relation
* @param array|null $orderBy
* @return Collection
*/
public function findBy(array $searchCriteria = [], $relation = null, array $orderBy = null)
{
$model = $this->prepareModelForRelationAndOrder($relation, $orderBy);
$limit = !empty($searchCriteria[\'per_page\']) ? (int)$searchCriteria[\'per_page\'] : 15; // it\'s needed for pagination
$queryBuilder = $model->where(function ($query) use ($searchCriteria) {
$this->applySearchCriteriaInQueryBuilder($query, $searchCriteria);
}
);
if (!empty($searchCriteria[\'per_page\'])) {
return $queryBuilder->paginate($limit);
}
return $queryBuilder->get();
}
/**
* Find the Selected Columns
*
* @param array $selectedColumns
* @param null $relation
* @param array|null $orderBy
* @return Collection
*/
public function findSelected(array $selectedColumns = [], $relation = null, array $orderBy = null)
{
$model = $this->prepareModelForRelationAndOrder($relation, $orderBy);
$queryBuilder = $model->select($selectedColumns);
return $queryBuilder->get();
}
/**
* Apply condition on query builder based on search criteria
*
* @param Object $queryBuilder
* @param array $searchCriteria
* @return mixed
*/
protected function applySearchCriteriaInQueryBuilder($queryBuilder, array $searchCriteria = [])
{
foreach ($searchCriteria as $key => $value) {
//skip pagination related query params
if (in_array($key, [\'page\', \'per_page\'])) {
continue;
}
//we can pass multiple params for a filter with commas
$allValues = explode(\',\', $value);
if (count($allValues) > 1) {
$queryBuilder->whereIn($key, $allValues);
} else {
$operator = \'=\';
$queryBuilder->where($key, $operator, $value);
}
}
return $queryBuilder;
}
/**
* Search All resources by any values of a key
*
* @param string $key
* @param array $values
* @param null $relation
* @param array|null $orderBy
* @return Collection
*/
public function findIn($key, array $values, $relation = null, array $orderBy = null)
{
return $this->prepareModelForRelationAndOrder($relation, $orderBy)->whereIn($key, $values)->get();
}
/**
* @param null $perPage
* @param null $relation
* @param array|null $orderBy
* @return Collection|LengthAwarePaginator|Builder[]|Collection|Model[]
*/
public function findAll($perPage = null, $relation = null, array $orderBy = null)
{
$model = $this->prepareModelForRelationAndOrder($relation, $orderBy);
if ($perPage) {
return $model->paginate($perPage);
}
return $model->get();
}
/**
* @param $id
* @param null $relation
* @param array|null $orderBy
* @return Builder|Builder[]|Collection|Model|Model[]|mixed
*/
public function findOrFail($id, $relation = null, array $orderBy = null)
{
return $this->prepareModelForRelationAndOrder($relation, $orderBy)->findOrFail($id);
}
/**
* Save a resource
*
* @param array $data
* @return Model
*/
public function save(array $data)
{
return $this->model->create($data);
}
/**
* Update a resource
*
* @param Model $model
* @param array $data
* @return bool
*/
public function update(Model $model, array $data)
{
$fillAbleProperties = $this->model->getFillable();
foreach ($data as $key => $value) {
// update only fillAble properties
if (in_array($key, $fillAbleProperties)) {
$model->$key = $value;
}
}
// update the model
return $model->save();
}
/**
* Save or Update a resource
*
* @param array $attributes
* @param array $data
* @return Model
*/
public function saveOrUpdate(array $attributes, array $data)
{
return $this->model->updateOrCreate($attributes, $data);
}
/**
* Delete a resource
*
* @param Model $model
* @return mixed
* @throws Exception
*/
public function delete(Model $model)
{
return $model->delete();
}
/**
* @param $relation
* @param array|null $orderBy [[Column], [Direction]]
* @return Builder|Model
*/
private function prepareModelForRelationAndOrder($relation, array $orderBy = null)
{
$model = $this->model;
if ($relation) {
$model = $model->with($relation);
}
if ($orderBy) {
$model = $model->orderBy($orderBy[\'column\'], $orderBy[\'direction\']);
}
return $model;
}
}
';
}
/**
* @param bool $isContractsDirectoryExist
* @param $repository_name
*/
protected function makeInterface(): void
{
$filePath = $this->path . '/Contracts/RepositoryInterface.php';
if (!$this->files->exists($filePath)) {
$this->line('Repository Interface does not exist.');
$this->files->put($filePath, $this->repositoryInterfaceContent());
$this->info('Repository Interface has been created successfully');
}
}
/**
* @param $repository_name
*/
protected function makeAbstractClass(): void
{
$filePath = $this->path . '/AbstractRepository.php';
if (!$this->files->exists($filePath)) {
$this->line('Abstract Repository does not exist.');
$this->files->put($filePath, $this->repositoryAbstractContent());
$this->info('Abstract Repository has been created successfully');
}
}
/**
* @param $repository_name
* @param $model
*/
protected function makeRepositoryClass($repository_name, $model): void
{
$filePath = $this->path . '/' . $repository_name . '.php';
if (!$this->files->exists($filePath)) {
$this->files->put($filePath, $this->repositoryContent($repository_name, $model));
$this->info($repository_name . ' Repository has been created successfully');
} else {
$this->error($repository_name . ' Repository exists.');
}
}
/**
* @param $name
* @param $model
* @return string
*/
protected function repositoryContent($name, $model)
{
return $content = '<?php
namespace App\\' . $this->repository_directory . ';
use App\\' . config('repository.model_directory') . '\\' . $model . ';
class ' . $name . ' extends AbstractRepository
{
protected $modelName = ' . $model . '::class;
}
';
}
}