WordPress插件:如何避免“紧耦合”?

时间:2011-12-31 14:47:27

标签: php wordpress oop

我正在使用WordPress插件,我正在努力确保最佳做法。我有两个类,我的插件类“Jargonaut”是必需的,然后是另一个名为“Dictionary”的类,它与require_once()一起包含在我的主插件文件中。

Jargonaut类中的大多数代码都处理初始化并提供类似控制器的功能,但其中大部分都高度依赖于使用Dictionary对象(即与我对术语的理解紧密耦合)。我希望将Dictionary类保持分离,因为它更像是一个模型(在MVC架构中)并与我的数据库接口。

我看到紧密与松散耦合的很多灰色区域,我很难决定多少太多了?

1 个答案:

答案 0 :(得分:60)

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

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 System\Wordpress3();
                $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;
    }

...

引导方法还可以注册自动加载器或执行要求。

希望这很有用。