Opt-in traits
Three traits add registration helpers to your provider. Each centralizes an idempotency guard, so a double boot — package discovery plus an explicit provider registration, for example — never clobbers or double-registers anything:
use RoundlyConsulting\PackageToolkit\Concerns\InteractsWithGates;
use RoundlyConsulting\PackageToolkit\Concerns\RegistersBladeDirectives;
use RoundlyConsulting\PackageToolkit\Concerns\RegistersBlueprintMacros;
final class CommentsServiceProvider extends PackageServiceProvider
{
use InteractsWithGates;
use RegistersBladeDirectives;
use RegistersBlueprintMacros;
public function boot(): void
{
parent::boot();
$this->registerBlueprintMacros(); // ownerKey/morphKey/auditable/polymorphicSubject/whereLikeEscaped
$this->registerBladeDirective('comment', $handler); // no-op if already registered
$this->registerBladeIf('commented', $condition); // @commented(...) … @else … @endcommented
$this->defineGate('manage-comments', $callback); // left untouched if the host defined it
}
}InteractsWithGates
defineGate(string $ability, callable|string $callback) defines an authorization gate only when the host has not defined that ability already — a package never overrides the host’s own policy for the same ability.
RegistersBladeDirectives
registerBladeDirective(string $name, callable $handler) registers a Blade directive. registerBladeIf(string $name, callable $condition) registers a conditional one from a callback returning a boolean, which Blade expands into @name, @elsename, @unlessname and @endname. A name already taken in any of those four forms is left untouched:
// boot()
$this->registerBladeIf('commentsEnabled', static fn (bool $flag): bool => $flag);@commentsEnabled($flag)
Comments are open.
@else
Comments are closed.
@endcommentsEnabled
@unlesscommentsEnabled($flag)
Comments are closed.
@endcommentsEnabledRegistersBlueprintMacros
registerBlueprintMacros() registers the ownerKey(), morphKey(), auditable() and polymorphicSubject() schema macros on Blueprint, and the whereLikeEscaped() macro on both the query and the Eloquent builder. Each one is guarded by the framework’s own macro check. For usage, see Key types & schema macros and Database helpers.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.