-
Notifications
You must be signed in to change notification settings - Fork 1
feat(core): add replaceService to PluginContext #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,6 +119,15 @@ export class ObjectKernel { | |
| throw new Error(`[Kernel] Service '${name}' not found`); | ||
| } | ||
| }, | ||
| replaceService: <T>(name: string, implementation: T): void => { | ||
| const hasService = this.services.has(name) || this.pluginLoader.hasService(name); | ||
| if (!hasService) { | ||
| throw new Error(`[Kernel] Service '${name}' not found. Use registerService() to add new services.`); | ||
| } | ||
| this.services.set(name, implementation); | ||
| this.pluginLoader.replaceService(name, implementation); | ||
| this.logger.info(`Service '${name}' replaced`, { service: name }); | ||
|
Comment on lines
+122
to
+129
|
||
| }, | ||
| hook: (name, handler) => { | ||
| if (!this.hooks.has(name)) { | ||
| this.hooks.set(name, []); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -248,6 +248,18 @@ export class PluginLoader { | |
| this.serviceInstances.set(name, service); | ||
| } | ||
|
|
||
| /** | ||
| * Replace an existing service instance. | ||
| * Used by optimization plugins to swap kernel internals. | ||
| * @throws Error if service does not exist | ||
| */ | ||
| replaceService(name: string, service: any): void { | ||
| if (!this.hasService(name)) { | ||
| throw new Error(`Service '${name}' not found`); | ||
| } | ||
| this.serviceInstances.set(name, service); | ||
| } | ||
|
Comment on lines
+256
to
+261
|
||
|
|
||
| /** | ||
| * Check if a service is registered (either as instance or factory) | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -394,6 +394,12 @@ export class SecurePluginContext implements PluginContext { | |
| return this.baseContext.getService<T>(name); | ||
| } | ||
|
|
||
| replaceService<T>(name: string, implementation: T): void { | ||
| // Check permission before replacing service | ||
| this.permissionEnforcer.enforceServiceAccess(this.pluginName, name); | ||
| this.baseContext.replaceService(name, implementation); | ||
| } | ||
|
Comment on lines
+397
to
+401
|
||
|
|
||
| getServices(): Map<string, any> { | ||
| // Return all services (no permission check for listing) | ||
| return this.baseContext.getServices(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,6 +28,17 @@ export interface PluginContext { | |||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| getService<T>(name: string): T; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Replace an existing service with a new implementation. | ||||||||||||||||||||||||||||
| * Useful for optimization plugins that wrap or swap kernel internals | ||||||||||||||||||||||||||||
| * (e.g., metadata registry, connection pooling). | ||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||
| * @param name - Service name to replace | ||||||||||||||||||||||||||||
| * @param implementation - New service implementation | ||||||||||||||||||||||||||||
| * @throws Error if the service does not exist | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| replaceService<T>(name: string, implementation: T): void; | ||||||||||||||||||||||||||||
|
Comment on lines
+36
to
+40
|
||||||||||||||||||||||||||||
| * @param name - Service name to replace | |
| * @param implementation - New service implementation | |
| * @throws Error if the service does not exist | |
| */ | |
| replaceService<T>(name: string, implementation: T): void; | |
| * NOTE: This is optional for backward compatibility with existing plugins | |
| * and test contexts that may not implement service replacement. | |
| * | |
| * @param name - Service name to replace | |
| * @param implementation - New service implementation | |
| * @throws Error if the service does not exist | |
| */ | |
| replaceService?<T>(name: string, implementation: T): void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
IServiceRegistrybranch,replaceServicecallsthis.services.register(name, implementation)after confirming the service already exists.IServiceRegistry.register()is specified to throw if the name is already registered, so this path will fail wheneverservicesis anIServiceRegistry. Useunregister(name)(or a dedicated replace/set API) before re-registering.