用php建立一个插件系统 [英] build a plugin system with php

查看:208
本文介绍了用php建立一个插件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为自己使用的自定义CMS工作,并且正在考虑实现插件系统,以便我可以更轻松地扩展代码.我在概念化体系结构和布局时遇到了麻烦.

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.

我知道我可以通过几个实现类似功能的开源程序,但这在当时还只是学术上的事情,所以我真的不想花太多时间来研究外来代码.

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. 定义您希望插件插入的功能(即,它们将做什么以及针对什么进行操作)
  2. 定义一个适合插件的类层次结构,例如,所有文章处理插件应从ArticleMangler继承
  3. 为插件定义物理位置,例如/plugins
  4. 导入该位置中存在的所有插件
  5. 使用装饰器观察者模式来注入插件的行为或通知插件事件的发生. 策略在某些情况下也可能适用.
  1. Define the functionality you want the plugins to plug into (ie, what will they do and over what)
  2. Define a class hierarchy on which plugins fit, like, all article mangling plugins should inherit from ArticleMangler
  3. Define a physical location for plugins, like /plugins
  4. Import all plugins present in the location
  5. 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.

如果您不小心,PHP会使此操作变得相当容易,但有可能造成混乱.我喜欢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.

如果您不信任插件,则必须添加添加控件,以控制要允许任何插件注册的事件.

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.

这篇关于用php建立一个插件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