分离 Drupal 模块逻辑和 UI
我编写了一个 D6 模块,它为用户提供了通信、配置参数和测试 3rdparty API 服务的能力。
该模块按预期工作,但我想分离通信器类并将其捆绑为 foo 模块。然后将其余部分(管理页面)打包为 foo-ui 模块。就像views和views-ui一样。
我不知道这样做的最佳实践/设计模式是什么。有什么想法吗?
I wrote a D6 module which provide user with the ability to communicate, config params and test a 3rdparty API service.
The module works as intended but I want to separate the communicator class and bundle it as foo module. Then pack the rest (admin page) as the foo-ui module. Just like views and views-ui.
I have no idea about what's the best-practices / design patterns to do so. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,没有特定的模式,但总是有一个问题:
好的,假设我们的模块需要
_ui
东西。这里我尝试将模块的核心功能与其配置 UI 分开作为示例。看一下 Unfuddle API 模块,它是一个非常小且简单的模块,适合我们的情况。这是$tree ./unfuddle_api
:阅读它的代码!
unfuddle_api.install
不安装任何架构,但实现hook_uninstall()
以在模块卸载时使用variable_del()
删除一些变量。这些变量实际上是 API 连接参数,将在模块的管理页面中设置,您的模块中也可能有其中一些。IMO,这是要迁移到
unfuddle_api_ui
模块的部分。文件
unfuddle_api.classes.inc
是 unfuddle,它是以某种方式包装在一个类中的第 3 方代码,该类将用于实例化对象。IMO 这应该保留在核心模块中。但为了设计的目的,我们必须通过删除
variable_get()
调用并将传递的参数直接设置为类属性来更改构造函数。我们稍后会将它们添加到.module
文件中。文件
unfuddle_api.module
包含主模块代码,hook_perm()< 的实现< /code> 加 1 用于
嗯>。hook_menu()
创建管理页面和一个名为unfuddle_api_create()
的辅助函数,它在某种程度上是一个 Factory 帮助器,用于从unfuddle_api.classes.inc
中编码的Unfuddle
类创建对象.IMO 两个钩子实现都应该移植到
_ui
模块。此外,上面讨论的unfuddle_api.classes.inc
中删除的variable_get()
部分也应添加到unfuddle_api_create()
函数中因此,作为一个概述:我们有一个
unfuddle_api
模块,它充当Unfuddle
API 类的包装器。它为我们提供了所有功能,但没有配置 UI。所有配置都应通过代码中的unfuddle_api_create()
模块设置。此外,我们还有一个
unfuddle_api_ui
模块,它依赖于unfuddle_api
模块,如果启用,将为我们提供 UI 中的核心模块配置。希望有帮助。
As I know, there is no specific pattern, but there's always a question:
Ok, assuming that our module needs the
_ui
thing. Here I try to separate the core functionality of a module and its configuration UI as an example. Take a look at Unfuddle API module, it's a pretty tiny and simple module which fits our case. Here is the$ tree ./unfuddle_api
:Read its code! The
unfuddle_api.install
is installing no schema but implementing thehook_uninstall()
to delete a few variables usingvariable_del()
on module uninstall. These variables are actually the API connection parameters which will be set in admin page of the module, you might have a few of them in your module, too.IMO this is a part to be migrated to
unfuddle_api_ui
module.The file
unfuddle_api.classes.inc
is the API wrapper class for unfuddle, it's somehow 3rdparty code wrapped in a class which will be used to instantiate an object.IMO this should be remained in the core module. But for the sake of design we have to change the constructor method by removing
variable_get()
calls and setting passed parameters straight to class properties. We'll add them later in.module
file.The file
unfuddle_api.module
contains the main module code, An implementation ofhook_perm()
plus one forhook_menu()
to create the admin page and a helper function namedunfuddle_api_create()
which is somehow a Factory helper to create an object fromUnfuddle
class coded inunfuddle_api.classes.inc
.IMO both hook implementations should be ported to the
_ui
module. Also the removedvariable_get()
part of theunfuddle_api.classes.inc
discussed above, should be added here in theunfuddle_api_create()
function.So, As an outline: We have a
unfuddle_api
module which acts as a wrapper for theUnfuddle
API class. It provide us all the functionality but no configuration UI. All the configs should be set through theunfuddle_api_create()
module in code.Also we got a
unfuddle_api_ui
module which depends onunfuddle_api
module and if enabled will provide us with the core module configuration in UI.Hope it helps.
对于不经常使用 UI 的模块也很有用。禁用 UI 可以节省一些资源。
我建议您检查 imagecache 模块作为示例。 http://drupal.org/project/imagecache
Also it is usefull for modules that does not use UI offten. Disabling UI saves some resources.
I suggest you to check imagecache module as an example. http://drupal.org/project/imagecache
没有简单的答案。您需要强制将功能与用户界面您自己分离。因此,您需要将程序的所有 API 放在一个模块中,而将用户界面放在另一个模块中。使用 simpletest 模块为您的 API 编写测试用例。通过编写测试用例,您将成为 API 的“消费者”。事实上,您可以将 UI 的实现延迟足够长的时间,以便确定负责“核心”功能的模块中具有正确的函数/类集。
您还应该探索 CTools 模块。它提供了一些有用的工具,例如能够制作可导出的东西等等。
Drupal 没有像 Java 或 C# 那样的大量书籍形式的文献。但 PHP 可以。查看有关 PHP 设计模式的书籍。虽然我还没有读过,Manning 的“PHP in action”似乎有很多内容正是您所要求的内容。
做此类事情的另一种方法是看看其他人是如何做到的。 imagecache 模块足够小,它将 UI 与功能分开(您需要启用一个单独的 Imagecache UI 模块)。为什么不实际探索一下 imagecache 的源代码呢? [Views 的源代码超过 1 MB,因此目前探索它可能不是一个好主意:-)]
There is no easy answer. You need to enforce the separation of functionality from the User interface yourself. So you need to put all the APIs of your program in one module and the user interface in another. Write test cases for your API -- using the simpletest module. By writing test cases you are becoming the "consumer" of your APIs. In fact, you can delay the implementation of your UI long enough that you are sure that you have the correct set of functions/classes in the module responsible for "core" functionality.
Also you should explore the CTools module. It gives some useful tools like being able to make exportables and so on.
Drupal does not have a wealth of literature in book form like Java or C# for instance. But PHP does. Check out books on design patterns in PHP. Though I haven't read it, "PHP in action" by Manning seems to have a lot of stuff on precisely the things you're asking.
Another way to do such things is to see how others have done it. The imagecache module is small enough and it separates the UI from functionality (there is a separate Imagecache UI module you need to enable). Why not actually explore the source code of imagecache? [Views is 1 MB+ of source code so its probably not a good idea to explore that just yet :-)]