PHP PORTABLE IDE: Notepad++ Debugging 2

From The Uniform Server Wiki
Jump to navigation Jump to search

 

MPG UniCenter

Uniform Server Portable PHP IDE.

Notepad++ Debugging 2

This page provides a recap of material already covered. Currently our IDE has the capability of debugging WebPages on Uniform Server. Sequence is straightforward first start Uniform Server and then Notepad++ and its client. Initiate a debugging secession by entering a page to be debugged in PC’s default browser.


An alternative to initiating a debugging session from a browser is to run a small script via Notepad++. Currently this script has limitations it can only redirect to pages contained in root folder WWW. This page covers script modifications to initiate debugging from sub-folders.

Recap example

Test Scripts

For testing we will use the following two scripts:

<?php
include_once "server_debug_include_1.php";
$b=get_b();
$c=$a+$b;
print "Result a = $a <br />";
//xdebug_break() ;
print "Result b = $b <br />";
print "Result c = $c <br />";
?>

File name: server_debug_main.php

  • Includes a file server_debug_include_1.php containing function get_b()
  • Main file is assumed to be large and above function call berried deep in this file.
<?php
$a=10;
function get_b(){
xdebug_break() ;
 $b=222; 
 return $b;
}
?>

File name: server_debug_include_1.php

  • Include file contains function get_b()
  • Assume there is a problem with this function!
  • Insert a break-point using function xdebug_break()
  • Again file is assumed to be large.

Save both files to Uniform Server's root folder UniServer\www

Top

Debugging process 1

A debugging process initiated from browser

  • Start Uniform Server
  • Start default browser
  • Start Notepad++
  • Disable first line break:
    • We don't want to break on first line of script hence disable this
    • Open DBGp configuration window Plugins>DBGp>Config...
    • Step D) Un-Check Break at first line when debugging starts
    • Leave all other settings as they are
    • Step E) Click OK
  • Click debugger icon - starts debugger
  • Type the following into browser: http://localhost/server_debug_main.php
    • Browser displays page loading
    • Notepadd++ tray icon flashes
  • Edit window automatically displays page server_debug_include_1.php
    • Green arrow points to line xdebug_break()
  • Click Step Into and select Local context tab.
    • Variables are displayed.
  • Click Step Into until a value is displayed for $b
    • If this value is incorrect change this using Eval button.
  • With green arrow pointing to return $b Click Step Into
    • This completes function and returns to calling script
    • Calling script (server_debug_main.php) is loaded into edit window.
  • Continue debugging or compete run by pressing Run button - Runs to next break-point or script end

In the above example using short scripts does not do the debugging process justice however it provides provide an indication of XDebug’s power.

Top

Debugging process 2

A debugging process can be initiated from Notepadd++

  • Start Uniform Server
  • Start Notepad++
  • Disable first line break:
    • We don't want to break on first line of script hence disable this
    • Open DBGp configuration window Plugins>DBGp>Config...
    • Step D) Un-Check Break at first line when debugging starts
    • Leave all other settings as they are
    • Step E) Click OK
  • Click debugger icon - starts debugger
  • Load page UniServer\www\server_debug_main.php into Notepadd++
  • In Notepad++ click Run>Run PHP script on server
    • Opens browser if not already running
    • Browser displays page loading
    • Notepadd++ tray icon flashes
  • Edit window automatically displays page server_debug_include_1.php
    • Green arrow points to line xdebug_break()
  • Click Step Into and select Local context tab.
    • Variables are displayed.
  • Click Step Into until a value is displayed for $b
    • If this value is incorrect change this using Eval button.
  • With green arrow pointing to return $b Click Step Into
    • This completes function and returns to calling script (server_debug_main.php)
  • Continue debugging or compete run by pressing Run button - Runs to next break-point or script end

Note: Clicking Run>Run PHP script on server runs the redirection script C:\us_portable_ide\unicode\redirect.php

Top

Context Menu

Running the redirection script from Notepad++ run drop-down menu means it cannot be run from the context menu (right click in edit window).

Hence the entry in the shortcuts file C:\us_portable_ide\Unicode\shortcuts.xml is removed.

Remove shortcuts line

Line to be removed

 <Command name="Run PHP script on server" Ctrl="no" Alt="no" Shift="no" Key="0">$(NPP_DIRECTORY)\php\php-win.exe -c
$(NPP_DIRECTORY)\php\php_cli_ide.ini $(NPP_DIRECTORY)\redirect.php $(NPP_DIRECTORY) $(FILE_NAME)</Command>

Remember file shortcuts.xml cannot be directly edited using Notepad++ it is overwritten with original values when Notepad++ is closed.

