Initial commit
This commit is contained in:
12
di/src/factory/AbstractFactory.ts
Normal file
12
di/src/factory/AbstractFactory.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import {Collection} from '../../../lib/src/collection/Collection.ts'
|
||||
import {DependencyRequirement} from '../type/DependencyRequirement.ts'
|
||||
|
||||
export default abstract class AbstractFactory {
|
||||
protected constructor(
|
||||
protected token: any
|
||||
) {}
|
||||
|
||||
abstract produce(dependencies: any[], parameters: any[]): any
|
||||
abstract match(something: any): boolean
|
||||
abstract get_dependency_keys(): Collection<DependencyRequirement>
|
||||
}
|
||||
28
di/src/factory/Factory.ts
Executable file
28
di/src/factory/Factory.ts
Executable file
@@ -0,0 +1,28 @@
|
||||
import Instantiable from '../type/Instantiable.ts'
|
||||
import { DEPENDENCY_KEYS_METADATA_KEY } from '../type/DependencyKey.ts'
|
||||
import { Reflect } from '../../../lib/src/external/reflect.ts'
|
||||
import { Collection } from '../../../lib/src/collection/Collection.ts'
|
||||
import { DependencyRequirement } from '../type/DependencyRequirement.ts'
|
||||
import AbstractFactory from './AbstractFactory.ts'
|
||||
|
||||
export default class Factory extends AbstractFactory {
|
||||
constructor(
|
||||
protected token: Instantiable<any>
|
||||
) {
|
||||
super(token)
|
||||
}
|
||||
|
||||
produce(dependencies: any[], parameters: any[]) {
|
||||
return new this.token(...dependencies, ...parameters)
|
||||
}
|
||||
|
||||
match(something: any) {
|
||||
return something === this.token || (something.toString && String(something) === this.token.name)
|
||||
}
|
||||
|
||||
get_dependency_keys(): Collection<DependencyRequirement> {
|
||||
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.token)
|
||||
if ( meta ) return meta
|
||||
return new Collection<DependencyRequirement>()
|
||||
}
|
||||
}
|
||||
24
di/src/factory/FunctionFactory.ts
Normal file
24
di/src/factory/FunctionFactory.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import AbstractFactory from './AbstractFactory.ts'
|
||||
import {DependencyRequirement} from '../type/DependencyRequirement.ts'
|
||||
import {Collection} from '../../../lib/src/collection/Collection.ts'
|
||||
|
||||
export default class FunctionFactory extends AbstractFactory {
|
||||
constructor(
|
||||
protected name: string,
|
||||
protected token: () => any,
|
||||
) {
|
||||
super(token)
|
||||
}
|
||||
|
||||
get_dependency_keys(): Collection<DependencyRequirement> {
|
||||
return new Collection<DependencyRequirement>()
|
||||
}
|
||||
|
||||
match(something: any) {
|
||||
return something === this.name
|
||||
}
|
||||
|
||||
produce(dependencies: any[], parameters: any[]): any {
|
||||
return this.token()
|
||||
}
|
||||
}
|
||||
15
di/src/factory/NamedFactory.ts
Normal file
15
di/src/factory/NamedFactory.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import Factory from './Factory.ts'
|
||||
import Instantiable from "../type/Instantiable.ts";
|
||||
|
||||
export default class NamedFactory extends Factory {
|
||||
constructor(
|
||||
protected name: string,
|
||||
protected token: Instantiable<any>,
|
||||
) {
|
||||
super(token)
|
||||
}
|
||||
|
||||
match(something: any) {
|
||||
return something === this.name
|
||||
}
|
||||
}
|
||||
24
di/src/factory/SingletonFactory.ts
Normal file
24
di/src/factory/SingletonFactory.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import Factory from './Factory.ts'
|
||||
import {Collection} from '../../../lib/src/collection/Collection.ts'
|
||||
import {DependencyRequirement} from '../type/DependencyRequirement.ts'
|
||||
|
||||
export default class SingletonFactory extends Factory {
|
||||
constructor(
|
||||
protected token: FunctionConstructor,
|
||||
protected key: string,
|
||||
) {
|
||||
super(token)
|
||||
}
|
||||
|
||||
produce(dependencies: any[], parameters: any[]) {
|
||||
return this.token
|
||||
}
|
||||
|
||||
match(something: any) {
|
||||
return something === this.key
|
||||
}
|
||||
|
||||
get_dependency_keys(): Collection<DependencyRequirement> {
|
||||
return new Collection<DependencyRequirement>()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user