问题描述

我正在使用 WordPress 插件,并试图确保最佳做法。我有两个类,我的插件类”Jargonaut” 是必需的,然后另一个类叫做”Dictionary”,它包含在 require_once()到我的主要插件文件中。

Jargonaut 类中的大多数代码解决了初始化问题,并提供了 controller-like 功能,但大部分代码高度依赖于使用 Dictionary 对象 (即从我对该术语的理解中紧密耦合) 。我希望将 Dictionary 类分离,因为它更像是一个模型 (在 MVC 架构中) 并与我的数据库接口。

我看到很多灰色的地方在紧耦合松散耦合,我很难决定多少是太多?

最佳解决思路

如果你的插件需要字典对象,它必须要求它:

class MyPlugin
{
    /**
     * @var Dictionary
     */
    private $dictionary;
    private function __construct(Dictionary $dictionary)
    {
        $this->dictionary = $dictionary;
    }

您现在已经将插件与 Dictionary 松散耦合,插件类不再为自己创建 Dictionary,因为它被注入。它需要它需要什么

那么这个工作怎么样?该插件需要在某处创建,因此需要一个工厂。工厂构建方法知道您的插件需要什么:

class MyPluginFactory
{
    public static function build($pluginName)
    {
        $plugin = NULL;
        switch($pluginName)
        {
            case 'MyPlugin':
                $dictionary = new Dictionary();
                $plugin = new MyPlugin($dictionary);
        }
        return $plugin;
    }
}

由于这是 wordpress,我们知道插件的引导是通过包含插件文件完成的。所以在开始的时候,需要创建插件。由于包含在全局范围内完成,我们希望保留内存中的插件对象,但不可用作全局变量。这不会阻止您创建多个插件实例,但它将确保在 WordPress 初始化插件 (加载您的插件) 时,它将仅使用该单个实例。这可以通过使插件工厂做一些额外的功能:

class MyPluginFactory
{
    ...
    public static $plugins;
    public static function bootstrap($pluginName)
    {
        $plugin  = self::build($pluginName);
        self::$plugins[] = $plugin;
        return $plugin;
    }

请注意,静态类成员变量的唯一用法仅在于确保插件保留在内存中。它在技术上是一个我们通常想要防止的全局变量,但是实例需要存储在某个地方,所以这里是 (我把它改为 public,因为它是一个全局变量,它不应该是害羞的。私人或受保护的某些情况下公众可能会受到限制,也不应该是一个问题,如果是一个问题,还有另外一个问题首先要解决) 。

这基本上将您的插件代码与 WordPress 本身分离。您可能还想创建一个提供和使用任何 wordpress 功能的界面的类,因此您不必直接绑定这些函数,并且您的插件代码保持干净和松散地耦合到 wordpress 本身。

class WordPressSystem
{
    public function registerFilter($name, $plugin, $methodName)
    {
        ... do what this needs with WP, e.g. call the global wordpress function to register a filter.
    }
    ...
}

然后如果您的插件需要 WordPressSystem 执行任务 (通常是这种情况),则再次将其添加为依赖关系:

class MyPlugin
{
    ...
    public function __construct(WordPressSystem $wp, Dictionary $dictionary)
    ...

所以要最后包装,只需要插件 php 文件:

<?php
/*
 * MyPlugin
 *
 * Copyright 2010 by hakre <hakre.wordpress.com>, some rights reserved.
 *
 * WordPress Plugin Header:
 *
 *   Plugin Name:    My Plugin
 *   Plugin URI:     http://hakre.wordpress.com/plugins/my-plugin/
 *   Description:    Yet another wordpress plugin, but this time mine
 *   Version:        1.2-beta-2
 *   Stable tag:     1.1
 *   Min WP Version: 2.9
 *   Author:         hakre
 *   Author URI:     http://hakre.wordpress.com/
 *   Donate link:    http://www.prisonradio.org/donate.htm
 *   Tags:           my
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
Namespace MyPlugin;

# if your file is named 'MyPlugin.php' this will be 'MyPlugin'.
return PluginFactory::bootstrap(basename($plugin, '.php'));

class PluginFactory
{
    private static $plugins;
    public static function bootstrap($pluginName)
    {
        $plugin = self::build($pluginName);
        self::$plugins[] = $plugin;
        return $plugin;
    }
    public static function build($pluginName)
    {
        $plugin = NULL;
        switch($pluginName)
        {
            case 'MyPlugin':
                # Make your plugin work with different WordPress Implementations.
                $system = new SystemWordpress3();
                $dictionary = new Dictionary();
                $plugin = new Plugin($system, $dictionary);
        }
        return $plugin;
    }
}

class Plugin
{
    /**
     * @var System
     */
    private $system;
    /**
     * @var Dictionary
     */
    private $dictionary;
    private function __construct(System $system, Dictionary $dictionary)
    {
        $this->system = $system;
        $this->dictionary = $dictionary;
    }

...

引导方法还可以注意注册自动装载机或执行要求。

希望这是有用的。

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。