|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
|
6
|
|
|
use Illuminate\Contracts\Auth\Access\Gate; |
|
7
|
|
|
|
|
8
|
|
|
class AdminSectionsServiceProvider extends ServiceProvider |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var array Associative array in form of: Model::class => Section::class |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $sections = []; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array Associative array in form of: Section::class => Policy::class |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $policies = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
285 |
|
public function sections() |
|
24
|
|
|
{ |
|
25
|
285 |
|
return $this->sections; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param \SleepingOwl\Admin\Admin $admin |
|
30
|
|
|
*/ |
|
31
|
285 |
|
public function boot(\SleepingOwl\Admin\Admin $admin) |
|
32
|
|
|
{ |
|
33
|
285 |
|
$admin->registerSections($this->sections()); |
|
34
|
285 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Register the service provider. |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
|
|
public function register() |
|
42
|
|
|
{ |
|
43
|
|
|
// TODO: Implement register() method. |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string|null $namespace |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function policies($namespace = null) |
|
52
|
|
|
{ |
|
53
|
|
|
if (is_null($namespace)) { |
|
54
|
|
|
$namespace = config('sleeping_owl.policies_namespace', '\\App\\Policies\\'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$policies = []; |
|
58
|
|
|
$preparedPolicies = collect($this->policies); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($this->sections as $model => $section) { |
|
61
|
|
|
if ($preparedPolicies->has($section)) { |
|
62
|
|
|
$policies[$section] = $preparedPolicies->get($section); |
|
63
|
|
|
continue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$policies[$section] = $namespace.class_basename($section).'SectionModelPolicy'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $policies; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string|null $namespace |
|
74
|
|
|
*/ |
|
75
|
|
|
public function registerPolicies($namespace = null) |
|
76
|
|
|
{ |
|
77
|
|
|
foreach ($this->policies($namespace) as $section => $policy) { |
|
78
|
|
|
$this->app[Gate::class]->policy($section, $policy); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|