zazoomauro / node-dependency-injection

The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
https://github.com/zazoomauro/node-dependency-injection/wiki
MIT License
281 stars 34 forks source link

How to register config parameters #83

Closed alberto-leon-crespo closed 6 years ago

alberto-leon-crespo commented 6 years ago

I load a cnfig service yaml file. I have a instance to ContainerBuilder and i need to register config params before load yamel file. When i compile container y and put in my services.yaml file references to config params y get undefined. I register service like this:

services:
    bbdd.service:
        class: '../../dist/services/bbdd.service'
        arguments: ["%BBDD_HOST%","%BBDD_USER%","%BBDD_PASSWORD%","%BBDD_SCHEMA%","%BBDD_PORT%","%BBDD_DRIVER%"]

And i prepare container like this;

private loadDependencyInjection(){
        let container = new ContainerBuilder();
        let loader = new YamlFileLoader(container);
        for(let key of Object.keys(process.env)){
            container.setParameter(null, key, process.env[key]);
        }
        loader.load(this.fixRoute + '/config/services.yaml');
        container.compile();
        this.container = container;
}

What is wromg?

zazoomauro commented 6 years ago

@alberto-leon-crespo How are you defining parameters? Have you checked the documentation about this? https://github.com/zazoomauro/node-dependency-injection/wiki/Parameters

// add a new parameter
container.setParameter('mailer.transport', 'sendmail')

in your case would be something like:

for(let key of Object.keys(process.env)){
  container.setParameter(key, process.env[key])
}
alberto-leon-crespo commented 6 years ago

I need to use this parameters in yaml config file. I can get this parameters from controller but i cant pass registered parameters in the container in service arguments.

container.setParameter especify a first argument called "nameSpaceURI"

alberto-leon-crespo commented 6 years ago

I solve the problem, process.env variable dont register fine variables in my express application. Fix it