1 | <?php |
||
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) |
|
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) |
|
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() |
|
145 | } |
||
146 |