Files
lib/src/views/ViewEngineFactory.ts

80 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-03-08 18:07:55 -06:00
import {
AbstractFactory,
Container,
DependencyRequirement,
PropertyDependency,
isInstantiable,
DEPENDENCY_KEYS_METADATA_KEY,
2021-06-02 22:36:25 -05:00
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, StaticClass, Instantiable,
} from '../di'
import {Collection, ErrorWithContext} from '../util'
import {Logging} from '../service/Logging'
import {Config} from '../service/Config'
import {ViewEngine} from './ViewEngine'
import {PugViewEngine} from './PugViewEngine'
2021-03-08 18:07:55 -06:00
2021-03-25 08:50:13 -05:00
/**
* Dependency factory whose token matches the abstract ViewEngine class, but produces
* a particular ViewEngine implementation based on the configuration.
*/
2021-06-02 22:36:25 -05:00
export class ViewEngineFactory extends AbstractFactory<ViewEngine> {
2021-03-08 18:07:55 -06:00
protected readonly logging: Logging
2021-06-02 22:36:25 -05:00
2021-03-08 18:07:55 -06:00
protected readonly config: Config
constructor() {
super({})
this.logging = Container.getContainer().make<Logging>(Logging)
this.config = Container.getContainer().make<Config>(Config)
}
2021-06-02 22:36:25 -05:00
produce(): ViewEngine {
return new (this.getViewEngineClass())()
2021-03-08 18:07:55 -06:00
}
2021-06-02 22:36:25 -05:00
match(something: unknown): boolean {
2021-03-08 18:07:55 -06:00
return something === ViewEngine
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getViewEngineClass())
2021-06-02 22:36:25 -05:00
if ( meta ) {
return meta
}
2021-03-08 18:07:55 -06:00
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.getViewEngineClass()
do {
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
2021-06-02 22:36:25 -05:00
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
2021-03-08 18:07:55 -06:00
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
2021-03-25 08:50:13 -05:00
/**
* Using the config, get the implementation of the ViewEngine that should be used in the application.
* @protected
*/
2021-06-02 22:36:25 -05:00
protected getViewEngineClass(): StaticClass<ViewEngine, Instantiable<ViewEngine>> {
2021-03-08 18:07:55 -06:00
const ViewEngineClass = this.config.get('server.view_engine.driver', PugViewEngine)
if ( !isInstantiable(ViewEngineClass) || !(ViewEngineClass.prototype instanceof ViewEngine) ) {
2021-06-02 22:36:25 -05:00
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.ViewEngine')
2021-03-08 18:07:55 -06:00
e.context = {
2021-06-02 22:36:25 -05:00
configKey: 'server.view_engine.driver',
2021-03-08 18:07:55 -06:00
class: ViewEngineClass.toString(),
}
}
return ViewEngineClass
}
}