doctrine-extensions / DoctrineExtensions

Doctrine2 behavioral extensions, Translatable, Sluggable, Tree-NestedSet, Timestampable, Loggable, Sortable
MIT License
4.01k stars 1.26k forks source link

Sluggable handlers (only available in annotations) #2841

Open w-ap-admin opened 2 weeks ago

w-ap-admin commented 2 weeks ago

Hello,

I using doctrine-extension and I wodering why the argument handlers inside @Gedmo\Slug is available in annotations and not in attributes ? It is a technical constraint or could I help with an PR ?

Thank you for anwser.

Have a nice day :-)

mbabker commented 2 weeks ago

When PHP 8.0 released, you couldn't instantiate objects in attributes, so this syntax wasn't valid:

#[Gedmo\Slug(handlers: [new Gedmo\SlugHandler()])]

As a side effect, projects providing annotations that had nested annotations needed to adjust how those nested objects were processed. Usually, that meant promoting those nested objects to the top level, such as:

#[Gedmo\Slug]
#[Gedmo\SlugHandler]

The Doctrine ORM has similar examples of this (compare the @Index annotation with the #[Index] attribute and you'll see a similar change).

One of the features of PHP 8.1 (see https://stitcher.io/blog/php-81-new-in-initializers for more on that) removed that limitation. So, technically, the annotations syntax could be supported in the attributes mapping driver for PHP 8.1 and later, but at this point I personally don't trying to support both options in the attribute driver (both the original implementation and bringing back the annotation-related nested structure) is worth the effort.

nzhiti commented 2 weeks ago

`#[Gedmo\Slug]

[Gedmo\SlugHandler]`

Can slug handler being chained in order to have multiple RelativeSlugHandler ?

mbabker commented 2 weeks ago

Yes, you can configure multiple slug handlers, see this test fixture as an example with both annotations and attributes.

If your question is specifically whether you can have multiple RelativeSlugHandler's with different configurations for one property, I truthfully don't know as I've not personally used that feature.

nzhiti commented 2 weeks ago

Yes, you can configure multiple slug handlers, see this test fixture as an example with both annotations and attributes.

If your question is specifically whether you can have multiple RelativeSlugHandler's with different configurations for one property, I truthfully don't know as I've not personally used that feature.

That's exactly what I want, I'd like the slug being build based on relations fields. And only the last relativeSlugHandler was user to build the slug.

mbabker commented 2 weeks ago

OK, well that's a separate feature request from this item (which is mainly about the config difference between annotations and attributes). On a quick glance, it seems like the mapping driver keys the handlers array by class name, so that would definitely preclude multiple slug handlers of the same type being used on one property. I was going to say maybe there's a way to work around it with a custom chain implementation of the SlugHandlerInterface, but with the way all the existing handlers fetch their data, the idea that I had just won't work for your use case without basically re-implementing the handlers you need.

nzhiti commented 2 weeks ago

OK, well that's a separate feature request from this item (which is mainly about the config difference between annotations and attributes). On a quick glance, it seems like the mapping driver keys the handlers array by class name, so that would definitely preclude multiple slug handlers of the same type being used on one property. I was going to say maybe there's a way to work around it with a custom chain implementation of the SlugHandlerInterface, but with the way all the existing handlers fetch their data, the idea that I had just won't work for your use case without basically re-implementing the handlers you need.

Thank you ill dig into it and keep this post updated