diff --git a/lib/Autowire.js b/lib/Autowire.js index 18c356a..a38e5ee 100644 --- a/lib/Autowire.js +++ b/lib/Autowire.js @@ -4,7 +4,6 @@ import json5 from 'json5' import { parse } from '@typescript-eslint/typescript-estree' import Definition from './Definition' import Reference from './Reference' -import ServiceFile from './ServiceFile' import AutowireIdentifier from './AutowireIdentifier' import ContainerDefaultDirMustBeSet from './Exception/ContainerDefaultDirMustBeSet' import PassConfig from './PassConfig' @@ -29,6 +28,7 @@ export default class Autowire { } catch (e) { this._tsConfigPaths = null } + this._container.autowire = this } _ensureContainerIsValidForAutowire (container) { @@ -120,9 +120,6 @@ export default class Autowire { promises.push(this._executeFilePath(filePath)) } await Promise.all(promises) - if (this._serviceFile instanceof ServiceFile) { - await this._serviceFile.generateFromContainer(this._container) - } this._container.addCompilerPass( new AutowireOverridePass(), PassConfig.TYPE_BEFORE_OPTIMIZATION diff --git a/lib/ContainerBuilder.js b/lib/ContainerBuilder.js index b5e5e66..5c09001 100644 --- a/lib/ContainerBuilder.js +++ b/lib/ContainerBuilder.js @@ -12,6 +12,7 @@ import WrongDefinitionException from './Exception/WrongDefinitionException' import FrozenContainerException from './Exception/FrozenContainerException' import RootDirectoryNotFound from './Exception/RootDirectoryNotFound' import RootDirectoryMustBeAbsolute from './Exception/RootDirectoryMustBeAbsolute' +import ServiceFile from './ServiceFile' class ContainerBuilder { /** @@ -32,6 +33,18 @@ class ContainerBuilder { this._instanceManager = undefined this._containerReferenceAsService = containerReferenceAsService this._defaultDir = defaultDir + this._autowire = null + } + + /** + * @param {Autowire} autowire + */ + set autowire (autowire) { + this._autowire = autowire + } + + get autowire () { + return this._autowire } ensureDirectoryExists (directory) { @@ -163,6 +176,9 @@ class ContainerBuilder { async compile () { await new Compiler(this).run() + if (this._autowire?.serviceFile instanceof ServiceFile) { + await this._autowire.serviceFile.generateFromContainer(this) + } } /** diff --git a/test/Resources-ts/Autowire-Override/config/services-noimports.yaml b/test/Resources-ts/Autowire-Override/config/services-noimports.yaml new file mode 100644 index 0000000..da42c1f --- /dev/null +++ b/test/Resources-ts/Autowire-Override/config/services-noimports.yaml @@ -0,0 +1,14 @@ +services: + App.FooBarAutowireOverride: + class: './FooBarAutowireOverride' + override_arguments: + - '@CiAdapter' + - '@SomeService' + - '@AnotherService' + + App.AnotherFooBarAutowireOverride: + class: './AnotherFooBarAutowireOverride' + override_arguments: + - '@FooAdapter' + - '@SomeService' + - '@AnotherService' diff --git a/test/node-dependency-injection/lib-ts/Autowire.spec.js b/test/node-dependency-injection/lib-ts/Autowire.spec.js index 4f5f5e0..f81a070 100644 --- a/test/node-dependency-injection/lib-ts/Autowire.spec.js +++ b/test/node-dependency-injection/lib-ts/Autowire.spec.js @@ -38,51 +38,121 @@ describe('AutowireTS', () => { const excludedServiceMessage = 'The service ExcludedService is not registered' const inFolderExcludedMessage = 'The service InFolderExcludedService is not registered' - it("should not override single class with autowiring if not exists", async () => { - const configFile = path.join( - __dirname, - '..', - '..', - resourcesTsFolder, - 'Autowire-Override', - 'config', - 'services-not-exists.yaml' - ) - const cb = new ContainerBuilder() - const loader = new YamlFileLoader(cb) - await loader.load(configFile) - await cb.compile() + describe("Autowiring override arguments", () => { + const folder = 'Autowire-Override'; + + it("should override arguments be visible after compile container and dump file", async () => { + // Arrange. + const configFile = path.join( + __dirname, + '..', + '..', + resourcesTsFolder, + folder, + 'config', + 'services-noimports.yaml' + ) + const dir = path.join(__dirname, '..', '..', resourcesTsFolder, folder, 'src') + const container = new ContainerBuilder(false, dir) + const loader = new YamlFileLoader(container) + const autowire = new Autowire(container) + const dumpPath = `${dumpServicesPath}-override.yaml` + autowire.serviceFile = new ServiceFile(dumpPath) + await loader.load(configFile) + await autowire.process() + await container.compile() + + const containerFromDump = new ContainerBuilder(false, dir) + const loaderFromDump = new YamlFileLoader(containerFromDump) + + // Act. + await loaderFromDump.load(dumpPath) + + // Act. + const actual = containerFromDump.get(FooBarAutowireOverride) + const actualAnother = containerFromDump.get(AnotherFooBarAutowireOverride) + + // Assert. + assert.equal(actual.getString(), "ci") + assert.equal(actualAnother.getString(), "foo") + }); + + it("should override arguments be visible after compile container", async () => { + // Arrange. + const dir = path.join(__dirname, '..', '..', resourcesTsFolder, folder, 'src') + const container = new ContainerBuilder(false, dir) + const autowire = new Autowire(container) + const loader = new YamlFileLoader(container); + const configFile = path.join( + __dirname, + '..', + '..', + resourcesTsFolder, + folder, + 'config', + 'services-noimports.yaml' + ) + await loader.load(configFile) + await autowire.process() + await container.compile() + + // Act. + const actual = container.get(FooBarAutowireOverride) + const actualAnother = container.get(AnotherFooBarAutowireOverride) + + // Assert. + assert.equal(actual.getString(), "ci") + assert.equal(actualAnother.getString(), "foo") + }); - // Act. - const actual = cb.get(FooBarAutowireOverride) + it("should not override single class with autowiring if not exists", async () => { + // Arrange. + const configFile = path.join( + __dirname, + '..', + '..', + resourcesTsFolder, + folder, + 'config', + 'services-not-exists.yaml' + ) + const cb = new ContainerBuilder() + const loader = new YamlFileLoader(cb) + await loader.load(configFile) + await cb.compile() - // Assert. - assert.isUndefined(actual.adapter) - }); - - it("should override single class with autowiring", async () => { - const configFile = path.join( - __dirname, - '..', - '..', - resourcesTsFolder, - 'Autowire-Override', - 'config', - 'services.yaml' - ) - const cb = new ContainerBuilder() - const loader = new YamlFileLoader(cb) - await loader.load(configFile) - await cb.compile() + // Act. + const actual = cb.get(FooBarAutowireOverride) + + // Assert. + assert.isUndefined(actual.adapter) + }); - // Act. - const actual = cb.get(FooBarAutowireOverride) - const actualAnother = cb.get(AnotherFooBarAutowireOverride) + it("should override single class with autowiring", async () => { + // Arrange. + const configFile = path.join( + __dirname, + '..', + '..', + resourcesTsFolder, + folder, + 'config', + 'services.yaml' + ) + const cb = new ContainerBuilder() + const loader = new YamlFileLoader(cb) + await loader.load(configFile) + await cb.compile() - // Assert. - assert.equal(actual.getString(), "ci") - assert.equal(actualAnother.getString(), "bar") - }); + // Act. + const actual = cb.get(FooBarAutowireOverride) + const actualAnother = cb.get(AnotherFooBarAutowireOverride) + + // Assert. + assert.equal(actual.getString(), "ci") + assert.equal(actualAnother.getString(), "bar") + }); + }) it('should get service file when was properly set', () => { // Arrange. @@ -124,6 +194,7 @@ describe('AutowireTS', () => { const dumpPath = `${dumpServicesPath}.yaml` autowire.serviceFile = new ServiceFile(dumpPath, true) await autowire.process() + await container.compile() const containerDump = new ContainerBuilder(false) const loader = new YamlFileLoader(containerDump) @@ -188,6 +259,7 @@ describe('AutowireTS', () => { await autowire.process() const containerDump = new ContainerBuilder(false, dir) const loader = new JsonFileLoader(containerDump) + await container.compile() // Act. await loader.load(dumpPath) @@ -216,6 +288,7 @@ describe('AutowireTS', () => { await autowire.process() const containerDump = new ContainerBuilder(false, dir) const loader = new JsFileLoader(containerDump) + await container.compile() // Act. await loader.load(dumpPath)