Skip to content

How to write a plugin

bianchins edited this page May 20, 2015 · 13 revisions

##How the plugin's system works## JarvisPHP choose the plugin to activate through a regular expression and a priority. Every plugins has a isLikely() functions that execute the pattern matching and returns a boolean result, and a $priority variable that indicate the priority of the plugin over the others. Higher the priority, higher the possibilities for the plugin to be choosen.

The behaviour of the plugin is in the answer function.

##Write your own plugin##

  • Create a folder called Yourplugin_plugin under Plugins folder.
  • Under the Yourplugin_plugin folder, create a file called Yourplugin_plugin.php with the content
namespace JarvisPHP\Plugins\Yourplugin_plugin;

use JarvisPHP\Core\JarvisSession;
use JarvisPHP\Core\JarvisPHP;
use JarvisPHP\Core\JarvisLanguage;
use JarvisPHP\Core\JarvisTTS;

class Yourplugin_plugin implements \JarvisPHP\Core\JarvisPluginInterface {
    /**
     * Priority of the plugin
     * @var int  
     */
    var $priority = 3;
    
    /**
     * the behaviour of the plugin
     * @param string $command
     */
    function answer($command) {
        //something to do
        $response = new \JarvisPHP\Core\JarvisResponse('Answer of JarvisPHP', JarvisPHP::getRealClassName(get_called_class()), true);
        $response->send();
    }
    
    /**
     * Get plugin's priority
     * @return int
     */
    function getPriority() {
        return $this->priority;
    }
    
    /**
     * Is it the right plugin for the command?
     * @param string $command
     * @return boolean
     */
    function isLikely($command) {
        return preg_match('/regular expression to activate the plugin/', $command);
    }
    
    /**
     * Does the plugin need a session?
     * @return boolean
     */
    function hasSession() {
        return false; //or return true
    }
}

You can use JarvisPHP static functions inside your plugins.

  • Create the translation folder called language under Yourplugin_plugin folder
  • Create one file for every languages called Yourplugin_plugin_[language].php, for example Yourplugin_plugin_en.php for english.
  • Edit it as below:
$_lang['translation_index_sentence'] = 'A sentence in english';
$_lang['another_translation_index_sentence'] = 'Another sentence in english';
//and so on...
  • Load the plugin at the startup, editing the index.php file and, in the Load plugin section, adding
JarvisPHP::loadPlugin('Yourplugin_plugin');
You can’t perform that action at this time.