1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | exports.Registrar = void 0;
|
12 | const path_1 = require("path");
|
13 | const utils_1 = require("@poppinss/utils");
|
14 | const helpers_1 = require("@poppinss/utils/build/helpers");
|
15 |
|
16 |
|
17 |
|
18 | class Registrar {
|
19 | constructor(providerConstructorParams, basePath) {
|
20 | this.providerConstructorParams = providerConstructorParams;
|
21 | this.basePath = basePath;
|
22 | |
23 |
|
24 |
|
25 | this.providersPaths = [];
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 | this.providers = [];
|
32 | |
33 |
|
34 |
|
35 |
|
36 | this.providersInstantiater = (provider) => new provider(...this.providerConstructorParams);
|
37 | |
38 |
|
39 |
|
40 | this.collected = false;
|
41 | }
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
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 |
|
63 |
|
64 |
|
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 |
|
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 |
|
87 |
|
88 |
|
89 |
|
90 |
|
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 |
|
100 |
|
101 | this.providers.forEach((provider) => {
|
102 | if (typeof provider.register === 'function') {
|
103 | provider.register();
|
104 | }
|
105 | });
|
106 | return this.providers;
|
107 | }
|
108 | |
109 |
|
110 |
|
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 |
|
122 |
|
123 | async registerAndBoot() {
|
124 | const providers = await this.register();
|
125 | await this.boot();
|
126 | return providers;
|
127 | }
|
128 | }
|
129 | exports.Registrar = Registrar;
|