|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\CMS\Repositories\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
|
6
|
|
|
use Illuminate\Support\Facades\File; |
|
7
|
|
|
use Yajra\CMS\Entities\Extension; |
|
8
|
|
|
use Yajra\CMS\Exceptions\InvalidManifestException; |
|
9
|
|
|
use Yajra\CMS\Repositories\RepositoryAbstract; |
|
10
|
|
|
|
|
11
|
|
|
class EloquentRepository extends RepositoryAbstract implements Repository |
|
12
|
|
|
{ |
|
13
|
|
|
use ValidatesRequests; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Install an extension. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $type |
|
19
|
|
|
* @param array $attributes |
|
20
|
|
|
* @return \Yajra\CMS\Entities\Extension |
|
21
|
|
|
*/ |
|
22
|
|
|
public function install($type, array $attributes) |
|
23
|
|
|
{ |
|
24
|
|
|
$extension = new Extension; |
|
25
|
|
|
$extension->type = $type; |
|
26
|
|
|
$extension->name = $attributes['name']; |
|
27
|
|
|
if (isset($attributes['parameters'])) { |
|
28
|
|
|
$extension->parameters = $attributes['parameters']; |
|
29
|
|
|
} |
|
30
|
|
|
$extension->manifest = json_encode($attributes); |
|
31
|
|
|
$extension->save(); |
|
32
|
|
|
|
|
33
|
|
|
return $extension; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Uninstall extension. |
|
38
|
|
|
* |
|
39
|
|
|
* @param int $id |
|
40
|
|
|
* @throws \Exception |
|
41
|
|
|
*/ |
|
42
|
|
|
public function uninstall($id) |
|
43
|
|
|
{ |
|
44
|
|
|
$extension = $this->getModel()->query()->findOrFail($id); |
|
|
|
|
|
|
45
|
|
|
// TODO: remove extension files. |
|
46
|
|
|
$extension->delete(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get repository model. |
|
51
|
|
|
* |
|
52
|
|
|
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getModel() |
|
55
|
|
|
{ |
|
56
|
|
|
return new Extension; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get all extensions. |
|
61
|
|
|
* |
|
62
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
|
63
|
|
|
*/ |
|
64
|
|
|
public function all() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->getModel()->all(); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Find or fail an extension. |
|
71
|
|
|
* |
|
72
|
|
|
* @param int $id |
|
73
|
|
|
* @return \Yajra\CMS\Entities\Extension |
|
74
|
|
|
*/ |
|
75
|
|
|
public function findOrFail($id) |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->getModel()->query()->findOrFail($id); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get all registered widgets. |
|
82
|
|
|
* |
|
83
|
|
|
* @return \Illuminate\Support\Collection |
|
84
|
|
|
*/ |
|
85
|
|
|
public function allWidgets() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->getModel()->query()->where('type', 'widget')->get(); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Register an extension using manifest config file. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $path |
|
94
|
|
|
* @throws \Exception |
|
95
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function registerManifest($path) |
|
98
|
|
|
{ |
|
99
|
|
|
if (! File::exists($path)) { |
|
100
|
|
|
throw new \Exception('Extension manifest file does not exist! Path: ' . $path); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$manifest = File::get($path); |
|
104
|
|
|
$manifest = json_decode($manifest, true); |
|
105
|
|
|
$this->register($manifest); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Register an extension. |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $attributes |
|
112
|
|
|
* @return $this |
|
113
|
|
|
* @throws \Exception |
|
114
|
|
|
*/ |
|
115
|
|
|
public function register(array $attributes) |
|
116
|
|
|
{ |
|
117
|
|
|
$validator = $this->getValidationFactory()->make($attributes, [ |
|
118
|
|
|
'type' => 'required', |
|
119
|
|
|
'name' => 'required', |
|
120
|
|
|
'parameters.class' => 'required_if:type,widget', |
|
121
|
|
|
'parameters.templates' => 'required_if:type,widget', |
|
122
|
|
|
'parameters.template' => 'required_if:type,menu', |
|
123
|
|
|
]); |
|
124
|
|
|
|
|
125
|
|
|
if ($validator->fails()) { |
|
126
|
|
|
throw new InvalidManifestException("Invalid manifest file detected!"); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$extension = new Extension; |
|
130
|
|
|
$extension->type = $attributes['type']; |
|
131
|
|
|
$extension->name = $attributes['name']; |
|
132
|
|
|
if (isset($attributes['protected'])) { |
|
133
|
|
|
$extension->protected = $attributes['protected']; |
|
134
|
|
|
} |
|
135
|
|
|
if (isset($attributes['parameters'])) { |
|
136
|
|
|
$extension->parameters = $attributes['parameters']; |
|
137
|
|
|
} |
|
138
|
|
|
$extension->manifest = json_encode($attributes); |
|
139
|
|
|
$extension->save(); |
|
140
|
|
|
|
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: