Skip to content

Commit

Permalink
Use Blade directives from rapidez/blade-directives + hasAny macro (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning authored Jun 28, 2023
1 parent ba79c9a commit bfaeb1f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"illuminate/events": "^9.0",
"illuminate/queue": "^9.0",
"illuminate/support": "^9.23",
"rapidez/blade-directives": "^0.0.1",
"tormjens/eventy": "^0.8"
},
"require-dev": {
Expand Down
38 changes: 37 additions & 1 deletion src/RapidezServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Vite;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\ComponentAttributeBag;
use Rapidez\Core\Commands\IndexProductsCommand;
use Rapidez\Core\Commands\InstallCommand;
use Rapidez\Core\Commands\InstallTestsCommand;
Expand All @@ -29,6 +30,7 @@ public function boot()
->bootPublishables()
->bootRoutes()
->bootViews()
->bootMacros()
->bootBladeComponents()
->bootMiddleware()
->bootTranslations();
Expand All @@ -44,7 +46,8 @@ public function register()
$this
->registerConfigs()
->registerBindings()
->registerThemes();
->registerThemes()
->registerBladeDirectives();
}

protected function bootCommands(): self
Expand Down Expand Up @@ -134,6 +137,11 @@ protected function bootBladeComponents(): self
{
Blade::component('placeholder', PlaceholderComponent::class);

return $this;
}

protected function registerBladeDirectives(): self
{
Blade::directive('content', function ($expression) {
return "<?php echo Rapidez::content({$expression}) ?>";
});
Expand All @@ -157,6 +165,34 @@ protected function bootBladeComponents(): self
return $this;
}

protected function bootMacros(): self
{
/**
* @see https://github.com/laravel/framework/pull/47569
* Check if the componentAttributeBag contains any of the keys.
* Usage: $attributes->hasAny(['href', ':href', "v-bind:href"])
*/
ComponentAttributeBag::macro('hasAny', function ($key)
{
/** @var ComponentAttributeBag $this */
if (!isset($this->attributes)) {
return false;
}

$keys = is_array($key) ? $key : func_get_args();

foreach ($keys as $value) {
if ($this->has($value)) {
return true;
}
}

return false;
});

return $this;
}

protected function bootMiddleware(): self
{
$this->app->make(Kernel::class)->pushMiddleware(DetermineAndSetShop::class);
Expand Down

0 comments on commit bfaeb1f

Please sign in to comment.