問題描述
我正在使用 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。