UNPKG

4.27 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/fold
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.Registrar = void 0;
12const path_1 = require("path");
13const utils_1 = require("@poppinss/utils");
14const helpers_1 = require("@poppinss/utils/build/helpers");
15/**
16 * Registrar is used to register and boot the providers
17 */
18class Registrar {
19 constructor(providerConstructorParams, basePath) {
20 this.providerConstructorParams = providerConstructorParams;
21 this.basePath = basePath;
22 /**
23 * The first level of provider paths provided to the registrar
24 */
25 this.providersPaths = [];
26 /**
27 * An array of loaded providers. Their can be more providers than the
28 * `_providersPaths` array, since each provider can provide it's
29 * own sub providers
30 */
31 this.providers = [];
32 /**
33 * Method to instantiate provider instances. One can also defined
34 * a custom instantiater function
35 */
36 this.providersInstantiater = (provider) => new provider(...this.providerConstructorParams);
37 /**
38 * Whether or not the providers can be collected
39 */
40 this.collected = false;
41 }
42 /**
43 * Load the provider by requiring the file from the disk
44 * and instantiate it. If ioc container is using ES6
45 * imports, then default exports are handled
46 * automatically.
47 */
48 async loadProvider(providerPath, basePath) {
49 providerPath = this.basePath
50 ? (0, helpers_1.resolveFrom)(basePath || this.basePath, providerPath)
51 : providerPath;
52 const provider = (0, utils_1.esmRequire)(providerPath);
53 if (typeof provider !== 'function') {
54 throw new utils_1.Exception(`"${providerPath}" provider must use export default statement`);
55 }
56 return {
57 provider: this.providersInstantiater(provider),
58 resolvedPath: (0, path_1.dirname)(providerPath),
59 };
60 }
61 /**
62 * Loop's over an array of provider paths and pushes them to the
63 * `providers` collection. This collection is later used to
64 * register and boot providers
65 */
66 async collect(providerPaths, basePath) {
67 for (let providerPath of providerPaths) {
68 const { provider, resolvedPath } = await this.loadProvider(providerPath, basePath);
69 this.providers.push(provider);
70 if (provider.provides) {
71 await this.collect(provider.provides, resolvedPath);
72 }
73 }
74 }
75 /**
76 * Register an array of provider paths
77 */
78 useProviders(providersPaths, callback) {
79 this.providersPaths = providersPaths;
80 if (typeof callback === 'function') {
81 this.providersInstantiater = callback;
82 }
83 return this;
84 }
85 /**
86 * Register all the providers by instantiating them and
87 * calling the `register` method.
88 *
89 * The provider instance will be returned, which can be used
90 * to boot them as well.
91 */
92 async register() {
93 if (this.collected) {
94 return this.providers;
95 }
96 this.collected = true;
97 await this.collect(this.providersPaths);
98 /**
99 * Register collected providers
100 */
101 this.providers.forEach((provider) => {
102 if (typeof provider.register === 'function') {
103 provider.register();
104 }
105 });
106 return this.providers;
107 }
108 /**
109 * Boot all the providers by calling the `boot` method.
110 * Boot methods are called in series.
111 */
112 async boot() {
113 const providers = await this.register();
114 for (let provider of providers) {
115 if (typeof provider.boot === 'function') {
116 await provider.boot();
117 }
118 }
119 }
120 /**
121 * Register an boot providers together.
122 */
123 async registerAndBoot() {
124 const providers = await this.register();
125 await this.boot();
126 return providers;
127 }
128}
129exports.Registrar = Registrar;