For my PHP-DI definitions I need to make complex calculations beforhand, which are based on other configurations not coming from PHP-DI (those configurations are stored at build time in a variable). As it is harder to explain, than to show, let me give a simplified example which will show, what I would like to do, but will actually not work.
First let’s build the container. The code here is nothing special and shoud work:
<?php
use DIContainer;
use DIContainerBuilder;
class MyContainerBuilder
{
// @param number is the build time (configuration) variable
// I would like to use later
public function build(int $number): Container
{
$builder = new DIContainerBuilder();
$builder->addDefinitions(['number' => $number]);
// In config.php I now want to do some complex calculations
$builder->addDefinitions(__DIR__ . '/config.php');
// here the container may be compiled, such that in production, the complex
// calculations from `config.php` will only be done once (to my understanding)
return $builder->build();
}
}
Now comes the problematic part. The calculation in config.php
<?php
$number = $container->get('number');
// complex calculation:
$number += 1;
return [
'improvedNumber' => $number,
];
This is of course not possible, since the php-di container seems not to be available in the configuration file. Is it still possible to make the value of $number
somehow available in the config.php
?
Sidenote: What I really try to archive is to determine a bunch of services, which shall be injected using php-di. Those services are located in several directories and those directories I must know, when searching for them in the config.php-file.