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

2022-08-30 13:25:04

我正在开发WordPress插件,并试图确保最佳实践。我有两个类,我的插件类“Jargonaut”是必需的,然后是另一个名为“字典”的类,它包含在我的主插件文件中。require_once()

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

我在紧耦合与松耦合中看到了很多灰色区域,很难确定多少是太多?


答案 1

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

class MyPlugin
{
    /**
     * @var Dictionary
     */
    private $dictionary;
    private function __construct(Dictionary $dictionary)
    {
        $this->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;
    }

请注意,静态类成员变量的唯一用法只是为了确保插件保留在内存中。从技术上讲,它是我们通常想要防止的全局变量,但是,实例需要存储在某个地方,所以它在这里(我将其更改为公共,因为它是一个全局变量,它不应该害羞。在某些情况下,私人或受保护限制性太强,拥有公共公众可能会有所帮助。这也不应该是一个问题。如果问题,则应首先修复另一个问题)。

这基本上将您的插件代码与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;
    }

...

bootstrap 方法还可以负责注册自动加载程序或满足要求。

希望这是有用的。


答案 2

推荐