Skip to content

Commit 24209ee

Browse files
committed
feat: add useful directives
1 parent c96cbc9 commit 24209ee

File tree

2 files changed

+121
-19
lines changed

2 files changed

+121
-19
lines changed

phpunit.xml

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Blade.php

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ public function directive(string $name, callable $handler)
5353
}
5454

5555
/**
56-
* Summary of blade
56+
* Return actual blade instance
5757
* @return \Jenssegers\Blade\Blade
5858
*/
5959
public function blade()
6060
{
6161
return $this->blade;
6262
}
6363

64+
/**
65+
* Hook into the blade compiler
66+
*/
67+
public function compiler()
68+
{
69+
return $this->blade->compiler();
70+
}
71+
6472
/**
6573
* Setup default directives
6674
*/
@@ -97,5 +105,117 @@ protected function setupDefaultDirectives()
97105
$this->directive('alpine', function ($expression) {
98106
return '<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>';
99107
});
108+
109+
$this->directive('isNull', function ($expression) {
110+
return "<?php if (is_null($expression)) : ?>";
111+
});
112+
113+
$this->directive('endisNull', function ($expression) {
114+
return "<?php endif; ?>";
115+
});
116+
117+
$this->compiler()->directive('env', function ($expression) {
118+
if (empty($expression)) {
119+
return "<?php echo _env('APP_ENV'); ?>";
120+
}
121+
122+
return "<?php if (strtolower(_env('APP_ENV')) === strtolower($expression)) : ?>";
123+
});
124+
125+
$this->directive('session', function ($expression) {
126+
return implode('', [
127+
"<?php if (session()->has($expression)) : ?>",
128+
"<?php if (isset(\$value)) { \$___originalCurrentSessionValue = \$value; } ?>",
129+
"<?php \$value = session()->get($expression); ?>"
130+
]);
131+
});
132+
133+
$this->directive('endsession', function ($expression) {
134+
return implode('', [
135+
"<?php unset(\$value); ?>",
136+
"<?php if (isset(\$___originalCurrentSessionValue)) { \$value = \$___originalCurrentSessionValue; } ?>",
137+
"<?php endif; ?>"
138+
]);
139+
});
140+
141+
$this->directive('flash', function ($expression) {
142+
return implode('', [
143+
"<?php if (isset(\$message)) { \$___originalCurrentFlashValue = \$message; } ?>",
144+
"<?php if (\$message = flash()->display($expression)) : ?>",
145+
]);
146+
});
147+
148+
$this->directive('endflash', function ($expression) {
149+
return implode('', [
150+
"<?php unset(\$message); ?>",
151+
"<?php if (isset(\$___originalCurrentFlashValue)) { \$message = \$___originalCurrentFlashValue; } ?>",
152+
"<?php endif; ?>",
153+
]);
154+
});
155+
156+
$this->directive('disabled', function ($expression) {
157+
return "<?php echo $expression ? 'disabled' : ''; ?>";
158+
});
159+
160+
$this->directive('selected', function ($expression) {
161+
return "<?php echo $expression ? 'selected' : ''; ?>";
162+
});
163+
164+
$this->directive('checked', function ($expression) {
165+
return "<?php echo $expression ? 'checked' : ''; ?>";
166+
});
167+
168+
$this->directive('readonly', function ($expression) {
169+
return "<?php echo $expression ? 'readonly' : ''; ?>";
170+
});
171+
172+
$this->directive('required', function ($expression) {
173+
return "<?php echo $expression ? 'required' : ''; ?>";
174+
});
175+
176+
$this->directive('use', function ($expression) {
177+
$expression = preg_replace('/[\'"]/', '', $expression);
178+
return "<?php use $expression; ?>";
179+
});
180+
181+
$this->compiler()->directive('auth', function ($expression) {
182+
return "<?php if (!!auth($expression)->user()) : ?>";
183+
});
184+
185+
$this->compiler()->directive('guest', function ($expression) {
186+
return "<?php if (!auth($expression)->user()) : ?>";
187+
});
188+
189+
$this->directive('is', function ($expression) {
190+
return "<?php if (auth()->user()->is($expression)) : ?>";
191+
});
192+
193+
$this->directive('endis', function ($expression) {
194+
return "<?php endif; ?>";
195+
});
196+
197+
$this->directive('isnot', function ($expression) {
198+
return "<?php if (auth()->user()->isNot($expression)) : ?>";
199+
});
200+
201+
$this->directive('endisnot', function ($expression) {
202+
return "<?php endif; ?>";
203+
});
204+
205+
$this->directive('can', function ($expression) {
206+
return "<?php if (auth()->user()->can($expression)) : ?>";
207+
});
208+
209+
$this->directive('endcan', function ($expression) {
210+
return "<?php endif; ?>";
211+
});
212+
213+
$this->directive('cannot', function ($expression) {
214+
return "<?php if (auth()->user()->cannot($expression)) : ?>";
215+
});
216+
217+
$this->directive('endcannot', function ($expression) {
218+
return "<?php endif; ?>";
219+
});
100220
}
101221
}

0 commit comments

Comments
 (0)