HomeBack home

S3
Call a hook (service callback) with class-methods

$Date$

 Table of contents

  Introduction

This example allows communication between two applications through a "hook". This hook, also called service, will initialize gettext extension with help of method initTextDomain found in class gettextPlugin of file methods.php.
This service, function, required 3 mandatory parameters ('language','domain','translationsDir'), which will have values on call:

  • $language = 'en';
  • $domain = 'sw4p';
  • $translationsDir = 'locale';

    Resulting that gettext engine will keep translation through the file services/locale/en/sw4p.mo

    [Top]

      Synopsis

    $registry = &Registry::singleton();
    $app = 'sw4p';
    $registry->registerApplication($app);
    
    $method = 'textdomain/set';
    $file = 'methods.php';
    $func = array('gettextPlugin','initTextDomain');
    $args = array('language','domain','translationsDir');
    $srv = $registry->registerService($app, $method, $file, $func, $args);
    ...
    $args = array('en','sw4p','locale');
    $srv = $registry->callService($app, $method, [$args = array(), [$extra = null]]);
    

    [Top]

      Parameters

    string    $app          The desired application
    string    $method       The method to call
    array     $args         (optional) Arguments to the method
    mixed     $extra        (optional) Non-standard arguments to the method
    

    [Top]

      Play example

    
    <?php
    /**
     * Example S3 for Config_Registry : call Services
     * (hooks came from class/methods)
     * 
     * @version    0.1
     * @author     Laurent Laville <pear@laurent-laville.org>
     * @access     public
     * @package    Config_Registry
     * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
     *
     * $Id$
     */
    
    require_once 'Config/Registry.php';
    require_once '../handleError.php';
    
    print "<h1>Call services of application - class-methods hooks </h1>";
    print "<p>Registers application 'gettext' with two services <ul>";
    print "<li>set_textdomain: to initialize language and bind textdomain</li>";
    print "<li>get_message: to read a message with parameter (%1, %2, ...) values replacement</li>";
    print "</ul></p>";
    
    $registry =& Config_Registry::singleton();
    
    $app = 'gettext';
    $reg = $registry->registerApplication($app, 'php-gettext plugin', realpath('.'));
    handleError($reg);
    
    $method = 'textdomain/set';
    $file = './methods.php';
    $func = array('gettextPlugin','initTextDomain');
    $args = array('language','domain','translationsDir');
    $srv = $registry->registerService($app, $method, $file, $func, $args);
    handleError($srv);
    
    $method = 'message/get';
    $file = './methods.php';
    $func = array('gettextPlugin','getMessage');
    $args = array('stringID');
    $srv = $registry->registerService($app, $method, $file, $func, $args);
    handleError($srv);
    
    print "<h2>try 1 </h2>";
    print "<p>Display english message [1] without first parameter [3] replacement<p>";
    
    $method = 'textdomain/set';
    $args = array('en','sw4p','locale');
    $srv = $registry->callService($app, $method, $args);
    handleError($srv);
    
    $method = 'message/get';
    $args = array('SetupFileMissing');
    $srv = $registry->callService($app, $method, $args);
    handleError($srv);
    
    echo "<b>$srv</b>";
    
    print "<h2>try 2 </h2>";
    print "<p>Display french message [1] with first parameter [3] replacement<p>";
    
    $method = 'textdomain/set';
    $args = array('fr','sw4p','locale');
    $srv = $registry->callService($app, $method, $args);
    handleError($srv);
    
    $method = 'message/get';
    $args = array('SetupFileMissing');
    $extra = 'Progress.php';
    $srv = $registry->callService($app, $method, $args, $extra);
    handleError($srv);
    
    echo "<b>$srv</b>";
    
    print "<h2>try 3 </h2>";
    print "<p>Display english message [2] with all parameters [3][4][5] replacement<p>";
    
    $method = 'textdomain/set';
    $args = array('en','sw4p','locale');
    $srv = $registry->callService($app, $method, $args);
    handleError($srv);
    
    $method = 'message/get';
    $args = array('ErrorFunctionFailedWithMessage');
    $extra = array('DataBase connection','5','invalid account');
    $srv = $registry->callService($app, $method, $args, $extra);
    handleError($srv);
    
    echo "<b>$srv</b>";
    
    print "<hr/><ul>";
    print "<li>[1] stringID = SetupFileMissing = 'Le fichier %1 n'a pas été trouvé dans le répertoire de l'installation.%n%nCorrigez le problème ou obtenez une nouvelle copie du programme.'</li>";
    print "<li>[2] stringID = ErrorFunctionFailedWithMessage = '%1 failed; code %2.%n%n%3'</li>";
    print "<li>[3] %1</li>";
    print "<li>[4] %2</li>";
    print "<li>[5] %3</li>";
    print "</ul><p>";
    ?>
    
    
    href:  examples/services/call-methods.php
    Run this script

    [Top]