laravel / pennant

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

Use name for class based features in the resulting array when using the values function with a class name #106

Closed rhysv96 closed 2 months ago

rhysv96 commented 2 months ago

When calling values, it doesn't use the given name of class-based features when the key given is the class name.

app/Features/FooBar.php

<?php

namespace App\Features;

class FooBar
{
    public string $name = 'foobar';

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

Usage

use App\Features\Foobar;
use Laravel\Pennant\Feature;

// ...

Feature::define(Foobar::class);

Feature::active('foobar'); // true, as expected

Feature::values([ 'foobar' ]); // [ 'foobar' => true ], as expected

Feature::values([ Foobar::class ]); // [ 'App\Features\Foobar' => true ], I would rather this use the given name, 'foobar'

My use case is that I want to use Foobar::class internally. It's easier for other members of the team to quickly jump to the feature and see what it is. However, I also want to dump a whole lot of features out into an API, so the frontend can ingest it, and I want a simpler name to be used there.

A workaround on my end could be to make $name get it's value from a const, and use that const in values

class FooBar
{
    public const NAME = 'foobar';
    public string $name = self::NAME;

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

Feature::define(Foobar::class);

Feature::values([ Foobar::NAME ]); // [ 'foobar' => true ]

But it feels to me that it maybe shouldn't work this way. I'm not sure if it's a bug.

timacdonald commented 2 months ago

Thanks for raising this. I've just opened a PR, https://github.com/laravel/pennant/issues/107, to address this one. I will close this and we can follow along with the PR.