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