HomeBack home

S2
Call a hook (service callback) with functions

$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 function initTextDomain found in file functions.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 = 'functions.php';
    $func = '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 S2 for Config_Registry : call Services
     * (hooks came from functions)
     * 
     * @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 </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 = './functions.php';
    $func = 'initTextDomain';
    $args = array('language','domain','translationsDir');
    $srv = $registry->registerService($app, $method, $file, $func, $args);
    handleError($srv);
    
    $method = 'message/get';
    $file = './functions.php';
    $func = '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('SetupLdrStartupMessage');
    $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('SetupLdrStartupMessage');
    $extra = 'PEAR';
    $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('LastErrorMessage');
    $extra = array('DataBase','2008','MySQL client out of memory');
    $srv = $registry->callService($app, $method, $args, $extra);
    handleError($srv);
    
    echo "<b>$srv</b>";
    
    print "<hr/><ul>";
    print "<li>[1] stringID = SetupLdrStartupMessage = '%1 va ĂȘtre installĂ©. Souhaitez-vous continuer ?'</li>";
    print "<li>[2] stringID = LastErrorMessage = '%1.%n%nError %2 : %3'</li>";
    print "<li>[3] %1</li>";
    print "<li>[4] %2</li>";
    print "<li>[5] %3</li>";
    print "</ul><p>";
    ?>
    
    
    href:  examples/services/call-functions.php
    Run this script

    [Top]