Skip to content

Conversation

@binaryfire
Copy link
Contributor

This PR ports Laravel's #[Boot] and #[Initialize] attributes to Hypervel, allowing trait methods to be marked as boot/initialize methods without requiring the conventional naming.

Summary

  • Adds #[Boot] attribute for marking static methods as boot methods
  • Adds #[Initialize] attribute for marking methods as initialize methods
  • Adds HasBootableTraits trait that overrides bootTraits() to support attributes
  • Modifies Model to use the new HasBootableTraits trait

Usage

#[Boot] Attribute

Traditionally, trait boot methods must be named boot{TraitName}:

trait SoftDeletes
{
    public static function bootSoftDeletes(): void
    {
        // Called once during model boot
    }
}

With the #[Boot] attribute, you can name the method anything:

trait SoftDeletes
{
    #[Boot]
    public static function registerSoftDeleteScope(): void
    {
        // Called once during model boot
    }
}

#[Initialize] Attribute

Traditionally, trait initialize methods must be named initialize{TraitName}:

trait HasUuids
{
    public function initializeHasUuids(): void
    {
        // Called on each new model instance
    }
}

With the #[Initialize] attribute, you can name the method anything:

trait HasUuids
{
    #[Initialize]
    public function setupUuidBehavior(): void
    {
        // Called on each new model instance
    }
}

Changes

New Files

  • src/core/src/Database/Eloquent/Attributes/Boot.php - The Boot attribute class
  • src/core/src/Database/Eloquent/Attributes/Initialize.php - The Initialize attribute class
  • src/core/src/Database/Eloquent/Concerns/HasBootableTraits.php - Trait providing enhanced bootTraits()
  • tests/Core/Database/Eloquent/Concerns/HasBootableTraitsTest.php - Tests (7 tests, 18 assertions)

Modified Files

  • src/core/src/Database/Eloquent/Model.php - Added HasBootableTraits trait

How It Works

  1. HasBootableTraits overrides Hyperf's bootTraits() method
  2. Uses ReflectionClass to iterate through all methods on the model
  3. For static methods: checks both conventional naming (boot{TraitName}) and #[Boot] attribute
  4. For all methods: checks both conventional naming (initialize{TraitName}) and #[Initialize] attribute
  5. Conventional naming is still fully supported - attributes are an alternative, not a replacement

Notes

  • Boot methods must be static (same as conventional boot methods)
  • Initialize methods are called on each new model instance
  • Both attributes and conventional naming work together

Ports Laravel's #[Boot] and #[Initialize] attributes to Hypervel, allowing
trait methods to be marked as boot/initialize methods without requiring
the conventional `boot{TraitName}` or `initialize{TraitName}` naming.

- Add Boot attribute for static boot methods
- Add Initialize attribute for instance initialize methods
- Add HasBootableTraits trait with enhanced bootTraits() method
- Modify Model to use HasBootableTraits trait
- Add tests (7 tests, 18 assertions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant