Examples of PHP API code
Use the following examples of PHP API code for common tasks to help you write your own PHP code to modify pattern instances.
About this task
Procedure
- Running additional PHP scripts. The following example shows how to use a PHP script to run another PHP script, by using the mb_pattern_run_template() function. The mb_pattern_run_template() function uses the following parameters:
- The first parameter required by mb_pattern_run_template() is the name of the project containing the PHP template; Transform in this example.
- The second parameter required by mb_pattern_run_template() is the name of the PHP script to run; in this example example.esql.php, which is in a subfolder called mqsi.
- The third parameter required by mb_pattern_run_template() is the name of the file to which the output is written; in this example example.esql, in subfolder mqsi.
<?php if ($_MB['PP']['includeErrorHandling'] == 'true') { mb_pattern_run_template("Transform", "mqsi/example.esql.php", "mqsi/example.esql"); } ?>
- Using PHP scripts with markup for ESQL. In the following example, the user-defined pattern contains a pattern parameter called errorQueue, which can contain the values none or errorAction:
BROKER SCHEMA mqsi <?php if ($_MB['PP']['errorAction'] == 'errorQueue') { echo "DECLARE ErrorAction EXTERNAL CHARACTER '".$_MB['PP']['errorAction']."';"; echo <<<ESQL CREATE FILTER MODULE CheckErrorAction CREATE FUNCTION Main() RETURNS BOOLEAN BEGIN IF ErrorAction = 'errorQueue' THEN RETURN TRUE; ELSE RETURN FALSE; END IF; END; END MODULE; ESQL; } ?>
- Using PHP scripts to remove files from a project. In the
following example, the script first checks the value of the check
box pattern parameter, which has the pattern parameter ID pp1. If this parameter is set to false, the script deletes the Log.msgflow and Log.esql files
from the pattern instance project. Message flow files must be deleted
by using the removeMessageFlow() function. Non-message
flow files can be deleted by using the standard PHP function unlink():
if ($_MB['PP']['pp1'] == 'false') { $pim = $_MB["PATTERN_INSTANCE_MANAGER"]; $logmsgflow = $pim->getMessageFlow("Example_Flows", "mqsi/Log.msgflow"); $pim->removeMessageFlow($logmsgflow); $piworkspace = $pim->getWorkspaceLocation(); $piname = $pim->getPatternInstanceName(); $logesql = $piworkspace . "/" . $piname . "_Example_Flows/mqsi/Log.esql"; unlink($logesql); }
- Running Java™ code
from PHP by using a static Java method.
The following example shows a Java class, MyClass,
that can be run from within a PHP script. The class contains a static
method:
package com.your.company.domain.code; import com.ibm.broker.config.appdev.MessageFlow; import com.ibm.broker.config.appdev.patterns.GeneratePatternInstanceTransform; import com.ibm.broker.config.appdev.patterns.PatternInstanceManager; public class MyClass implements GeneratePatternInstanceTransform { public static void doSomethingUseful(String message) { System.out.println("Message received [" + message + "]"); } @Override public void onGeneratePatternInstance(PatternInstanceManager patternInstanceManager) { } }
You can run the MyClass class from PHP as shown in the following example. In the line
$class = $pim->getPluginClass("com.your.company.domain.code", "com.your.company.domain.code.MyClass")
, the first argument,com.your.company.domain.code
, is the ID of the plug-in that contains the Java class. The second argument,com.your.company.domain.code.MyClass
, is the name of the class.<?php $pim = $_MB["PATTERN_INSTANCE_MANAGER"]; $class = $pim->getPluginClass("com.your.company.domain.code", "com.your.company.domain.code.MyClass"); java_import("com.your.company.domain.code.MyClass"); MyClass::doSomethingUseful("Hello!"); ?>
- Running Java code
from PHP by using a non-static Java method.
The following example shows a Java class, MyClass,
that can be run from within a PHP script. The class contains a method
that is not static:
package com.your.company.domain.code; import com.ibm.broker.config.appdev.MessageFlow; import com.ibm.broker.config.appdev.patterns.GeneratePatternInstanceTransform; import com.ibm.broker.config.appdev.patterns.PatternInstanceManager; public class MyClass implements GeneratePatternInstanceTransform { public void doSomethingUseful(String message) { System.out.println("Message received [" + message + "]"); } @Override public void onGeneratePatternInstance(PatternInstanceManager patternInstanceManager) { } }
You can run the MyClass class from PHP as shown in the following example. In the line
$class = $pim->getPluginClass("com.your.company.domain.code", "com.your.company.domain.code.MyClass")
, the first argument,com.your.company.domain.code
, is the ID of the plug-in that contains the Java class. The second argument,com.your.company.domain.code.MyClass
, is the name of the class. A new instance of the class is created in the line$obj = $class->newInstance()
. In the last line of the example, thedoSomethingUseful()
method of the class is run.<?php $pim = $_MB["PATTERN_INSTANCE_MANAGER"]; $class = $pim->getPluginClass("com.your.company.domain.code", "com.your.company.domain.code.MyClass"); $obj = $class->newInstance(); $obj->doSomethingUseful("Hello!"); ?>
- Using PHP scripts to read table values. In the
following example, the PHP script extracts the values from an array
stored in the superglobal variable $_MB. First
the script searches inside the array for any values stored in the event and response variables,
and then returns the values:
<?php $pim = $_MB["PATTERN_INSTANCE_MANAGER"]; $table = $pim->getParameterTable("table1"); $count = $table->getRowCount(); for ($j=0;$j<$count ;$j++ ) { echo "".$_MB['PP']['table1'][$j]['event']."';\n"; echo "".$_MB['PP']['table1'][$j]['response']."';\n"; } ?>
The array has the following structure:
array(4) { ["PATTERN_INSTANCE_MANAGER"]=> object(Java)#1 (0) { } ["PP"]=> array(10) { ["queueName"]=>string(11) "QP.QUEUE.QS" ["configurePrefixSuffix"]=>string(5) "false" ["queuePrefix"]=>string(3) "QP." ["queueSuffix"]=>string(3) ".QS" ["logging"]=>string(5) "queue" ["generateMessageSet"]=>string(4) "true" ["table1"]=> array(2) { [0]=> array(2) { ["event"]=>string(6) "event1" ["response"]=>string(9) "response1" } [1]=> array(2) { ["event"]=>string(6) "event2" ["response"]=>string(9) "response2" } } }