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
|
63 |
|
public function boot() |
27
|
|
|
{ |
28
|
63 |
|
\Blade::directive('macro', function($expression) { |
29
|
63 |
|
return $this->macroDirective($expression); |
30
|
|
|
}); |
31
|
2 |
|
|
32
|
2 |
|
\Blade::directive( |
33
|
2 |
|
'endmacro', |
34
|
|
|
function () { |
35
|
2 |
|
return "\n<?php return ob_get_clean();} ?>\n"; |
36
|
|
|
} |
37
|
|
|
); |
38
|
|
|
\Blade::directive('usemacro', function($expression) { |
39
|
2 |
|
return $this->usemacroDirective($expression); |
40
|
63 |
|
}); |
41
|
|
|
|
42
|
|
|
\Blade::directive( |
43
|
63 |
|
'permission', |
44
|
63 |
|
function ($expression) { |
45
|
|
|
return "<?php if(!Auth::guest() && Auth::user()->permission{$expression}): ?>"; |
46
|
2 |
|
} |
47
|
63 |
|
); |
48
|
|
|
|
49
|
|
|
\Blade::directive( |
50
|
63 |
|
'endpermission', |
51
|
63 |
|
function () { |
52
|
|
|
return '<?php endif; ?>'; |
53
|
2 |
|
} |
54
|
2 |
|
); |
55
|
2 |
|
|
56
|
|
|
\Blade::directive( |
57
|
2 |
|
'mailattrs', |
58
|
|
|
function ($expression) { |
59
|
|
|
// Get parameters |
60
|
|
|
$params = array_map('trim', explode('|', trim($expression, '()'))); |
61
|
2 |
|
// Get style based on the first parameter |
62
|
63 |
|
$style = config('mailcss.' . $params[0]); |
63
|
|
|
|
64
|
|
|
if (!$style) { |
65
|
63 |
|
return ''; |
66
|
63 |
|
} |
67
|
|
|
|
68
|
5 |
|
// Convert the parameters starting from second into key => value array |
69
|
63 |
|
$override = array_reduce(array_slice($params, 1), function ($override, $param) { |
70
|
|
|
$segments = array_map('trim', explode('=', $param)); |
71
|
|
|
$override[$segments[0]] = $segments[1]; |
72
|
63 |
|
|
73
|
63 |
|
return $override; |
74
|
|
|
}, []); |
75
|
5 |
|
|
76
|
63 |
|
// Style can be callback or an array. Merge and echo. |
77
|
|
|
if (is_callable($style)) { |
78
|
|
|
$style = $style($override); |
79
|
63 |
|
} elseif (isset($params[1])) { |
80
|
63 |
|
$style = array_merge($style, $override); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return "<?php echo '" . \Html::attributes($style) . "'; ?>"; |
84
|
|
|
} |
85
|
1 |
|
); |
86
|
|
|
} |
87
|
1 |
|
|
88
|
|
|
/** |
89
|
|
|
* Callback method for macro directive. |
90
|
|
|
* |
91
|
|
|
* @param $expression |
92
|
1 |
|
* |
93
|
1 |
|
* @return string |
94
|
1 |
|
*/ |
95
|
|
View Code Duplication |
protected function macroDirective($expression) |
|
|
|
|
96
|
1 |
|
{ |
97
|
1 |
|
$name = $args = ''; |
98
|
|
|
extract($this->extractArgumentsAndName($expression)); |
|
|
|
|
99
|
|
|
|
100
|
1 |
|
return sprintf("<?php \$___tiny['%s']=function(%s)use(\$__env){ ob_start(); ?>\n", $name, $args); |
101
|
1 |
|
} |
102
|
1 |
|
|
103
|
|
|
/** |
104
|
|
|
* Callback method for usemacro directive. |
105
|
|
|
* |
106
|
1 |
|
* @param $expression |
107
|
63 |
|
* |
108
|
|
|
* @return string |
109
|
63 |
|
*/ |
110
|
|
View Code Duplication |
protected function usemacroDirective($expression) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$name = $args = ''; |
113
|
|
|
extract($this->extractArgumentsAndName($expression)); |
|
|
|
|
114
|
|
|
|
115
|
|
|
return sprintf("<?php echo \$___tiny['%s'](%s); ?>\n", $name, $args); |
116
|
|
|
} |
117
|
|
|
|
118
|
63 |
|
/** |
119
|
|
|
* Return arguments and name from blade expression. |
120
|
63 |
|
* |
121
|
|
|
* @param $expression |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function extractArgumentsAndName($expression) |
126
|
|
|
{ |
127
|
|
|
$pattern = '/(\([\'|\"](\w+)[\'|\"],\s*(([^\@])+|(.*))\))/xim'; |
128
|
|
|
$matches = []; |
129
|
|
|
preg_match_all($pattern, $expression, $matches); |
130
|
|
|
|
131
|
|
|
if (!isset($matches[3][0])) { |
132
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid arguments in blade: macro%s', $expression)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return [ |
136
|
|
|
'name' => $matches[2][0], |
137
|
|
|
'args' => $matches[3][0], |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Register any application services. |
143
|
|
|
* |
144
|
|
|
* This service provider is a great spot to register your various container |
145
|
|
|
* bindings with the application. As you can see, we are registering our |
146
|
|
|
* "Registrar" implementation here. You can add your own bindings too! |
147
|
|
|
*/ |
148
|
|
|
public function register() |
149
|
|
|
{ |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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.