1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maklad\Permission\Directives; |
4
|
|
|
|
5
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class PermissionDirectives |
9
|
|
|
* @package Maklad\Permission\Directives |
10
|
|
|
*/ |
11
|
|
|
class PermissionDirectives |
12
|
|
|
{ |
13
|
|
|
private $bladeCompiler; |
14
|
|
|
|
15
|
18 |
|
public function __construct(BladeCompiler $bladeCompiler) |
16
|
|
|
{ |
17
|
18 |
|
$this->bladeCompiler = $bladeCompiler; |
18
|
18 |
|
} |
19
|
|
|
|
20
|
|
View Code Duplication |
public function role_directive() |
|
|
|
|
21
|
|
|
{ |
22
|
18 |
|
$this->bladeCompiler->directive('role', function ($arguments) { |
23
|
5 |
|
$arguments = preg_replace('(\(|\)| )', '', $arguments); |
24
|
5 |
|
list($role, $guard) = \explode(',', $arguments . ','); |
25
|
|
|
|
26
|
5 |
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>"; |
27
|
18 |
|
}); |
28
|
|
|
|
29
|
18 |
|
$this->bladeCompiler->directive('endrole', function () { |
30
|
5 |
|
return '<?php endif; ?>'; |
31
|
18 |
|
}); |
32
|
18 |
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function hasrole_directive() |
|
|
|
|
35
|
|
|
{ |
36
|
18 |
|
$this->bladeCompiler->directive('hasrole', function ($arguments) { |
37
|
5 |
|
$arguments = preg_replace('(\(|\)| )', '', $arguments); |
38
|
5 |
|
list($role, $guard) = \explode(',', $arguments . ','); |
39
|
|
|
|
40
|
5 |
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>"; |
41
|
18 |
|
}); |
42
|
18 |
|
$this->bladeCompiler->directive('endhasrole', function () { |
43
|
5 |
|
return '<?php endif; ?>'; |
44
|
18 |
|
}); |
45
|
18 |
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function hasanyrole_directive() |
|
|
|
|
48
|
|
|
{ |
49
|
18 |
|
$this->bladeCompiler->directive('hasanyrole', function ($arguments) { |
50
|
8 |
|
$arguments = preg_replace('(\(|\)| )', '', $arguments); |
51
|
8 |
|
list($roles, $guard) = \explode(',', $arguments . ','); |
52
|
|
|
|
53
|
8 |
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>"; |
54
|
18 |
|
}); |
55
|
18 |
|
$this->bladeCompiler->directive('endhasanyrole', function () { |
56
|
8 |
|
return '<?php endif; ?>'; |
57
|
18 |
|
}); |
58
|
18 |
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function hasallroles_directive() |
|
|
|
|
61
|
|
|
{ |
62
|
18 |
|
$this->bladeCompiler->directive('hasallroles', function ($arguments) { |
63
|
8 |
|
$arguments = preg_replace('(\(|\)| )', '', $arguments); |
64
|
8 |
|
list($roles, $guard) = \explode(',', $arguments . ','); |
65
|
|
|
|
66
|
8 |
|
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>"; |
67
|
18 |
|
}); |
68
|
18 |
|
$this->bladeCompiler->directive('endhasallroles', function () { |
69
|
8 |
|
return '<?php endif; ?>'; |
70
|
18 |
|
}); |
71
|
|
|
} |
72
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.