1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Providers; |
13
|
|
|
|
14
|
|
|
use Illuminate\Support\ServiceProvider; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* BladeServiceProvider is the blade service provider for extending blade template engine. |
18
|
|
|
* |
19
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class BladeServiceProvider extends ServiceProvider |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Bootstrap any application services. |
25
|
|
|
*/ |
26
|
59 |
|
public function boot() |
27
|
|
|
{ |
28
|
59 |
|
\Blade::directive( |
29
|
59 |
|
'macro', |
30
|
|
View Code Duplication |
function ($expression) { |
|
|
|
|
31
|
2 |
|
$pattern = '/(\([\'|\"](\w+)[\'|\"],\s*(([^\@])+|(.*))\))/xim'; |
32
|
2 |
|
$matches = []; |
33
|
2 |
|
preg_match_all($pattern, $expression, $matches); |
34
|
|
|
|
35
|
2 |
|
if (!isset($matches[3][0])) { |
36
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid arguments in blade: macro%s', $expression)); |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
return sprintf("<?php \$___tiny['%s']=function(%s)use(\$__env){ ob_start(); ?>\n", $matches[2][0], $matches[3][0]); |
40
|
59 |
|
} |
41
|
|
|
); |
42
|
|
|
|
43
|
59 |
|
\Blade::directive( |
44
|
59 |
|
'endmacro', |
45
|
|
|
function () { |
46
|
2 |
|
return "\n<?php return ob_get_clean();} ?>\n"; |
47
|
59 |
|
} |
48
|
|
|
); |
49
|
|
|
|
50
|
59 |
|
\Blade::directive( |
51
|
59 |
|
'usemacro', |
52
|
|
View Code Duplication |
function ($expression) { |
|
|
|
|
53
|
2 |
|
$pattern = '/(\([\'|\"](\w+)[\'|\"],\s*(([^\@])+|(.*))\))/xim'; |
54
|
2 |
|
$matches = []; |
55
|
2 |
|
preg_match_all($pattern, $expression, $matches); |
56
|
|
|
|
57
|
2 |
|
if (!isset($matches[3][0])) { |
58
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid arguments in blade: usemacro%s', $expression)); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
return sprintf("<?php echo \$___tiny['%s'](%s); ?>\n", $matches[2][0], $matches[3][0]); |
62
|
59 |
|
} |
63
|
|
|
); |
64
|
|
|
|
65
|
59 |
|
\Blade::directive( |
66
|
59 |
|
'permission', |
67
|
|
|
function ($expression) { |
68
|
5 |
|
return "<?php if(!Auth::guest() && Auth::user()->permission{$expression}): ?>"; |
69
|
59 |
|
} |
70
|
|
|
); |
71
|
|
|
|
72
|
59 |
|
\Blade::directive( |
73
|
59 |
|
'endpermission', |
74
|
|
|
function () { |
75
|
5 |
|
return '<?php endif; ?>'; |
76
|
59 |
|
} |
77
|
|
|
); |
78
|
|
|
|
79
|
59 |
|
\Blade::directive( |
80
|
59 |
|
'mailattrs', |
81
|
|
|
function ($expression) { |
82
|
|
|
// Get parameters |
83
|
2 |
|
$params = array_map('trim', explode('|', trim($expression, '()'))); |
84
|
|
|
// Get style based on the first parameter |
85
|
2 |
|
$style = config('mailcss.' . $params[0]); |
86
|
|
|
|
87
|
2 |
|
if (!$style) { |
88
|
|
|
return ''; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Convert the parameters starting from second into key => value array |
92
|
2 |
|
$override = array_reduce(array_slice($params, 1), function ($override, $param) { |
93
|
2 |
|
$segments = array_map('trim', explode('=', $param)); |
94
|
2 |
|
$override[$segments[0]] = $segments[1]; |
95
|
|
|
|
96
|
2 |
|
return $override; |
97
|
2 |
|
}, []); |
98
|
|
|
|
99
|
|
|
// Style can be callback or an array. Merge and echo. |
100
|
2 |
|
if (is_callable($style)) { |
101
|
2 |
|
$style = $style($override); |
102
|
1 |
|
} elseif (isset($params[1])) { |
103
|
|
|
$style = array_merge($style, $override); |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
return "<?php echo '" . \Html::attributes($style) . "'; ?>"; |
107
|
59 |
|
} |
108
|
|
|
); |
109
|
59 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Register any application services. |
113
|
|
|
* |
114
|
|
|
* This service provider is a great spot to register your various container |
115
|
|
|
* bindings with the application. As you can see, we are registering our |
116
|
|
|
* "Registrar" implementation here. You can add your own bindings too! |
117
|
|
|
*/ |
118
|
59 |
|
public function register() |
119
|
|
|
{ |
120
|
59 |
|
} |
121
|
|
|
} |
122
|
|
|
|
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.