mirror of
https://github.com/strapi/strapi.git
synced 2025-11-10 23:29:33 +00:00
Init container
This commit is contained in:
parent
71aba0001b
commit
5a276e2b46
@ -8,6 +8,7 @@ const { Database } = require('@strapi/database');
|
|||||||
|
|
||||||
const loadConfiguration = require('./core/app-configuration');
|
const loadConfiguration = require('./core/app-configuration');
|
||||||
const { createHTTPServer } = require('./server');
|
const { createHTTPServer } = require('./server');
|
||||||
|
const { createContainer } = require('./container');
|
||||||
const loadModules = require('./core/load-modules');
|
const loadModules = require('./core/load-modules');
|
||||||
const utils = require('./utils');
|
const utils = require('./utils');
|
||||||
const bootstrap = require('./core/bootstrap');
|
const bootstrap = require('./core/bootstrap');
|
||||||
@ -32,6 +33,8 @@ const LIFECYCLES = {
|
|||||||
|
|
||||||
class Strapi {
|
class Strapi {
|
||||||
constructor(opts = {}) {
|
constructor(opts = {}) {
|
||||||
|
this.container = createContainer(this);
|
||||||
|
|
||||||
this.dir = opts.dir || process.cwd();
|
this.dir = opts.dir || process.cwd();
|
||||||
this.config = loadConfiguration(this.dir, opts);
|
this.config = loadConfiguration(this.dir, opts);
|
||||||
|
|
||||||
|
|||||||
45
packages/core/strapi/lib/container.js
Normal file
45
packages/core/strapi/lib/container.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const createContainer = strapi => {
|
||||||
|
const registerd = new Map();
|
||||||
|
const resolved = new Map();
|
||||||
|
|
||||||
|
return {
|
||||||
|
register(name, resolver) {
|
||||||
|
if (registerd.has(name)) {
|
||||||
|
throw new Error(`Cannot register already registered service ${name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
registerd.set(name, resolver);
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
|
||||||
|
get(name, args) {
|
||||||
|
// TODO: handle singleton vs reinstanciation everytime
|
||||||
|
if (resolved.has(name)) {
|
||||||
|
return resolved.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (registerd.has(name)) {
|
||||||
|
const resolver = registerd.get(name);
|
||||||
|
|
||||||
|
if (typeof resolver === 'function') {
|
||||||
|
resolved.set(name, resolver({ strapi }, args));
|
||||||
|
} else {
|
||||||
|
resolved.set(name, resolver);
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolved.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Could not resovle service ${name}`);
|
||||||
|
},
|
||||||
|
|
||||||
|
// TODO: implement
|
||||||
|
extend() {},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
createContainer,
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user