Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Hooks Laravel Package

artisanpack-ui/hooks

WordPress-style actions and filters for Laravel. Register callbacks on named hooks and filter values with helper functions, Facades, and Blade directives. Predictable priority order, auto-discovery, and support for removing specific or all callbacks.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Modularity & Extensibility: The package aligns well with Laravel’s modular architecture, enabling plugin-style extensions via WordPress-style hooks (actions/filters). This is ideal for:
    • Third-party packages needing to inject behavior into core workflows (e.g., analytics, logging, notifications).
    • Monolithic-to-microservices decomposition by decoupling concerns via hooks (e.g., payment processing, email templates).
    • Legacy system integration where procedural callbacks are preferred over event-driven patterns.
  • Separation of Concerns: Prioritized execution (lower numbers run first) mirrors Laravel’s service container and middleware, making it intuitive for developers familiar with the framework.
  • Blade Integration: Blade directives (@action, @filter) enable template-level extensibility, reducing the need for complex view composers or service injection in views.

Integration Feasibility

  • Zero Configuration: Laravel’s package discovery eliminates manual config/app.php edits, reducing deployment friction.
  • Backward Compatibility: Supports Laravel 10.x/11.x and PHP 8.2+, with no breaking changes in minor versions (e.g., removal APIs added in 1.1.0).
  • Facade vs. Functions: Offers dual APIs (helper functions + facades), accommodating teams with differing preferences (e.g., facades for global state, functions for local scope).
  • Testing Support: Pest integration ensures reliable unit/integration testing of hook-based logic, critical for CI/CD pipelines.

Technical Risk

Risk Area Mitigation Strategy
Performance Overhead Benchmark doAction/applyFilters in high-throughput systems (e.g., 100+ callbacks). Consider lazy-loading hooks or priority-based batching.
Callback Leaks Use removeAction/removeFilter to avoid memory leaks in long-running processes (e.g., queues). Monitor with Laravel’s debugbar or tntsearch/laravel-query-cache.
Priority Conflicts Document priority ranges (e.g., 0–10 for core, 10–20 for plugins) to prevent "callback hell."
Blade Security Sanitize @filter outputs in Blade to avoid XSS (e.g., wrap with e() or htmlspecialchars).
Version Locking Pin to ^1.2 in composer.json to avoid auto-updates during minor releases.

Key Questions

  1. Use Case Alignment:
    • Is this replacing Laravel Events, Observers, or Middleware? (Events are better for async workflows; hooks excel at modular extensions.)
    • Will hooks be used for runtime configuration (e.g., dynamic feature flags) or static behavior (e.g., logging)?
  2. Scalability:
    • How many hooks/filters will be registered? (Test with 10K+ callbacks to validate performance.)
    • Are hooks used in high-frequency loops (e.g., API rate limiting)? If so, consider caching doAction results.
  3. Team Adoption:
    • Does the team prefer explicit facades (e.g., Action::do()) or implicit functions (e.g., doAction())?
    • Will Blade directives be used in templates, or is this a backend-only concern?
  4. Maintenance:
    • How will hook namespaces be managed (e.g., auth.login, payment.process) to avoid collisions?
    • Are there plans for hook deprecation? (Use deprecated() in callbacks to warn users.)

Integration Approach

Stack Fit

  • Laravel-Centric: Designed for Laravel’s ecosystem (e.g., service providers, facades, Blade). Avoids reinventing Laravel’s Events or Service Container but complements them.
  • PHP 8.2+ Features: Leverages named arguments, arrow functions, and type hints for modern syntax.
  • Tooling Integration:
    • Laravel Boost: AI guidelines (resources/boost/) can auto-generate hook examples or validate hook usage.
    • Laravel Pint: Enforces consistent code style for hook callbacks (e.g., indentation, PSR-12).
    • GitLab CI: Pre-commit hooks can validate hook naming conventions (e.g., kebab-case).

Migration Path

Current Pattern Migration Strategy
Observers Replace Observing with addAction('model.saved', ...) for simpler callbacks.
Middleware Use addAction('kernel.handle', ...) for HTTP-level hooks (but middleware is still better for auth/CSRF).
Service Container Bindings Replace bind() for dynamic behavior with addFilter('value.transform', ...).
Custom Events Convert to hooks if events are synchronous and modular (e.g., Event::dispatch()doAction('event.name')).
View Composers Replace with @action('view.render', $data) in Blade for dynamic view logic.

Compatibility

  • Laravel 10/11: Fully supported; test with Laravel 12 if adopting early.
  • PHP 8.2+: Requires named parameters and first-class callable syntax.
  • Blade: Works with Laravel’s default Blade compiler; no template engine conflicts.
  • Package Discovery: Auto-registers in Laravel 5.5+; manual setup required for older versions.

Sequencing

  1. Phase 1: Core Integration
    • Install via Composer and verify auto-discovery (no config/app.php edits).
    • Replace 1–2 critical observers/events with hooks to validate performance.
  2. Phase 2: Modular Adoption
    • Migrate third-party packages to use hooks for extensibility (e.g., addAction('package.event')).
    • Introduce Blade directives in templates for dynamic content (e.g., @filter('user.display_name', $user)).
  3. Phase 3: Advanced Patterns
    • Implement priority-based workflows (e.g., addAction('order.process', ..., 5) for early validation).
    • Use removal APIs (removeAction) to clean up callbacks in queued jobs or API gateways.
  4. Phase 4: Tooling
    • Integrate Laravel Pint for callback formatting.
    • Add AI guidelines to auto-suggest hook names or validate usage.

Operational Impact

Maintenance

  • Callback Management:
    • Pros: Centralized hook registry (e.g., HooksServiceProvider) makes it easy to list all hooks (Action::getHooks() if exposed).
    • Cons: No built-in hook documentation system (consider Laravel’s php artisan make:hook or a custom hooks.json manifest).
  • Deprecation:
    • Use Laravel’s deprecated() in callbacks to warn users before removing hooks.
    • Example:
      addAction('legacy.hook', function () {
          deprecated('legacy.hook', '2025-01-01');
          // ...
      });
      
  • Testing:
    • Mock hooks in unit tests using Action::fake() (if supported) or partial mocking.
    • Example:
      Action::shouldReceive('do')->with('test.hook')->once();
      

Support

  • Debugging:
    • Log hook execution with doAction('debug.hook', $data) and a dedicated DebugAction listener.
    • Use Laravel’s tap() to inspect filter values:
      $value = applyFilters('data.process', $value)->tap(fn ($v) => logger("Filtered: $v"));
      
  • Performance Profiling:
    • Identify slow hooks with Xdebug or tighten/laravel-query-cache.
    • Example: Profile doAction('user.load') with 50+ callbacks.
  • Common Issues:
    • Callback not firing? Verify hook name spelling and priority conflicts.
    • Memory leaks? Use removeAllActions() in terminating middleware or queue workers.

Scaling

  • Horizontal Scaling:
    • Hooks are stateless (callbacks are executed per request), so they scale with Laravel’s queue workers and load balancers.
    • Caution: Avoid global state in callbacks (e.g., static variables).
  • Vertical Scaling:
    • High callback counts (e.g., 1K+ per hook) may impact performance. Mitigate with:
      • Priority tiers (e.g., `0–
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor