doctrine-extensions/DoctrineExtensions

Sluggable handlers (only available in annotations)

w-ap-admin opened this issue · 6 comments

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 :-)

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.

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

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

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.

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.

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.

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