DoctrineMongoDBBundle icon indicating copy to clipboard operation
DoctrineMongoDBBundle copied to clipboard

Dependency Injection and ODM Filter

Open t-veron opened this issue 6 years ago • 4 comments

Hello,

Did you plan to add the possibility to inject services in filters?

In fact, actually we can only inject static parameters.

This bundle MongodbOdmFiltersBundle add this functionnality.

t-veron avatar Mar 19 '19 15:03 t-veron

This makes sense, however given latest trends I think we should either allow pointing to a service or somehow discover the filters and autoconfigure them. The first option seems to be easier and leaves the door opened for the 2nd one so I think it might be better. So instead of injecting services manually like in the bundle you've linked we could leverage autowiring and start with

filters:
    Some\Filter:
        class: Some\Filter
        service: true

aiming to have simpler construct in the future for enabled filters.

filters:
    Some\Filter: ~

This however would require reworking a bit filters to accept instance of a filter instead of class name.

Having said that, injecting services as parameters might be easier for first step, a PR will be appreciated!

malarzm avatar Mar 19 '19 19:03 malarzm

I'm agree with you.

However I analysed the code of doctrine/mongodb-odm and we can't use directly dependancy injection because they create new objects from constructor call's.

  public function enable(string $name) : BsonFilter
    {
        if (! $this->has($name)) {
            throw new InvalidArgumentException("Filter '" . $name . "' does not exist.");
        }
        if (! $this->isEnabled($name)) {
            $filterClass      = $this->config->getFilterClassName($name);
            $filterParameters = $this->config->getFilterParameters($name);
            **$filter           = new $filterClass($this->dm);**
            foreach ($filterParameters as $param => $value) {
                $filter->setParameter($param, $value);
            }
            $this->enabledFilters[$name] = $filter;
        }
        return $this->enabledFilters[$name];
    }

t-veron avatar Mar 20 '19 09:03 t-veron

Yeah, I realized that by the time I was finishing my comment :)

Having said that, injecting services as parameters might be easier for first step, a PR will be appreciated!

malarzm avatar Mar 20 '19 09:03 malarzm

I've created https://github.com/doctrine/mongodb-odm/issues/1986 to track the refactoring of the filter logic to allow better configuration. Once this has been implemented in ODM we can then talk about using auto configuration and tags on services to tie filters to document managers 👍

alcaeus avatar Mar 29 '19 06:03 alcaeus