laravel / pennant

A simple, lightweight library for managing feature flags.
https://laravel.com/docs/pennant
MIT License
474 stars 48 forks source link

Feature classes not automatically registered, Documentation error? #92

Closed jeroendelau closed 6 months ago

jeroendelau commented 6 months ago

Pennant Version

1.6.0

Laravel Version

10.46.0

PHP Version

8.1.4

Database Driver & Version

MySql 8.1 Lando

Description

The documentation for features states.

Unlike closure based feature definitions, there is no need to register a class based feature in a service provider.

But they do not behave exactly the same. If you do not Define the feature, it will not show up if you run

# closure based
 $user->features->all();  // => ["my-feature" => false]

# class based
 $user->features->all();  // => []

Steps To Reproduce

Example:

Closure Based

class AppServiceProvider extends ServiceProvider
{
    ...
    public function boot(): void
    {
        Feature::define('new-api', fn (User $user) => match (true) {
            default = true
        });
    }
}

## Results in
dump($user->features()->all());
['new-api' => true];

Class Based

namespace App\Features;

class NewAPi
{
    public string $name = "new-api";

    public function resolve(mixed $scope): mixed
    {
        return true;
    }
}

class AppServiceProvider extends ServiceProvider
{
    ...
    public function boot(): void
    {

    }
}

## Results in
dump($user->features()->all());
[];

Class Based but defined in Service provider

namespace App\Features;

class NewAPi
{
    public string $name = "new-api";

    public function resolve(mixed $scope): mixed
    {
        return true;
    }
}

class AppServiceProvider extends ServiceProvider
{
    ...
    public function boot(): void
    {
        Feature::define(NewAPi::class);
    }
}

## Results in
dump($user->features()->all());
['new-api' => true];;
timacdonald commented 6 months ago

Hey, @jeroendelau, this is known and documented behaviour. You will want to use discovery if you do not want to register classes manually.

Screenshot 2024-03-08 at 14 34 37

See: https://laravel.com/docs/10.x/pennant#retrieving-multiple-features

jeroendelau commented 6 months ago

So sorry, completely missed that.

timacdonald commented 6 months ago

No worries at all :)

mrvnklm commented 2 months ago

I did too, maybe its worth mentioning it in the class-based-features section as it is a requirement for that feature to work.

timacdonald commented 2 months ago

@mrvnklm, you do not need to register class based features; registering them is optional. They register when you use them.