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