用php构建一个插件系统

时间:2023-01-24 18:30:45

I'm working on a custom CMS for my own use and was thinking about implementing a plugin system so I could extend the code a little easier. I'm having trouble conceptualizing the architecture and layout though.

我正在为我自己使用自定义CMS,并正在考虑实现一个插件系统,以便我可以更轻松地扩展代码。我在构思架构和布局方面遇到了麻烦。

I know I could go through a few open source programs that implement similar features but this is really just academic at this point so I really don't want to spend too much time digging through foreign code.

我知道我可以通过一些实现类似功能的开源程序,但这实际上只是学术上所以我真的不想花太多时间挖掘外国代码。

Does anyone have any good ideas of how to proceed? If someone could outline how some of the more popular programs do it that'd be perfect.

有没有人对如何进行有任何好的想法?如果有人可以概述一些更受欢迎的程序如何做到这一点是完美的。

1 个解决方案

#1


31  

  1. Define the functionality you want the plugins to plug into (ie, what will they do and over what)
  2. 定义您希望插件插入的功能(即,他们将做什么以及做什么)
  3. Define a class hierarchy on which plugins fit, like, all article mangling plugins should inherit from ArticleMangler
  4. 定义插件适合的类层次结构,例如,所有文章修改插件都应该从ArticleMangler继承
  5. Define a physical location for plugins, like /plugins
  6. 定义插件的物理位置,例如/ plugins
  7. Import all plugins present in the location
  8. 导入该位置中存在的所有插件
  9. Use either Decorator or Observer patterns to inject the plugin's behavior or to notify the plugins of events occurence. Strategy might be applicable in some cases as well.
  10. 使用Decorator或Observer模式注入插件的行为或通知插件事件发生。战略也可能适用于某些情况。

PHP makes this fairly easy at a potential cost of making a mess if you're not careful. I like the Observer method where plugins register themselves to a plugin manager which notify them of what happened and wait for their action to happen.

如果你不小心的话,PHP可以很容易地造成混乱。我喜欢Observer方法,其中插件将自己注册到插件管理器,该插件管理器通知他们发生了什么并等待他们的操作发生。

If you don't trust plugins then you'd have to put add controls over which events you are going to allow any plugin to register for.

如果您不信任插件,那么您必须添加控件,以允许您允许任何插件注册的事件。

#1


31  

  1. Define the functionality you want the plugins to plug into (ie, what will they do and over what)
  2. 定义您希望插件插入的功能(即,他们将做什么以及做什么)
  3. Define a class hierarchy on which plugins fit, like, all article mangling plugins should inherit from ArticleMangler
  4. 定义插件适合的类层次结构,例如,所有文章修改插件都应该从ArticleMangler继承
  5. Define a physical location for plugins, like /plugins
  6. 定义插件的物理位置,例如/ plugins
  7. Import all plugins present in the location
  8. 导入该位置中存在的所有插件
  9. Use either Decorator or Observer patterns to inject the plugin's behavior or to notify the plugins of events occurence. Strategy might be applicable in some cases as well.
  10. 使用Decorator或Observer模式注入插件的行为或通知插件事件发生。战略也可能适用于某些情况。

PHP makes this fairly easy at a potential cost of making a mess if you're not careful. I like the Observer method where plugins register themselves to a plugin manager which notify them of what happened and wait for their action to happen.

如果你不小心的话,PHP可以很容易地造成混乱。我喜欢Observer方法,其中插件将自己注册到插件管理器,该插件管理器通知他们发生了什么并等待他们的操作发生。

If you don't trust plugins then you'd have to put add controls over which events you are going to allow any plugin to register for.

如果您不信任插件,那么您必须添加控件,以允许您允许任何插件注册的事件。