fix: admin extender execution order

This commit is contained in:
Sami Mazouz
2025-04-11 15:26:57 +01:00
parent dbaf77d0f0
commit da4913902e
3 changed files with 64 additions and 53 deletions

View File

@ -129,12 +129,14 @@ export default class AdminApplication extends Application {
this.route = (Object.getPrototypeOf(Object.getPrototypeOf(this)) as Application).route.bind(this); this.route = (Object.getPrototypeOf(Object.getPrototypeOf(this)) as Application).route.bind(this);
} }
protected beforeMount(): void { protected runBeforeMount(): void {
BasicsPage.register(); BasicsPage.register();
AppearancePage.register(); AppearancePage.register();
MailPage.register(); MailPage.register();
AdvancedPage.register(); AdvancedPage.register();
PermissionsPage.register(); PermissionsPage.register();
super.runBeforeMount();
} }
/** /**

View File

@ -286,6 +286,8 @@ export default class Application {
private handledErrors: { extension: null | string; errorId: string; error: any }[] = []; private handledErrors: { extension: null | string; errorId: string; error: any }[] = [];
private beforeMounts: (() => void)[] = [];
public load(payload: Application['data']) { public load(payload: Application['data']) {
this.data = payload; this.data = payload;
this.translator.setLocale(payload.locale); this.translator.setLocale(payload.locale);
@ -326,7 +328,7 @@ export default class Application {
this.session = new Session(this.store.getById<User>('users', String(this.data.session.userId)) ?? null, this.data.session.csrfToken); this.session = new Session(this.store.getById<User>('users', String(this.data.session.userId)) ?? null, this.data.session.csrfToken);
this.beforeMount(); this.runBeforeMount();
this.mount(); this.mount();
@ -335,8 +337,13 @@ export default class Application {
caughtInitializationErrors.forEach((handler) => handler()); caughtInitializationErrors.forEach((handler) => handler());
} }
protected beforeMount(): void { public beforeMount(callback: () => void) {
// ... this.beforeMounts.push(callback);
}
protected runBeforeMount(): void {
this.beforeMounts.forEach((callback) => callback());
this.beforeMounts = [];
} }
public bootExtensions(extensions: Record<string, { extend?: IExtender[] }>) { public bootExtensions(extensions: Record<string, { extend?: IExtender[] }>) {

View File

@ -124,62 +124,64 @@ export default class Admin implements IExtender<AdminApplication> {
} }
extend(app: AdminApplication, extension: IExtensionModule) { extend(app: AdminApplication, extension: IExtensionModule) {
app.registry.for(this.context || extension.name); app.beforeMount(() => {
app.registry.for(this.context || extension.name);
this.settings.forEach(({ setting, customSetting, priority }) => { this.settings.forEach(({ setting, customSetting, priority }) => {
const settingConfig = setting ? setting() : customSetting!; const settingConfig = setting ? setting() : customSetting!;
if (settingConfig) { if (settingConfig) {
app.registry.registerSetting(settingConfig, priority); app.registry.registerSetting(settingConfig, priority);
}
});
this.settingReplacements.forEach(({ setting, replacement }) => {
app.registry.setSetting(setting, replacement);
});
this.settingPriorityChanges.forEach(({ setting, priority }) => {
app.registry.setSettingPriority(setting, priority);
});
this.settingRemovals.forEach((setting) => {
app.registry.removeSetting(setting);
});
this.permissions.forEach(({ permission, type, priority }) => {
const permissionConfig = permission();
if (permissionConfig) {
app.registry.registerPermission(permissionConfig, type, priority);
}
});
this.permissionsReplacements.forEach(({ permission, type, replacement }) => {
app.registry.setPermission(permission, replacement, type);
});
this.permissionsPriorityChanges.forEach(({ permission, type, priority }) => {
app.registry.setPermissionPriority(permission, type, priority);
});
this.permissionsRemovals.forEach(({ permission, type }) => {
app.registry.removePermission(permission, type);
});
if (this.customPage) {
app.registry.registerPage(this.customPage);
} }
});
this.settingReplacements.forEach(({ setting, replacement }) => { app.generalIndex.for(extension.name);
app.registry.setSetting(setting, replacement);
});
this.settingPriorityChanges.forEach(({ setting, priority }) => { Object.keys(this.generalIndexes).forEach((key) => {
app.registry.setSettingPriority(setting, priority); if (key !== 'settings' && key !== 'permissions') return;
});
this.settingRemovals.forEach((setting) => { const callback = this.generalIndexes[key];
app.registry.removeSetting(setting);
});
this.permissions.forEach(({ permission, type, priority }) => { if (callback) {
const permissionConfig = permission(); app.generalIndex.add(key, callback());
}
if (permissionConfig) { });
app.registry.registerPermission(permissionConfig, type, priority);
}
});
this.permissionsReplacements.forEach(({ permission, type, replacement }) => {
app.registry.setPermission(permission, replacement, type);
});
this.permissionsPriorityChanges.forEach(({ permission, type, priority }) => {
app.registry.setPermissionPriority(permission, type, priority);
});
this.permissionsRemovals.forEach(({ permission, type }) => {
app.registry.removePermission(permission, type);
});
if (this.customPage) {
app.registry.registerPage(this.customPage);
}
app.generalIndex.for(extension.name);
Object.keys(this.generalIndexes).forEach((key) => {
if (key !== 'settings' && key !== 'permissions') return;
const callback = this.generalIndexes[key];
if (callback) {
app.generalIndex.add(key, callback());
}
}); });
} }
} }