|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TwoDojo\ModuleManager\Registries; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use TwoDojo\ModuleManager\Repositories\EloquentRepository; |
|
8
|
|
|
use TwoDojo\ModuleManager\Support\ModuleDescriptor; |
|
9
|
|
|
|
|
10
|
|
|
class EloquentRegistry extends BaseRegistry |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $app; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var \TwoDojo\ModuleManager\Repositories\EloquentRepository |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $repository; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* EloquentRegistry constructor. |
|
24
|
|
|
* @param Application $app |
|
25
|
|
|
* @param EloquentRepository $repository |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Application $app, EloquentRepository $repository) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->app = $app; |
|
30
|
|
|
$this->repository = $repository; |
|
31
|
|
|
$this->repository->setModel($this->app->make(config('module_manager.models.module'))); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritdoc |
|
36
|
|
|
*/ |
|
37
|
|
|
public function save(array $attributes) |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->repository->save($attributes); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function update($id, array $attributes) |
|
46
|
|
|
{ |
|
47
|
|
|
$model = $this->repository->find($id); |
|
48
|
|
|
if ($model === null) { |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this->repository->update($model, $attributes); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function find($id) |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->mergeDescriptor($this->repository->find($id)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
|
|
*/ |
|
66
|
|
|
public function findByUniqueName(string $uniqueName) |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->findByField('uniqueName', $uniqueName); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritdoc |
|
73
|
|
|
*/ |
|
74
|
|
|
public function findByField(string $field, $value) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->mergeDescriptor($this->repository->findByField($field, $value)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
*/ |
|
82
|
|
|
public function all() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->mergeDescriptor($this->repository->all()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function mergeDescriptor($value) |
|
88
|
|
|
{ |
|
89
|
|
|
$collection = collect([]); |
|
90
|
|
|
|
|
91
|
|
|
if ($value === null) { |
|
92
|
|
|
return $collection; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (is_array($value) || $value instanceof Collection) { |
|
96
|
|
|
foreach ($value as $model) { |
|
97
|
|
|
$collection->put($model->id, new ModuleDescriptor($model->toArray())); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $collection; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $collection->put($value->id, new ModuleDescriptor($value->toArray())); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
protected function scope($scope, $arguments) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->repository->{$scope}(...$arguments); |
|
109
|
|
|
|
|
110
|
|
|
return parent::scope($scope, $arguments); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|