Config service with shared data on multiple instances


#1

I have Expressive application running on multiple servers (on each server is one instance). I would like to have an ability of changing (overriding) some options in container config service in my application gui.

I really dont know how to solve this.

I cant create delegator factory - because config is a service
I cant create config aggregator provider - because service container is not built

Help is really appreciated :slight_smile:


#2

I suggest setting environment variables in each server, and then accessing them via getenv() within your configuration files. This will work for any scalar option you have.


#3

I should be more precise in my question. Use case:
My application creates invoices. Default seller info is in config service.
These info is then injected to invoice generator in factory.

$config = [ // default config
    'seller_name' => 'My company ltd.',
    'seller_street' => 'Street 666',
];

InvoiceGeneratorFactory
{
    public function __invoke(ContainerInterface $config)
   {
        $config = $container->get('config');
        return InvoiceGenerator($config['seller_name'], /* ....*/);
   }
}

Wellโ€ฆ and I would like to create seller info form which can than override default seller info provided by config service.


#4

Something like this?

class InvoiceGeneratorFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $config = $container->get('config');

        return InvoiceGenerator($config['seller_name'], /* ....*/);
   }
}
class InvoiceGenerator
{
    /** @var string */
    private $sellerName;

    public function __construct(string $sellerName, /* .... */)
    {
        $this->sellerName = $sellerName;
    }

    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ) : ResponseInterface {
        $query = $request->getQueryParams();
        
        $sellerName = $query['seller_name'] ?? $this->sellerName);

        /* generate invoice .... */
    }
}

#5

I think config shall be used for immutable or default values only. Why not go for setter method? InvoiceGenerator is obviously not a value object, so why would you not want to have a setter for seller?