PHP PORTABLE IDE: Notepad++ Debugging 2
US PHP IDE : Introduction | Notepad++ | Install PHP | NppExec Config | PHP extensions | Run Menu | XDebug 1 | Debugging 1 | XDebug 2 | Debugging 2
|
|
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
|
<?php $a=10; function get_b(){ xdebug_break() ; $b=222; return $b; } ?> |
File name: server_debug_include_1.php
|
Save both files to Uniform Server's root folder UniServer\www
Debugging process 1
A debugging process initiated from browser
|
In the above example using short scripts does not do the debugging process justice however it provides provide an indication of XDebug’s power.
Debugging process 2
A debugging process can be initiated from Notepadd++
|
Note: Clicking Run>Run PHP script on server runs the redirection script C:\us_portable_ide\unicode\redirect.php
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++
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:
Opens Script name side window:
|
Following assigns new command script to menu items.
Check new menu item added to macro drop-down 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:
|
Check macro drop-down menu:
|
Above provides two options for running command scripts either via the context menu or from the macro drop-down menu.
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
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 === ?>
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.
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)
Summary
That concludes our IDE nothing is written in stone and hopefully you will tailor it to meet you own requirements.