Difference between revisions of "Supporting plugins in your component/zh-tw"
From Joomla! Documentation
(4 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
== 背景 == | == 背景 == | ||
− | === Joomla! | + | === Joomla! 觀察者模式 === |
− | Joomla! | + | Joomla! 透過[https://api.joomla.org/cms-3/classes/JPlugin.html JPlugin] (觀察者) 以及 [https://api.joomla.org/cms-3/classes/Joomla.Event.Dispatcher.html JEventDispatcher] (Observable) classes,在全域層級使用 觀察者模式。任何想要接收事件通知的開發者,可以建立一個外掛,擴展 <code>JPlugin</code> class。當他們的外掛類別被載入時,<code>JPlugin</code> 的Subclasses會自動自行註冊到全域的<code>JEventDispatcher</code> class (稍後我們會講到更多)。 <code>JEventDispatcher</code> class is used as a dispatching mechanism that receives events from the communicators and forwards them on to the listeners that have been loaded. 要了解所有的完整說明,推薦閱讀 [[S:MyLanguage/Plugin Developer Overview|外掛開發者總覽]] |
− | === | + | === 為什麼要成為溝通者 === |
− | + | 也許您曾經在開發元件的過程中,覺得假如可以通知其他的元件,某些動作被發動了,豈不是好極了。例如,如果您有一個光碟資料庫元件,當一個新的CD資料被加到資料庫時,您決定要讓其他人知道。在這個案例中,您可以寫一個讓大家使用的事件 (比方說 <code>onCdAddedToLibrary</code>) ,在適當的時間點「觸發」事件,將 CD 的相關資料送出來。所有其他應用了本事件的外掛,會獲得通知,並且可以處理想要作的作業 ,實現即時溝通的需求! | |
− | == | + | == 實踐 == |
− | === | + | === 如何成為溝通者 === |
Since all the dispatching is handled by the Joomla! core, it is easy to become a communicator. In fact, it's really just a matter of loading a certain set of plug-ins and calling the trigger method of the <code>JEventDispatcher</code> class. | Since all the dispatching is handled by the Joomla! core, it is easy to become a communicator. In fact, it's really just a matter of loading a certain set of plug-ins and calling the trigger method of the <code>JEventDispatcher</code> class. | ||
Line 47: | Line 47: | ||
===您正在定義一個 API === | ===您正在定義一個 API === | ||
− | + | 提供事件來提供其他外掛回應,您正在製作 [[wikipedia:API|API]]。請切記要謹慎的規劃 &ndash 一旦您發佈了您的程式碼,其他的開發者會開始使用您事件的名稱以及參數,之後要修改的話,就會影響到所有正在使用這些參數的外掛相容性。 | |
=== 載入正確的外掛群組 === | === 載入正確的外掛群組 === |
Latest revision as of 20:26, 25 January 2021
JEventDispatcher
並不存在於 Joomla 2.5 當中,在這個案例中,您可以不要使用 JEventDispatcher
,而使用 JDispatcher
。真的要在Joomla 3.x 中使用 JDispatcher
也不是不行,但是會出現 deprecated 語法警告。The event system in Joomla! allows for a flexible method for components, modules and plug-ins to communicate with other plug-ins by following the Observer pattern. This pattern is most easily described as a simple communication mechanism. The basic premise is that zero or more "observers" or "listeners" register themselves with the system for a certain, known event. During a specific point in an application's lifecycle, the "communicator" (in our case a component, module or plug-in) fires the event, passing some information to all the observers. The observers can then act on the information passed to them and optionally return a result to the communicator.
Contents
背景
Joomla! 觀察者模式
Joomla! 透過JPlugin (觀察者) 以及 JEventDispatcher (Observable) classes,在全域層級使用 觀察者模式。任何想要接收事件通知的開發者,可以建立一個外掛,擴展 JPlugin
class。當他們的外掛類別被載入時,JPlugin
的Subclasses會自動自行註冊到全域的JEventDispatcher
class (稍後我們會講到更多)。 JEventDispatcher
class is used as a dispatching mechanism that receives events from the communicators and forwards them on to the listeners that have been loaded. 要了解所有的完整說明,推薦閱讀 外掛開發者總覽
為什麼要成為溝通者
也許您曾經在開發元件的過程中,覺得假如可以通知其他的元件,某些動作被發動了,豈不是好極了。例如,如果您有一個光碟資料庫元件,當一個新的CD資料被加到資料庫時,您決定要讓其他人知道。在這個案例中,您可以寫一個讓大家使用的事件 (比方說 onCdAddedToLibrary
) ,在適當的時間點「觸發」事件,將 CD 的相關資料送出來。所有其他應用了本事件的外掛,會獲得通知,並且可以處理想要作的作業 ,實現即時溝通的需求!
實踐
如何成為溝通者
Since all the dispatching is handled by the Joomla! core, it is easy to become a communicator. In fact, it's really just a matter of loading a certain set of plug-ins and calling the trigger method of the JEventDispatcher
class.
You may wonder how to know which set of plug-ins to load. That's up to you. Plug-ins are managed at a group level that is defined in the plug-in's XML deployment file. There are eight predefined plug-in groups and each plug-in group is meant to handle a different set of tasks. For example, there is a search plug-in group that is meant to handle searching and a user plug-in group meant to handle user specific functions such as adding and removing a user from the Joomla! system. These plug-in groups are only loaded when they are needed, namely by the communicator. So you can create your own plug-in group and call it whatever you want. Because your component is well-defined, all listeners will know exactly which plug-in group and events they should be listening for.
如何觸發事件
這就是本文的重點所在:如何觸發事件,以使外掛可以在元件當中作用,第一件要做的事情就是載入外掛群組,可以用以下的程式碼來實現:
JPluginHelper::importPlugin( 'myplugingroup' );
這會載入所有啟用的外掛,它們會自行定義為您的群組的一部分。下一件要作的事情是要取得 JEventDispatcher
class 的實體(instance):
$dispatcher = JEventDispatcher::getInstance();
在這裡要注意兩鍵事情。首先,我們使用了 getInstance()
method,而不是 new
,來建立新的實例 (instance)。這是因為我們需要取得JEventDispatcher 物件的全域單例實例(singleton instance),其中包含所有可用的外掛清單。
接著,我們需要觸發我們的客製化事件:
$results = $dispatcher->trigger( 'onCdAddedToLibrary', array( &$artist, &$title ) );
Here we have triggered the event onCdAddedToLibrary
and passed in the artist name and title of the track. Which and how many parameters you pass is up to you. Passing parameters by reference (using an &
) allows the plug-ins to change the variables passed in. All plug-ins will receive these parameters, process them and optionally pass back information. The calling code can access the returned values via the array $results
(each element corresponds to the result of one plugin).
注意事項
您正在定義一個 API
提供事件來提供其他外掛回應,您正在製作 API。請切記要謹慎的規劃 &ndash 一旦您發佈了您的程式碼,其他的開發者會開始使用您事件的名稱以及參數,之後要修改的話,就會影響到所有正在使用這些參數的外掛相容性。
載入正確的外掛群組
關於觸發的方法,有件事需要注意:沒有定義哪一個群組的外掛要被告知。事實上,所有載入的外掛都不分群組地被告知了。確認您的事件名稱沒有和別的外掛的事件名稱衝突,是很重要的。這在大多數情形下也許不是問題,因為您的元件會載入外掛群組,所以您很清楚哪些被載入了,然而要知道 system 外掛群組在一開始的請求中就會被載入,請確認您沒有任何事件名稱和系統的事件名稱衝突。