configureInjector()

in src/Dgeni.ts [99:154]


  configureInjector() {

    if ( !this.injector ) {

      // Sort the packages by their dependency - ensures that services and configs are loaded in the
      // correct order
      const packages: Package[] = this.packages = sortByDependency(this.packages, 'namedDependencies');

      // Create a module containing basic shared services
      this.stopOnProcessingError = true;
      const dgeniModule = new di.Module()
        .value('dgeni', this)
        .factory('log', logFactory)
        .factory('getInjectables', getInjectablesFactory);

      // Create the dependency injection container, from all the packages' modules
      const modules = packages.map(pkg => pkg.module);
      modules.unshift(dgeniModule);

      // Create the injector and
      const injector = this.injector = new di.Injector(modules);

      // Apply the config blocks
      packages.forEach((pkg) => pkg.configFns.forEach((configFn) => injector.invoke(configFn)));

      // Get the the processors and event handlers
      const processorMap = {};
      this.handlerMap = {};
      packages.forEach((pkg) => {

        pkg.processors.forEach(function(processorName) {
          const processor = injector.get(processorName);
          // Update the processor's name and package
          processor.name = processorName;
          processor.$package = pkg.name;

          // Ignore disabled processors
          if ( processor.$enabled !== false ) {
            processorMap[processorName] = processor;
          }
        });

        for (const eventName in pkg.handlers) {
          const handlers: Function[] = this.handlerMap[eventName] = (this.handlerMap[eventName] || []);
          pkg.handlers[eventName].forEach(handlerName => handlers.push(injector.get(handlerName)));
        }
      });

      // Once we have configured everything sort the processors.
      // This allows the config blocks to modify the $runBefore and $runAfter properties of processors.
      // (Crazy idea, I know, but useful for things like debugDumpProcessor)
      this.processors = sortByDependency(processorMap, '$runAfter', '$runBefore');
    }

    return this.injector;
  }