Skip to content

Support attributes to configure indices/indexes #387

@FluffyDiscord

Description

@FluffyDiscord

Description
Add modern approach to defining index indices using PHP attributes. Symfony is pushing attributes quite a lot and we all love how convenient they are and can't get enough of them!

Since we are already using Symfony Serializer, lets simply map the YAML configuration:

Basic example

use Meilisearch\Bundle\Entity\Attribute as Meilisearch;
use Symfony\Component\Serializer\Attribute as Serializer;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[Meilisearch\Index(groups: 'my_meilisearch_group'))]
class Post {    
    #[Serializer\Groups('my_meilisearch_group')]
    private ?string $content = null
}

Where the Index could look like this:

// single indices entry from the config
#[\Attribute(Attribute::TARGET_CLASS)]
readonly class Index
{
    public function __construct(
        // replacement for 'name', could be inferred from the entity short name later on
        // when configuring, like 'new ReflectionClass($entity)->getShortName()'
        public ?string $name = null,

        // replacement for 'enable_serializer_groups'
        public bool $useSerializer = false,

        // if not null, set 'enable_serializer_groups' to 'true'
        // and 'serializer_groups' to the value
        public array|string|null $groups = null,
       
        // replaces 'settings'
        public $settings = [],
    )
    {
    }
}

You could then use $container->registerAttributeForAutoconfiguration(), which is available since forever now (Symfony 5.4) to add definitions to your services.

Other
YAML... Yuck!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions