|
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\Support\Facades\Cache; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ViewComposer |
|
11
|
|
|
* |
|
12
|
|
|
* @package Arcanesoft\Auth\Bases |
|
13
|
|
|
* @author ARCANEDEV <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class ViewComposer |
|
16
|
|
|
{ |
|
17
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
18
|
|
|
| Properties |
|
19
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
20
|
|
|
*/ |
|
21
|
|
|
/** |
|
22
|
|
|
* The View instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @var \Illuminate\Contracts\View\View |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $view; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Caching time. |
|
30
|
|
|
* |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $minutes = 5; |
|
34
|
|
|
|
|
35
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
36
|
|
|
| Main Functions |
|
37
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
38
|
|
|
*/ |
|
39
|
|
|
/** |
|
40
|
|
|
* Get all cached users. |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getCachedUsers() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->cacheResults('users.all', function() { |
|
47
|
|
|
return User::all(); |
|
48
|
|
|
}); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get all cached roles. |
|
53
|
|
|
* |
|
54
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getCachedRoles() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->cacheResults('roles.all', function() { |
|
59
|
|
|
return Role::all(); |
|
60
|
|
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get all cached permissions. |
|
65
|
|
|
* |
|
66
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function getCachedPermissions() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->cacheResults('permissions.all', function() { |
|
71
|
|
|
return Permission::all(); |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
76
|
|
|
| Other Functions |
|
77
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
78
|
|
|
*/ |
|
79
|
|
|
/** |
|
80
|
|
|
* Cache the results. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $name |
|
83
|
|
|
* @param \Closure $callback |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function cacheResults($name, Closure $callback) |
|
88
|
|
|
{ |
|
89
|
|
|
return Cache::remember("auth::{$name}", $this->minutes, $callback); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|