Skip to content

Conversation

@binaryfire
Copy link
Contributor

This PR ports Laravel's #[UseEloquentBuilder] attribute to Hypervel, allowing models to declaratively specify a custom Eloquent builder class.

Summary

  • Adds #[UseEloquentBuilder] attribute for declaring custom builder classes on models
  • Modifies Model::newModelBuilder() to check for the attribute and use custom builders
  • Adds resolveCustomBuilderClass() method with static caching for performance

Usage

use Hypervel\Database\Eloquent\Attributes\UseEloquentBuilder;
use Hypervel\Database\Eloquent\Model;

#[UseEloquentBuilder(UserBuilder::class)]
class User extends Model
{
    // ...
}

// Now User::query() returns a UserBuilder
$builder = User::query(); // instanceof UserBuilder

Changes

New Files

  • src/core/src/Database/Eloquent/Attributes/UseEloquentBuilder.php - The attribute class
  • tests/Core/Database/Eloquent/UseEloquentBuilderTest.php - Tests (8 tests, 13 assertions)

Modified Files

  • src/core/src/Database/Eloquent/Model.php:
    • Added $resolvedBuilderClasses static cache
    • Modified newModelBuilder() to check for custom builder attribute
    • Added resolveCustomBuilderClass() method

How It Works

  1. When newModelBuilder() is called on a model, it first checks the static cache
  2. If not cached, resolveCustomBuilderClass() reads the #[UseEloquentBuilder] attribute via reflection
  3. If a custom builder class is found and is a subclass of Builder, it's used
  4. Otherwise, falls back to the default Hypervel\Database\Eloquent\Builder
  5. Results are cached per model class to avoid repeated reflection calls

Notes

  • PHP attributes are not inherited - child classes need their own #[UseEloquentBuilder] attribute if they want a custom builder
  • This follows Laravel's implementation pattern

Ports Laravel's #[UseEloquentBuilder] attribute to Hypervel, allowing models
to declaratively specify a custom Eloquent builder class.

- Add UseEloquentBuilder attribute class
- Modify Model::newModelBuilder() to check for custom builder attribute
- Add resolveCustomBuilderClass() method with static caching
- Add tests (8 tests, 13 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