1
|
|
|
<?php namespace Arcanesoft\Auth\ViewComposers; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Auth\Models\Permission; |
4
|
|
|
use Arcanesoft\Auth\Models\Role; |
5
|
|
|
use Arcanesoft\Auth\Models\User; |
6
|
|
|
use Closure; |
7
|
|
|
use Illuminate\Contracts\View\View; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DashboardComposer |
11
|
|
|
* |
12
|
|
|
* @package Arcanesoft\Auth\ViewComposers |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class DashboardComposer |
16
|
|
|
{ |
17
|
|
|
/* ------------------------------------------------------------------------------------------------ |
18
|
|
|
| Properties |
19
|
|
|
| ------------------------------------------------------------------------------------------------ |
20
|
|
|
*/ |
21
|
|
|
/** |
22
|
|
|
* @var \Illuminate\Contracts\View\View |
23
|
|
|
*/ |
24
|
|
|
protected $view; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Arcanesoft\Auth\Models\User |
28
|
|
|
*/ |
29
|
|
|
protected $users; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Arcanesoft\Auth\Models\Role |
33
|
|
|
*/ |
34
|
|
|
protected $roles; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Arcanesoft\Auth\Models\Permission |
38
|
|
|
*/ |
39
|
|
|
protected $permissions; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
protected $minutes = 5; |
45
|
|
|
|
46
|
|
|
/* ------------------------------------------------------------------------------------------------ |
47
|
|
|
| Constructor |
48
|
|
|
| ------------------------------------------------------------------------------------------------ |
49
|
|
|
*/ |
50
|
|
|
/** |
51
|
|
|
* DashboardComposer constructor. |
52
|
|
|
* |
53
|
|
|
* @param User $users |
54
|
|
|
*/ |
55
|
|
|
public function __construct(User $users, Role $roles, Permission $permissions) |
56
|
|
|
{ |
57
|
|
|
$this->users = $users; |
58
|
|
|
$this->roles = $roles; |
59
|
|
|
$this->permissions = $permissions; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/* ------------------------------------------------------------------------------------------------ |
63
|
|
|
| Main Functions |
64
|
|
|
| ------------------------------------------------------------------------------------------------ |
65
|
|
|
*/ |
66
|
|
|
/** |
67
|
|
|
* Compose the view. |
68
|
|
|
* |
69
|
|
|
* @param \Illuminate\Contracts\View\View $view |
70
|
|
|
*/ |
71
|
|
|
public function compose(View $view) |
72
|
|
|
{ |
73
|
|
|
$this->view = $view; |
74
|
|
|
|
75
|
|
|
$this->composeUsersTotal(); |
76
|
|
|
$this->composeRolesTotal(); |
77
|
|
|
$this->composePermissionsTotal(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function composeUsersTotal() |
81
|
|
|
{ |
82
|
|
|
$total = $this->cacheResults('auth-users-count', function() { |
83
|
|
|
return $this->users->count(); |
|
|
|
|
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
$this->view->with('authUsersTotal', $total); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function composeRolesTotal() |
90
|
|
|
{ |
91
|
|
|
$total = $this->cacheResults('auth-roles-count', function() { |
92
|
|
|
return $this->roles->count(); |
|
|
|
|
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
$this->view->with('authRolesTotal', $total); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function composePermissionsTotal() |
99
|
|
|
{ |
100
|
|
|
$total = $this->cacheResults('auth-permissions-count', function() { |
101
|
|
|
return $this->permissions->count(); |
|
|
|
|
102
|
|
|
}); |
103
|
|
|
|
104
|
|
|
$this->view->with('authPermissionsTotal', $total); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Cache the results. |
109
|
|
|
* |
110
|
|
|
* @param string $name |
111
|
|
|
* @param \Closure $callback |
112
|
|
|
* |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
|
|
protected function cacheResults($name, Closure $callback) |
116
|
|
|
{ |
117
|
|
|
return \Cache::remember($name, $this->minutes, $callback); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: