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

Dependency Injection and ODM Filter #547

Open
t-veron opened this issue Mar 19, 2019 · 4 comments
Open

Dependency Injection and ODM Filter #547

t-veron opened this issue Mar 19, 2019 · 4 comments
Labels
Projects

Comments

@t-veron
Copy link

t-veron commented Mar 19, 2019

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.

@malarzm
Copy link
Member

malarzm commented Mar 19, 2019

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!

@t-veron
Copy link
Author

t-veron commented Mar 20, 2019

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];
    }

@malarzm
Copy link
Member

malarzm commented Mar 20, 2019

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!

@alcaeus
Copy link
Member

alcaeus commented Mar 29, 2019

I've created doctrine/mongodb-odm#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 alcaeus added this to 4.x in Roadmap Mar 29, 2019
@IonBazan IonBazan changed the title Dependancy Injection and ODM Filter Dependency Injection and ODM Filter Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

No branches or pull requests

3 participants