Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rhysv96 opened this issue Jun 19, 2024 · 1 comment · Fixed by #107

Comments

@rhysv96
Copy link

rhysv96 commented Jun 19, 2024

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
Copy link
Member

timacdonald commented Jun 20, 2024

Thanks for raising this. I've just opened a PR, #107, to address this one. I will close this and we can follow along with the PR.

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 a pull request may close this issue.

2 participants