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

[QueryBuilder] Allow to provide an "index" of Definitions? #618

Closed
holtkamp opened this issue Aug 28, 2018 · 8 comments
Closed

[QueryBuilder] Allow to provide an "index" of Definitions? #618

holtkamp opened this issue Aug 28, 2018 · 8 comments

Comments

@holtkamp
Copy link
Contributor

When a lot of DIC Definitions are involved, I found it was useful to organize them in separate files, which contain arrays:

//Config1.php
return [
        LoggerInterface::class => \DI\create(Logger::class),
        //...
];

Then a single "index" file can be used to refer the these separate files:

//definitionsFiles.php
return [
    __DIR__ . \DIRECTORY_SEPARATOR . 'Config1.php',
    __DIR__ . \DIRECTORY_SEPARATOR . 'Config2.php',
    __DIR__ . \DIRECTORY_SEPARATOR . 'ConfigN.php',
];

Then this "index" of Definitions can be processed like this:

$definitionsFiles = require 'definitionsFiles.php';
foreach($definitionsFiles as $definitionsFile{
    $containerBuilder->addDefinitions($definitionsFile);
}

Would a PR with a function that accepts such an "index" of Definitions be accepted, or is out of of scope? Something like this:

//QueryBuilder.php
public function addDefinitionsIndex(string $definitionsIndex): self{
    foreach($definitionsIndex as $definitions{
        $containerBuilder->addDefinitions($definitions);
    }
}
@holtkamp holtkamp changed the title Allow to provide an "index" of Definitions [QueryBuilder] Allow to provide an "index" of Definitions? Aug 28, 2018
@mnapoli
Copy link
Member

mnapoli commented Aug 28, 2018

I would rather like to keep the API as small as possible ("don't make me think"). Here we would just avoid a foreach so there doesn't seem to be a lot of added value.

I wonder though if addDefinitions() could be changed to take a list of files/arrays using variadic parameters. That way the API stays the same but it can support lists of definitions. Example of usage:

$builder->addDefinitions(...$definitionsIndex);

What do you think?

@holtkamp
Copy link
Contributor Author

Aah, yeah, variadic parameters would be a great alternative to reach the same goal, nice!

@juliangut
Copy link
Contributor

IMO the way you store your definitions and how you collect them should be out of the container

If you know you're not overriding any definition (DI\add or DI\decorate) you can just do this:

$builder->addDefinitions(\array_merge(...$definitionsIndex));

@holtkamp
Copy link
Contributor Author

@juliangut note that in my example the $definitionsIndex is an array of strings (pointing to the configuration files) and not an array of arrays.

@mnapoli
Copy link
Member

mnapoli commented Aug 29, 2018

IMO the way you store your definitions and how you collect them should be out of the container

Agreed. Although if the variadic things work out that is a very little impact on the API (almost invisible) and easy to maintain so that's why I think "why not".

@juliangut
Copy link
Contributor

@holtkamp that is right, I overlooked that part, anyway that is what I don't think fits the library, checking if passed a string and if so require the file, you can easily decorate builder to do so

@mnapoli just accepting a variadic list of definition arrays is a good addition, it would simplify this code to a single addDefinitions call

@mnapoli
Copy link
Member

mnapoli commented Sep 17, 2018

Done in #627!

@mnapoli mnapoli closed this as completed Sep 17, 2018
@holtkamp
Copy link
Contributor Author

Nice work @juliangut ! Thanks a bunch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants