четвъртък, 29 ноември 2012 г.

Sharetronix Plugin Architecture - Part 1 (Overview)

Overview 

I want to provide you some information how the Sharetronix Plugin Architectures works. In this part the focus is how Sharetronix is managing the apps. In the second part i will take a look on the app itself, what is their structure and how they attach to events. 

To summarize Sharetronix plugin architecture i can say that it is events driven and every plugin(app) attach to the event it needs and return the results to this event. Well, that is very basically said.

First of all apps are stored in the ./apps folder. Every app has unique name and contain plugin.php file which handles the events in Sharetronix(more about this in Part 2). 
After an app is installed(automatically through admin panel or manually) in the database table "plugins" is stored information about the plugin installed.  

Sharetronix system has "Plugin Manager" which handles the events in the apps. On every single page event it first checks the "plugins" table in the database and get the installed (active) plugins, after that checks all plugins one by one if there is some one of them to want to interact with the called event. If there is, it stores the result, after all plugins are checked it stores the result(which plugin wants to interct with the event called) in the database table "plugins_cache" so next time the same event occurs will not be needed to check all plugins one by one, the needed plugins are stored in this table and the "Plugins Manager" just gets them.   When "Plugins Manager" has all plugins which wants to interact with the specific event it call them one by one. More about Sharetronix Events you can find here

How to store result in the view 

This is a typical view in Sharetronix


  1. <!DOCTYPE html>
  2. <html lang="{%html_lang_abbrv%}">
  3.         <head>
  4.                 <title>{%page_title%}</title>
  5.                 {%header_data%}
  6.         </head>
  7.         <body class="fixed-header layout-{%header_page_layout%}">
  8.                 <div id="layout-container">
  9.                        
  10.                                 <div id="header">
  11.                                         <div class="header-container">
  12.                                        
  13.                                                 {%logo_data%}
  14.                                                
  15.                                                 <div id="header-content">
  16.                                                
  17.                                                         {%header_content%}
  18.                                                
  19.                                                 </div>
  20.                                                
  21.                                                 <div class="clear"></div>
  22.                                         </div>
  23.                                 </div>
  24.                                
  25.                                 <div id="page-container">


 or view the code in pastebin.

When you see something like {%header_data%} this is sharetornix variable (placeholder) where controller put some data in it(not mandatory, if controller do not put something it will be deleted). 
This data is provided by the controller, of cource the event onPageLoad() calls all plugins. This event is called after the page loads so all the HTML is loaded in the template and every plugin could add some data it needs to it. 

Let's say we are in a plugin which wants to put some more data in the {%header_data%} on Page load (more about it in the next part). This is how the code looks (pastebin)


  1. public function onPageLoad()
  2. {
  3.         $this->setVar( 'header_data', 'Some data' );
  4. }


That's all. If you want to remove all {%header_data%} that controller has setup then (pastebin)


  1. public function onPageLoad()
  2. {
  3.         $this->setVar( 'header_data', 'Some data', 'replace' );
  4. }


just add 'replace' parameter and that's all. Now all {%header_data%} contents will be 'Some data'. 

This is how plugin events works for now. 

Something very useful about the plugins is that they can have its own controllers. Let's say you have a plugin which you will have to enter some data in it and after that this data to be shown on separate page on some event. In this case very useful is to create an own plugin controller. 

Plugin controllers are stored in ./apps/your-plugin-name/controller/controler-name.php 

Store all the data you need in this controller, you can use the Sharetronix template engine(more information here). 

To reach this plugin user should use the address:

http://site.com/plugin/your-plugin-name/controller-name 

of course you can send POST/GET data to this page if you want. 

That's for now. 

P.S. there is html chache mechanism wich caches some of the html content and stores it in the ./system/cache_html folder. Stored content means that this are pure html files and variables like {%header_data%} is replaced with HTML data. 

Няма коментари:

Публикуване на коментар