Centralize configure-able factory classes
This commit is contained in:
@@ -1,74 +1,23 @@
|
||||
import {
|
||||
AbstractFactory,
|
||||
Container,
|
||||
DependencyRequirement,
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
Instantiable, FactoryProducer, getPropertyInjectionMetadata,
|
||||
} from '../../../di'
|
||||
import {Collection, ErrorWithContext} from '../../../util'
|
||||
import {Config} from '../../../service/Config'
|
||||
import {Instantiable, FactoryProducer} from '../../../di'
|
||||
import {ClientRepository} from '../types'
|
||||
import {ConfigClientRepository} from './ConfigClientRepository'
|
||||
import {ConfiguredSingletonFactory} from '../../../di/factory/ConfiguredSingletonFactory'
|
||||
|
||||
/**
|
||||
* A dependency injection factory that matches the abstract ClientRepository class
|
||||
* and produces an instance of the configured repository driver implementation.
|
||||
*/
|
||||
@FactoryProducer()
|
||||
export class ClientRepositoryFactory extends AbstractFactory<ClientRepository> {
|
||||
protected get config(): Config {
|
||||
return Container.getContainer().make<Config>(Config)
|
||||
export class ClientRepositoryFactory extends ConfiguredSingletonFactory<ClientRepository> {
|
||||
protected getConfigKey(): string {
|
||||
return 'oauth2.repository.client'
|
||||
}
|
||||
|
||||
produce(): ClientRepository {
|
||||
return new (this.getClientRepositoryClass())()
|
||||
protected getDefaultImplementation(): Instantiable<ClientRepository> {
|
||||
return ConfigClientRepository
|
||||
}
|
||||
|
||||
match(something: unknown): boolean {
|
||||
return something === ClientRepository
|
||||
}
|
||||
|
||||
getDependencyKeys(): Collection<DependencyRequirement> {
|
||||
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getClientRepositoryClass())
|
||||
if ( meta ) {
|
||||
return meta
|
||||
}
|
||||
return new Collection<DependencyRequirement>()
|
||||
}
|
||||
|
||||
getInjectedProperties(): Collection<PropertyDependency> {
|
||||
const meta = new Collection<PropertyDependency>()
|
||||
let currentToken = this.getClientRepositoryClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
currentToken = Object.getPrototypeOf(currentToken)
|
||||
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
|
||||
|
||||
return meta
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the instantiable class of the configured client repository backend.
|
||||
* @protected
|
||||
* @return Instantiable<ClientRepository>
|
||||
*/
|
||||
protected getClientRepositoryClass(): Instantiable<ClientRepository> {
|
||||
const ClientRepositoryClass = this.config.get('oauth2.repository.client', ConfigClientRepository)
|
||||
|
||||
if ( !isInstantiable(ClientRepositoryClass) || !(ClientRepositoryClass.prototype instanceof ClientRepository) ) {
|
||||
const e = new ErrorWithContext('Provided client repository class does not extend from @extollo/lib.ClientRepository')
|
||||
e.context = {
|
||||
configKey: 'oauth2.repository.client',
|
||||
class: ClientRepositoryClass.toString(),
|
||||
}
|
||||
}
|
||||
|
||||
return ClientRepositoryClass
|
||||
protected getAbstractImplementation(): any {
|
||||
return ClientRepository
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user