|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\CMS\Widgets; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
|
6
|
|
|
use Illuminate\Support\Facades\File; |
|
7
|
|
|
use Yajra\CMS\Entities\Extension; |
|
8
|
|
|
|
|
9
|
|
|
class EloquentRepository implements Repository |
|
10
|
|
|
{ |
|
11
|
|
|
use ValidatesRequests; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Register a widget using manifest config file. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $path |
|
17
|
|
|
* @throws \Exception |
|
18
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
19
|
|
|
*/ |
|
20
|
|
|
public function registerManifest($path) |
|
21
|
|
|
{ |
|
22
|
|
|
if (! File::exists($path)) { |
|
23
|
|
|
throw new \Exception('Widget manifest file does not exist! Path: ' . $path); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$manifest = File::get($path); |
|
27
|
|
|
$manifest = json_decode($manifest, true); |
|
28
|
|
|
$this->register($manifest); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Register a widget. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $attributes |
|
35
|
|
|
* @return $this |
|
36
|
|
|
* @throws \Exception |
|
37
|
|
|
*/ |
|
38
|
|
|
public function register(array $attributes) |
|
39
|
|
|
{ |
|
40
|
|
|
$validator = $this->getValidationFactory()->make($attributes, [ |
|
41
|
|
|
'type' => 'required', |
|
42
|
|
|
'name' => 'required', |
|
43
|
|
|
'class' => 'required', |
|
44
|
|
|
'templates' => 'required', |
|
45
|
|
|
'parameters' => 'json', |
|
46
|
|
|
]); |
|
47
|
|
|
|
|
48
|
|
|
if ($validator->fails()) { |
|
49
|
|
|
throw new \Exception('Invalid widget config detected!'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$extension = new Extension; |
|
53
|
|
|
$extension->type = 'widget'; |
|
54
|
|
|
$extension->name = $attributes['name']; |
|
55
|
|
|
if (isset($attributes['parameters'])) { |
|
56
|
|
|
$extension->parameters = $attributes['parameters']; |
|
57
|
|
|
} |
|
58
|
|
|
$extension->manifest = json_encode($attributes); |
|
59
|
|
|
$extension->save(); |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Find or fail a widget. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $name |
|
68
|
|
|
* @return \Yajra\CMS\Widgets\Widget |
|
69
|
|
|
* @throws \Yajra\CMS\Widgets\NotFoundException |
|
70
|
|
|
*/ |
|
71
|
|
|
public function findOrFail($name) |
|
72
|
|
|
{ |
|
73
|
|
|
if ($extension = Extension::widget($name)) { |
|
74
|
|
|
|
|
75
|
|
|
return new Widget($extension->manifest); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
throw new NotFoundException('Widget not found!'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get all registered widgets. |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Illuminate\Support\Collection |
|
85
|
|
|
*/ |
|
86
|
|
|
public function all() |
|
87
|
|
|
{ |
|
88
|
|
|
return Extension::all(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|