mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 07:03:38 +00:00
Use a registry for the plugins
This commit is contained in:
parent
575cbbdc7c
commit
8252a51ba5
@ -76,20 +76,9 @@ module.exports = {
|
||||
},
|
||||
|
||||
async plugins(ctx) {
|
||||
try {
|
||||
const plugins = Object.keys(strapi.plugins).reduce((acc, key) => {
|
||||
acc[key] = _.get(strapi.plugins, [key, 'package', 'strapi'], {
|
||||
name: key,
|
||||
});
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
ctx.send({ plugins });
|
||||
} catch (err) {
|
||||
strapi.log.error(err);
|
||||
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
|
||||
}
|
||||
// TODO: use name from plugin package.json info
|
||||
const plugins = _.mapValues(strapi.plugins, (_, key) => ({ name: key }));
|
||||
ctx.send({ plugins });
|
||||
},
|
||||
|
||||
async uninstallPlugin(ctx) {
|
||||
|
||||
@ -11,12 +11,16 @@ module.exports = async (ctx, next) => {
|
||||
return ctx.send({ error: 'contentType.notFound' }, 404);
|
||||
}
|
||||
|
||||
const target = ct.plugin === 'admin' ? strapi.admin : strapi.plugins[ct.plugin];
|
||||
const target = ct.plugin === 'admin' ? strapi.admin : strapi.plugin(ct.plugin);
|
||||
const configPath =
|
||||
ct.plugin === 'admin'
|
||||
? ['server.admin.layout', ct.modelName, 'actions', ctx.request.route.action]
|
||||
: ['plugin', ct.plugin, 'layout', ct.modelName, 'actions', ctx.request.route.action];
|
||||
|
||||
const actionPath = ['config', 'layout', ct.modelName, 'actions', ctx.request.route.action];
|
||||
const actionConfig = strapi.config.get(configPath);
|
||||
|
||||
if (_.has(target, actionPath)) {
|
||||
const [controller, action] = _.get(target, actionPath, []).split('.');
|
||||
if (!_.isNil(actionConfig)) {
|
||||
const [controller, action] = actionConfig.split('.');
|
||||
|
||||
if (controller && action) {
|
||||
return await target.controllers[controller.toLowerCase()][action](ctx);
|
||||
|
||||
@ -7,8 +7,8 @@ const { getService } = require('../utils');
|
||||
module.exports = ({ strapi }) => ({
|
||||
canConfigureContentType({ userAbility, contentType }) {
|
||||
const action = contentTypesUtils.isSingleType(contentType)
|
||||
? 'plugins::ontent-manager.single-types.configure-view'
|
||||
: 'pplugins::ntent-manager.collection-types.configure-view';
|
||||
? 'plugin::content-manager.single-types.configure-view'
|
||||
: 'plugin::content-manager.collection-types.configure-view';
|
||||
|
||||
return userAbility.can(action);
|
||||
},
|
||||
|
||||
@ -33,8 +33,10 @@ const policiesRegistry = require('./core/registries/policies');
|
||||
const middlewaresRegistry = require('./core/registries/middlewares');
|
||||
const controllersRegistry = require('./core/registries/controllers');
|
||||
const modulesRegistry = require('./core/registries/modules');
|
||||
const pluginsRegistry = require('./core/registries/plugins');
|
||||
const createConfigProvider = require('./core/registries/config');
|
||||
const loadPlugins = require('./core/load-plugins');
|
||||
// const { nameToSlug } = require('../../utils/lib');
|
||||
|
||||
const LIFECYCLES = {
|
||||
REGISTER: 'register',
|
||||
@ -53,6 +55,7 @@ class Strapi {
|
||||
this.container.register('middlewares', middlewaresRegistry(this));
|
||||
this.container.register('controllers', controllersRegistry(this));
|
||||
this.container.register('modules', modulesRegistry(this));
|
||||
this.container.register('plugins', pluginsRegistry(this));
|
||||
|
||||
this.isLoaded = false;
|
||||
this.reload = this.reload();
|
||||
@ -82,7 +85,11 @@ class Strapi {
|
||||
}
|
||||
|
||||
plugin(name) {
|
||||
return this.plugins[name];
|
||||
return this.container.get('plugins').get(name);
|
||||
}
|
||||
|
||||
get plugins() {
|
||||
return this.container.get('plugins').getAll();
|
||||
}
|
||||
|
||||
async start() {
|
||||
@ -219,6 +226,14 @@ class Strapi {
|
||||
this.config.set('server.admin', _.merge(this.admin.config, userAdminConfig));
|
||||
}
|
||||
|
||||
async loadPlugins() {
|
||||
const loadedPlugins = await loadPlugins(this);
|
||||
|
||||
for (const pluginName in loadedPlugins) {
|
||||
this.container.get('plugins').add(pluginName, loadedPlugins[pluginName]);
|
||||
}
|
||||
}
|
||||
|
||||
async load() {
|
||||
this.app.use(async (ctx, next) => {
|
||||
if (ctx.request.url === '/_health' && ['HEAD', 'GET'].includes(ctx.request.method)) {
|
||||
@ -229,19 +244,11 @@ class Strapi {
|
||||
}
|
||||
});
|
||||
|
||||
const plugins = await loadPlugins(this);
|
||||
|
||||
this.plugins = {};
|
||||
|
||||
for (const pluginName in plugins) {
|
||||
const plugin = plugins[pluginName];
|
||||
const moduleInstance = this.container.get('modules').add(`plugin::${pluginName}`, plugin);
|
||||
this.plugins[pluginName] = moduleInstance;
|
||||
}
|
||||
await this.loadPlugins();
|
||||
|
||||
const modules = await loadModules(this);
|
||||
|
||||
this.loadAdmin();
|
||||
await this.loadAdmin();
|
||||
|
||||
this.api = modules.api;
|
||||
this.components = modules.components;
|
||||
|
||||
@ -57,7 +57,7 @@ const createModule = (namespace, rawModule, strapi) => {
|
||||
return strapi.container.get('policies').get(`${namespace}.${policyName}`);
|
||||
},
|
||||
get policies() {
|
||||
return strapi.container.get('policies').getAll(namespace);
|
||||
return rawModule.policies;
|
||||
},
|
||||
middleware(middlewareName) {
|
||||
return strapi.container.get('middlewares').get(`${namespace}.${middlewareName}`);
|
||||
|
||||
28
packages/core/strapi/lib/core/registries/plugins.js
Normal file
28
packages/core/strapi/lib/core/registries/plugins.js
Normal file
@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
const { has } = require('lodash/fp');
|
||||
|
||||
const pluginsRegistry = strapi => {
|
||||
const plugins = {};
|
||||
|
||||
return {
|
||||
get(name) {
|
||||
return plugins[name];
|
||||
},
|
||||
getAll() {
|
||||
return plugins;
|
||||
},
|
||||
add(name, pluginConfig) {
|
||||
if (has(name, plugins)) {
|
||||
throw new Error(`Plugin ${name} has already been registered.`);
|
||||
}
|
||||
|
||||
const moduleInstance = strapi.container.get('modules').add(`plugin::${name}`, pluginConfig);
|
||||
plugins[name] = moduleInstance;
|
||||
|
||||
return plugins[name];
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = pluginsRegistry;
|
||||
@ -65,11 +65,10 @@ module.exports = {
|
||||
},
|
||||
|
||||
async getPolicies(ctx) {
|
||||
const policies = _.keys(strapi.plugin('users-permissions').policies);
|
||||
|
||||
ctx.send({
|
||||
policies: _.without(
|
||||
_.keys(strapi.plugin('users-permissions').policies),
|
||||
'plugin::users-permissions.permissions'
|
||||
),
|
||||
policies: _.without(policies, 'permissions'),
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user