1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TwoDojo\ModuleManager\Repositories; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
6
|
|
|
use TwoDojo\ModuleManager\Contracts\Repository; |
7
|
|
|
use TwoDojo\ModuleManager\Exceptions\UnknownRegistryTypeException; |
8
|
|
|
use TwoDojo\ModuleManager\Registries\EloquentRegistry; |
9
|
|
|
use TwoDojo\ModuleManager\Registries\FileRegistry; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ModuleRegistryRepository |
13
|
|
|
* |
14
|
|
|
* @method \TwoDojo\ModuleManager\Repositories\ModuleRegistryRepository enabled() |
15
|
|
|
*/ |
16
|
|
|
class ModuleRegistryRepository implements Repository |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
20
|
|
|
*/ |
21
|
|
|
protected $app; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $registryType; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \TwoDojo\ModuleManager\Registries\BaseRegistry |
30
|
|
|
*/ |
31
|
|
|
protected $registry; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ModuleRegistry constructor. |
35
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Application $app) |
38
|
|
|
{ |
39
|
|
|
$this->app = $app; |
40
|
|
|
$this->registryType = config('module_manager.registry'); |
41
|
|
|
|
42
|
|
|
$this->initializeRegistryRepository(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function save(array $attributes) |
49
|
|
|
{ |
50
|
|
|
return $this->registry->save($attributes); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
|
public function update($id, array $attributes) |
57
|
|
|
{ |
58
|
|
|
return $this->registry->update($id, $attributes); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function initializeRegistryRepository() |
62
|
|
|
{ |
63
|
|
|
switch ($this->registryType) { |
64
|
|
|
case 'database': |
65
|
|
|
$this->registry = $this->app->make(EloquentRegistry::class); |
66
|
|
|
break; |
67
|
|
|
case 'file': |
68
|
|
|
$this->registry = $this->app->make(FileRegistry::class); |
69
|
|
|
break; |
70
|
|
|
default: |
71
|
|
|
throw new UnknownRegistryTypeException($this->registryType); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function find($uniqueName) |
79
|
|
|
{ |
80
|
|
|
return $this->registry->findByUniqueName($uniqueName); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
|
|
public function findByField(string $field, $value) |
87
|
|
|
{ |
88
|
|
|
return $this->registry->findByField($field, $value); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function all() |
95
|
|
|
{ |
96
|
|
|
return $this->registry->all(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function __call($scope, $arguments) |
100
|
|
|
{ |
101
|
|
|
$this->registry->{$scope}(...$arguments); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|