How to edit shortcuts.xml

  • Copy file C:\us_portable_ide\unicode\shortcuts.xml to folder C:\us_portable_ide\php_scripts
  • Start Notepad++
  • Open file C:\us_portable_ide\php_scripts\shortcuts.xml
  • Remove the above line top of <UserDefinedCommands> section
  • Save file and close Notepad++
  • Copy file C:\us_portable_ide\php_scripts\shortcuts.xml to folder C:\us_portable_ide\unicode
  • Start Notepad++

Top

Add new command

A new command list is created and saved as item Run Script On Server

Command list:

NPP_SAVE
cls
$(NPP_DIRECTORY)\php\php.exe -c $(NPP_DIRECTORY)\php\php_cli_ide.ini $(NPP_DIRECTORY)\redirect.php $(NPP_DIRECTORY)$(FILE_NAME)

Add new command script as follows:

  • Press key F6
  • Step A) From drop-down menu select <temporary script>
  • Step B) Enter list of commands
  • Step C) Click save button

Opens Script name side window:

  • Step D) Enter a name for command script run_script_on_server
  • Step E) Click save
  • Step F) Click OK - Saves and runs script.

Top

Assign command script to menu items

Following assigns new command script to menu items.

  • Open NppExec Advanced Options Plugins>NppExec>Advanced Options..
  • Step G) Check box Place to the Macro submenu
  • Step H) From the drop-down menu select command script run_script_on_server
  • Step I) Enter Item name Run Script On Server
  • Step J) Click Add/Modify Menu Items are automatically inserted.
  • Step K) Click OK and Restart Notepad++.

Check new menu item added to macro drop-down menu.

Top

Add menu items to Context menu

Add the above four menu items to context menu as follows:

  • Settings>Edit Popup ContexMenu
  • A warning is given, after saving edits restart Notepadd++
  • Context menu displayed in edit window.
  • Just below this line:
        <Item MenuEntryName="Edit" MenuItemName="Select all"/>
  • Add the following lines:
	<!-- id="0" is the separator -->
        <Item id="0"/>
		
	<Item  PluginEntryName="NppExec" PluginCommandItemName="Check PHP Syntax"/>
	<Item  PluginEntryName="NppExec" PluginCommandItemName="Run as PHP CLI"/>
	<Item  PluginEntryName="NppExec" PluginCommandItemName="Run PHP CLI Debug"/>
	<Item  PluginEntryName="NppExec" PluginCommandItemName="Run Script On Server"/>	
  • Save and restart Notepad++

Check Context menu:

  • Right click in edit window
  • Context menu displayed
  • New entries A-D

Check macro drop-down menu:

  • Click Macro
  • Corresponding entries displayed
  • Entries A-D

Above provides two options for running command scripts either via the context menu or from the macro drop-down menu.

Top

Run Script On Server

The redirection script redirect.php is intended only to provide a working example hence has a few limitations. Currently it only runs files being edited in Uniform Server’s root folder WWW and redirect to standard port 80.

This section addresses the above limitations.

Command list

Currently two parameters $(NPP_DIRECTORY) and $(FILE_NAME) are passed to our redirection script. These parameters lack a file’s sub-folder information hence replace $(FILE_NAME) with $(FULL_CURRENT_PATH) which contains the information we want:

Updated command list:

NPP_SAVE
cls
$(NPP_DIRECTORY)\php\php.exe -c $(NPP_DIRECTORY)\php\php_cli_ide.ini $(NPP_DIRECTORY)\redirect.php
 $(NPP_DIRECTORY)$ $(FULL_CURRENT_PATH)

Update command list

  • Press key F6
  • From the drop down menu select Run Script On Server
  • Delete parameter $(FILE_NAME)
  • Add parameter $(FULL_CURRENT_PATH) as shown above
  • Click OK

Top

New redirection script

A user can move Uniform Server to different ports. Since we can determine server’s root folder from a file being edited on the server it is easy to directly extract port from Apache’s configuration file.

New redirection file:

<?php
/*
###############################################################################
# Name: redirect.php
# Developed By: The Uniform Server Development Team
# Modified Last By: Mike Gleaves (Ric)
# Web: http://www.uniformserver.com
# V1.0 25-11-2010
##############################################################################
*/
// Args passed 
// $(NPP_DIRECTORY)     - Notepad++ root folder
// $(FULL_CURRENT_PATH))- Absolute path to currently displayed script


$domain_name = "localhost";      // Domain name default localhost 
$file_name   = "redirect.html";  // File to create for redirect

if($argc != 3){                  // Are two args passed
 exit;                           // No: Give up
}
$npp_root  = str_replace('\\', '/', $argv[1]);   // Notepad++ root folder
$page_path = str_replace('\\', '/', $argv[2]);   // Absolute path to page file

