PHP WinBinder 4: Command Processing

From The Uniform Server Wiki
Jump to navigation Jump to search

 

UniServer 5-Nano
PHP WinBinder 4 - Tray Menu.

WinBinder Part 4 - Command Processing

Introduction

Our tray menu is currently a pleasing cosmetic curiosity and performs no useful function.

After all it’s supposed to launch applications this part of the tutorial covers various method using PHP to do this.

Detached process

Problem

PHP functions such as exec(), system(), passthru() and even back ticks start a process and wait for any data to be returned from the process before continuing onto the next instruction. Waiting for a process to complete, clearly is not desirable for our tray menu.

Solution

Any process our tray-menu starts must be detached. I covered a solution to this problem sometime ago see page Detached Processes for details.

For convenience I have reproduced the solution below:

A neat one line solution is provided using the PHP function popen().


pclose(popen($command,'r'));

 

  • It creates a pipe (popen) to a process for reading (‘r’)
  • Process is specified by $command
  • Instantly closes (pclose) the file pointer created hence detaching the process.

Top

Commands

So what can be passed to the above function more importantly what do we require for a command processing function?

Answer is to run a few test cases for example:

Edit file test_case_2.phpw un-comment one of the command ($cmd) lines and run test_case_2.bat

<?php
chdir(dirname(__FILE__)); // Change wd to this files location

//$cmd = "start \"Test 1\" cmd.exe ";                      // open a command window set title
//$cmd = "start cmd.exe ";                                 // open a command window
//$cmd = "start notepad.exe ";                             // open notepad
//$cmd = "start notepad.exe test.txt";                     // open a text file into notepad
//$cmd = "start test.txt";                                 // open file text.txt in default editor
//$cmd = "start http://localhost/";                        // open file index.* in local browser
//$cmd = "start ../test.txt";                              // open file text.txt in default editor
                                                           // one folder up relative to this test file
//$cmd = "start notepad.exe \"C:/z test/test.txt\"";       // Space in path to file

//$cmd = "start php.exe -n rollover_1.phpw";               // Run Winbinder PHP script
                                                           // Command window  displayed

//$cmd = "start php-win.exe -n rollover_1.phpw";           // Run Winbinder PHP script
                                                           // Command window not displayed

//=== Examples using a PHP configuration file  
//$cmd = "start php.exe -c php-wb.ini rollover_1.phpw";     // Run Winbinder PHP script
                                                            // Command window  displayed

//$cmd = "start php-win.exe -c php-wb.ini rollover_1.phpw"; // Run Winbinder PHP script
                                                            // Command window not displayed

pclose(popen($cmd,'r'));                                    // Run a detatched process

If the penny has not dropped there is no real difference between command format.

They all require a path to the executable either directly or implied and an optional parameter.

Since there is no difference our action options reduce

  • From shell, run, sub, separator
  • To run, sub, separator


Top

Hidden Process

Perhaps a hidden process requires a special command entry.

Most processes are easy to hide use one of the following methods:

$cmd = "start /b php.exe -n rollover_1.phpw"; 

 

Script is run in the background any scripts run from this inherits the hidden environment hence are hidden.

$cmd = "start php-win.exe -n rollover_1.phpw";  

 

The command window is hidden and the windows application is visible

Reason for showing the above is to look at the command format. Again in terms of the command processing there is nothing special required.

Persistent

The following issues may require a special command entry.

Certain processes are very persistent and are difficult to hide in particular Apache when run as a standard program. It opens a new command window and runs in that.

Similarly if a process runs an infinite loop within a command window that window will remain visible. Another annoying scenario is where a process periodically fires-up and produces a pop-up command window to run a cron process.

Solution is to use Uniform Server's utility uniserv.exe (2.5k) this was specifically designed to run Apache in the background hidden and can be used to resolve the above issues. Once a process is hidden it indirectly hides the task bar icon.

$cmd = "start uniserv.exe \"php.exe test_case_3_a.phpw\""; // Hide app 

In terms of command format there is no difference.

However from a user point of view could use a command such as “runh” for run hidden.

Top

Test Example

The following example shows how to use Uniform Server’s process hide utility.

test_case_3.bat

php.exe -n   test_case_3.phpw
pause

test_case_3.phpw

<?php
chdir(dirname(__FILE__)); // Change wd to this files location

//$cmd = "start php.exe -n test_case_3_a.phpw";                 // #1 
//$cmd = "start uniserv.exe \"php.exe -n test_case_3_a.phpw\""; // #2 

pclose(popen($cmd,'r'));                 // Run a detatched process
?>

test_case_2.phpw

<?php
chdir(dirname(__FILE__)); // Change wd

$loop=0;
while($loop <= 10 ){
  sleep(1);
   file_put_contents("z_test.txt",$loop,FILE_APPEND);
   $loop = $loop+1;
}
?>

 

test_case_3.bat

Runs script test_case_3.phpw (batch file required only for testing)

test_case_3.phpw

Runs script test_case_3_a.phpw

Test 1:

  • Un-comment line #1 and run the batch file test_case_3.bat
  • A command window opens and runs script test_case_3.phpw.
  • Shortly after a second command window opens. Showing script test_case_3_a.phpw is running.
  • This can be confirmed because it creates a file z_test.txt
  • After ten seconds script completes and the command window automatically closes.


Test 2:

  • Delete file z_test.txt
  • Un-comment line #2 and run the batch file test_case_3.bat
  • A command window opens and runs script test_case_3.phpw.
  • Shortly after script test_case_3_a.phpw is run.
  • Note: This time a second command window does not opens (runs in background).
  • To confirm test_case_3_a.phpw has been run check file z_test.txt was created

Note: In both cases instantly returns to script test_case_3.bat because script test_case_3_a.phpw is detatched.

Top

Pattern

There should be no difference in text entered by a user to run a command other than the commadnd itself (run or runh) Is this acheiveable? I have rearranged the two lines as shown below:

$cmd = "start                 php.exe -n test_case_3_a.phpw          " ;             
$cmd = "start uniserv.exe \"  php.exe -n test_case_3_a.phpw    \"    "; 

Ignoring text to the left and right of php.exe -n test_case_3_a.phpw demonstates there is no problem

Top

Summary

The above has shown how to read a configuration file into an array.

This array is scanned extracted data is combined with positioning information to form a master array.

Our tray-menu contains unresolved details such as what elements to include in the master array and configuration file. Next page covers coding and puts a stake in the ground.

Top