From c7f411d199bc0572859101c61ec9582852c4792f Mon Sep 17 00:00:00 2001 From: Cristian Date: Wed, 28 Jun 2023 12:26:25 +0200 Subject: [PATCH] Enhancement: Support main property on factory definition (#201) * support main property on factory definition * tests for supporting main prop for factory * fix ts test --- lib/Loader/FileLoader.js | 2 +- test/Resources-ts/config/main.yml | 7 ++++++- test/Resources-ts/kebab-case-filename-factory.ts | 7 +++++++ test/Resources/config/main.yml | 7 ++++++- test/Resources/kebab-case-filename-factory.js | 7 +++++++ .../lib-ts/Loader/YamlFileLoader.spec.js | 3 +++ .../lib/Loader/YamlFileLoader.spec.js | 3 +++ 7 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 test/Resources-ts/kebab-case-filename-factory.ts create mode 100644 test/Resources/kebab-case-filename-factory.js diff --git a/lib/Loader/FileLoader.js b/lib/Loader/FileLoader.js index 33d8473..746b3fa 100644 --- a/lib/Loader/FileLoader.js +++ b/lib/Loader/FileLoader.js @@ -94,7 +94,7 @@ class FileLoader { if (service.factory.class.includes('@', 0)) { object = new Reference(service.factory.class.slice(1)) } else { - object = this._requireClassNameFromPath(service.factory.class) + object = this._requireClassNameFromPath(service.factory.class, service.factory.main) } const definition = new Definition() diff --git a/test/Resources-ts/config/main.yml b/test/Resources-ts/config/main.yml index 568216c..152a8a3 100644 --- a/test/Resources-ts/config/main.yml +++ b/test/Resources-ts/config/main.yml @@ -8,4 +8,9 @@ services: multipleExports: class: ./../MultipleExports defaultClass: - class: ./../MultipleExportsWithDefault \ No newline at end of file + class: ./../MultipleExportsWithDefault + kebabCaseFilenameFactory: + factory: + class: ./../kebab-case-filename-factory + main: KebabCaseFilenameFactory + method: "create" \ No newline at end of file diff --git a/test/Resources-ts/kebab-case-filename-factory.ts b/test/Resources-ts/kebab-case-filename-factory.ts new file mode 100644 index 0000000..14b6b2e --- /dev/null +++ b/test/Resources-ts/kebab-case-filename-factory.ts @@ -0,0 +1,7 @@ +export class KebabCaseFilenameFactory { + static create(){ + return new KebabCaseFilenameClass(); + } +} + +export class KebabCaseFilenameClass {} \ No newline at end of file diff --git a/test/Resources/config/main.yml b/test/Resources/config/main.yml index 568216c..152a8a3 100644 --- a/test/Resources/config/main.yml +++ b/test/Resources/config/main.yml @@ -8,4 +8,9 @@ services: multipleExports: class: ./../MultipleExports defaultClass: - class: ./../MultipleExportsWithDefault \ No newline at end of file + class: ./../MultipleExportsWithDefault + kebabCaseFilenameFactory: + factory: + class: ./../kebab-case-filename-factory + main: KebabCaseFilenameFactory + method: "create" \ No newline at end of file diff --git a/test/Resources/kebab-case-filename-factory.js b/test/Resources/kebab-case-filename-factory.js new file mode 100644 index 0000000..14b6b2e --- /dev/null +++ b/test/Resources/kebab-case-filename-factory.js @@ -0,0 +1,7 @@ +export class KebabCaseFilenameFactory { + static create(){ + return new KebabCaseFilenameClass(); + } +} + +export class KebabCaseFilenameClass {} \ No newline at end of file diff --git a/test/node-dependency-injection/lib-ts/Loader/YamlFileLoader.spec.js b/test/node-dependency-injection/lib-ts/Loader/YamlFileLoader.spec.js index 67849ff..6637c37 100644 --- a/test/node-dependency-injection/lib-ts/Loader/YamlFileLoader.spec.js +++ b/test/node-dependency-injection/lib-ts/Loader/YamlFileLoader.spec.js @@ -20,6 +20,7 @@ import DecoratingMailerTwo from '../../../Resources-ts/DecoratingMailerTwo' import ChildClass from '../../../Resources-ts/abstract/ChildClass' import Service from '../../../Resources-ts/abstract/Service' import { MultipleExports, ClassOne, ClassTwo } from '../../../Resources-ts/MultipleExports' +import { KebabCaseFilenameClass } from '../../../Resources-ts/kebab-case-filename-factory' import DefaultClass from '../../../Resources-ts/MultipleExportsWithDefault' import { NamedService } from '../../../Resources-ts/NamedService' import RepositoryManager from '../../../Resources-ts/RepositoryManager' @@ -354,11 +355,13 @@ describe('YamlFileLoaderTS', () => { const two = container.get('classTwo') const multipleExports = container.get('multipleExports') const defaultClass = container.get('defaultClass') + const kebabCaseFilenameFactory = container.get('kebabCaseFilenameFactory') // Assert. assert.instanceOf(one, ClassOne) assert.instanceOf(two, ClassTwo) assert.instanceOf(defaultClass, DefaultClass) + assert.instanceOf(kebabCaseFilenameFactory, KebabCaseFilenameClass) return assert.instanceOf(multipleExports, MultipleExports) }) diff --git a/test/node-dependency-injection/lib/Loader/YamlFileLoader.spec.js b/test/node-dependency-injection/lib/Loader/YamlFileLoader.spec.js index 5679cd2..57e4c7a 100644 --- a/test/node-dependency-injection/lib/Loader/YamlFileLoader.spec.js +++ b/test/node-dependency-injection/lib/Loader/YamlFileLoader.spec.js @@ -21,6 +21,7 @@ import ChildClass from '../../../Resources/abstract/ChildClass' import ChildClassTwo from '../../../Resources/abstract/ChildClassTwo' import Service from '../../../Resources/abstract/Service' import { MultipleExports, ClassOne, ClassTwo } from '../../../Resources/MultipleExports' +import { KebabCaseFilenameClass } from '../../../Resources/kebab-case-filename-factory'; import DefaultClass from '../../../Resources/MultipleExportsWithDefault' import { NamedService } from '../../../Resources/NamedService' import RepositoryManager from '../../../Resources/RepositoryManager' @@ -347,11 +348,13 @@ describe('YamlFileLoader', () => { const two = container.get('classTwo') const multipleExports = container.get('multipleExports') const defaultClass = container.get('defaultClass') + const kebabCaseFilenameFactory = container.get('kebabCaseFilenameFactory') // Assert. assert.instanceOf(one, ClassOne) assert.instanceOf(two, ClassTwo) assert.instanceOf(defaultClass, DefaultClass) + assert.instanceOf(kebabCaseFilenameFactory, KebabCaseFilenameClass) return assert.instanceOf(multipleExports, MultipleExports) })