$pos = strpos($page_path, "www");      // Look for root folder www
if ($pos === false){                   // No root folder
 print " Root folder www not found\n"; // Inform user
 exit;                                 // Give up
}

$url_part_array = explode("www",$page_path);  // Split path
$url_part = $url_part_array[1];               // [0] Gives root/  [1] Gives /folders/file


$port = ide_get_apache_port($url_part_array[0]);  //


$redirect_array = array();                       // Create new array and reset
$redirect_array[] = '<html><head>';
$redirect_array[] = '<meta http-equiv="refresh" content="1;url=http://'.$domain_name.':'.$port.$url_part.'">';
$redirect_array[] = '<title>Uniform Server Redirect</title>';
$redirect_array[] = '</head><body></body></html>';

$string = implode("\n",$redirect_array);                   // Convert array to string
file_put_contents("$npp_root/$file_name",$string);         // Save string to file

// Run redirect file
$cmd= "$npp_root/$file_name";                            // Command to run
$WshShell = new COM("WScript.Shell");                    // Open com object
$oExec = $WshShell->Run($cmd);                           // Run command


//=== Get Apache port =========================================================
function ide_get_apache_port($root){
  if ($filearray=file($root.'usr/local/apache2/conf/httpd.conf')) {     // read file into array
    foreach ($filearray as $txt) {           // scan array for port
                                             // check $text line save $matches 
     if(preg_match("/^Listen\s+(\d+)/", $txt,$matches)){  
       $apache_port =  $matches[1];          // match found save port number 
       break;                                // give up nothing else to do
     }
    }
  }
  else {                                     // failed to read file
    return "80";                             // Use default port
  }
  return $apache_port;
}
//===================================================== END Get Apache port ===
?>

Top

Plugin

At this stage we have an IDE that is independent of Uniform Server. Only interaction when files are edited directly from the server’s root folder or its sub-folders.

IDE size is currently 17.0 MB the integrated PHP interpreter takes up 11.9 MB hence converting our IDE to a plugin will considerably reduce its size.

Top

What requires changing?

Changes required:

  • Decide on a suitable location for IDE for example UniServer\us_portable_ide
  • Copy configuration files php_cli_ide.ini and php_cli_ide_debug.ini to folder UniServer\usr\local\php
  • Copy appropriate XDebug (VC6 or VC9) extension to UniServer\usr\local\php\extensions
  • Update paths in command scripts (see below)

File us_portable_ide\unicode\plugins\Config\npes_saved.txt contains our four command line scripts:

::run_php_cli
NPP_SAVE
cls
$(NPP_DIRECTORY)\php\php.exe -c $(NPP_DIRECTORY)\php\php_cli_ide.ini $(FULL_CURRENT_PATH)
::php_syntax_check
NPP_SAVE
cls
$(NPP_DIRECTORY)\php\php.exe -ln $(FULL_CURRENT_PATH)
::run_php_cli_debug
NPP_SAVE
cls
$(NPP_DIRECTORY)\php\php.exe -c $(NPP_DIRECTORY)\php\php_cli_ide_debug.ini $(FULL_CURRENT_PATH)
::run_script_on_server
NPP_SAVE
cls
$(NPP_DIRECTORY)\php\php.exe -c $(NPP_DIRECTORY)\php\php_cli_ide.ini  $(NPP_DIRECTORY)\redirect.php
 $(NPP_DIRECTORY) $(FULL_CURRENT_PATH)

Note: Paths are defined as relative to the plugin for example:

$(NPP_DIRECTORY)\php\php.exe becomes $(NPP_DIRECTORY)\..\..\usr\local\php\php.exe

Following shows updated file:

::run_php_cli
NPP_SAVE
cls
$(NPP_DIRECTORY)\..\..\usr\local\php\php.exe -c $(NPP_DIRECTORY)\..\..\usr\local\php\php_cli_ide.ini $(FULL_CURRENT_PATH)
::php_syntax_check
NPP_SAVE
cls
$(NPP_DIRECTORY)\..\..\usr\local\php\php.exe -ln $(FULL_CURRENT_PATH)
::run_php_cli_debug
NPP_SAVE
cls
$(NPP_DIRECTORY)\..\..\usr\local\php\php.exe -c $(NPP_DIRECTORY)\..\..\usr\local\php\php_cli_ide_debug.ini $(FULL_CURRENT_PATH)
::run_script_on_server
NPP_SAVE
cls
$(NPP_DIRECTORY)\..\..\usr\local\php\php.exe -c $(NPP_DIRECTORY)\..\..\usr\local\php\php_cli_ide.ini
  $(NPP_DIRECTORY)\redirect.php $(NPP_DIRECTORY) $(FULL_CURRENT_PATH)

Top

Summary

That concludes our IDE nothing is written in stone and hopefully you will tailor it to meet you own requirements.

Top