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