PhpToolCase
Api Documentation Version 0.9.2
|
The goal of this class is to ease the process of debugging php scripts. The class uses a floating panel at the top right of the page. There are also a couple of features that can fix security issues with the php default error handler. All class properties and methods are static because it's required to let them work on script shutdown when FATAL error occurs.
Main Features:
$GLOBALS
, $_POST
, $_GET
, $_COOKIE
...) "Chrome Browser"
only)
The PtcDebug component works right out of the box, by calling the method PtcDebug::load():
This will initialize the component, but keep in mind that there are more features that can be configured on initialization.
The followig paragraph will explain all possible options that can be passed to the class when the method PtcDebug::load( ) is called.
This is the full list of options available on intialization, stored in PtcDebug::$_defaultOptions property:
The following sections will describe these options in details.
To show the interface we use a "$_GET"
parameter in the url (see url_pass), and to turn it off the same parameter + "_off"
can be used. The default key is "debug"
and should be changed to something else!
Default value: debug
(string)
The value to pass in the url (see url_key), the default value is "true"
and should be changed to something else!
Default value: true
(string)
Replaces the default php error handler to show php errors in the debug messages panel.
Default value: true
(bool)
This options controls the error_report level of your application.
Default value: E_ALL
(constant)
This options set to "true"
, can catch uncaught exceptions and log them to the messages panel.
Default value: true
(bool)
This option, if set to "true"
, will check for the url_key and url_pass with the referer if any. This is quite usefull to debug ajax scripts with the show_interface option set to "false"
, and the debug_console set to "true"
.
Default value: false
(bool)
Set this option to false to continue if fatal error occurs, keep in mind that this will only work with the replace_error_handler option set to "true"
.
Default value: true
(bool)
Only for Chrome
Browser
, with php-console
addon installed. This feature is very usefull to debug ajax requests.
See Ajax Debugging for more information and detailed usage.
Default value: false
(bool)
To restrict access use this option to pass an ip or an array of ip's, and only the allowed ip's will be able to turn on/off the debug interface.
Default value: null
(string|array)
For persistent debugging, set this option to "true"
, and the class will try to store the url_key and url_pass in a "$_SESSION"
var. You will need to use url_key + "_off" to hide the interface.
Default value: false
(bool)
This option, if set to "false"
, does not show the floating panel. It is usefull to send messages to console only with the option debug_console set to "true"
.
See Ajax debugging (chrome only) for more details over this setup.
Default value: true
(bool)
Sets the php global execution time limit.
Default value: null
(float)
Sets memory limit for php.
Default value: null
(float)
Controls if the messages panel will be shown, if set to "false"
the interface will not show this panel and will be faster to load. This option can be turned on and off with the "$_GET"
parameter "hidepanels"
in the url.
see Hiding Panels for more information about this option.
Default value: true
(bool)
Controls if the "$GLOBALS"
variable will be shown in the vars panel, if set to "false"
the interface will not show the "$GLOBALS"
and will be faster to load. This option can be turned on and off with the "$_GET"
parameter "hidepanels"
in the url.
see Hiding Panels for more information about this option.
Default value: true
(bool)
This option controls if the sql panel will be shown, if set to "false"
the interface will not show this panel and will be faster to load. This option can be turned on and off with the "$_GET"
parameter "hidepanels"
in the url.
see Hiding Panels for more information about this option.
Default value: true
(bool)
This option controls if the w3c validator panel will be shown, if set to "false"
the interface will not show this panel and will be faster to load. This option can be turned on and off with the "$_GET"
parameter "hidepanels"
in the url.
see Hiding Panels for more information about this option.
Default value: true
(bool)
By default the html output of the class is compressed. To print the output uncompressed set minified_html to "false"
.
Default value: true
(bool)
This option sets a depth limit for the backtrace. Increase it if you think that the backtrace is leaving out files.
Default value: 10
(float)
This option sets the maximum depth when dumping variables to prevent recursion. If maximum depth is reached, the message "**MAX DEPTH REACHED**"
will be shown. Increase it if you need a higher depth.
Default value: 6
(float)
This option sets the top position in pixels
for the floating panel, change it to lower the panel.
Default value: 0px
(string)
This option sets the right position in pixels
for the floating panel, change it to move the panel more to the left.
Default value: 0px
(string)
The default category for the log/sql messages.
Default value: General
(string)
This option, enables the variable inspector, which can be used to watch if a given variable has changed. The backtrace will be more precise if the PtcDebug::watch( ) method is used inside a "declare(ticks=n){}"
code block, instead of relying on the ticks
declaration present at the beginning of the PtcDebug class file.
See Inspecting Variable Changes for example usages.
Default value: true
(bool)
This option turns on/off the code coverage analisys engine to check which lines of code where executed. Set this option to "full"
to check all your application.
See Code Coverage Analysis for example usages.
Default value: true
(bool|string)
"full"
will slow down code execution. This option turns on/off the function calls tracing engine to trace function calls and passed arguments of your application. Set this option to "full"
to check all your application.
See Function Calls Tracing for example usages.
Default value: true
(bool|string)
"full"
will slow down code execution.This option controls categories that should be discarded from the log. Quite usefull to exclude categories making the output faster to load.
Set this option to "null"
to show all categories.
Default value: array
( 'Event Manager' , 'Autoloader' ) (mixed)
The PtcDebug component offers many ways to log data, the next paragraphs will demostrate the various possibilities.
To send data to the messages panel is quite simple, just use the PtcDebug::bufferLog( ) method:
To log sql queries use the PtcDebug::bufferSql( ) method:
The class allows to time execution for php code and sql queries. The following sections will give examples on how this can be done.
To check how long things take to execute you can use the PtcDebug::stopTimer( ) method:
To attach the result of the loop to the message, use the PtcDebug::addToBuffer( ) method:
See Attaching Data To Messages for details over the PtcDebug::addToBuffer( ) method.
To check how long a query takes to execute you can use the PtcDebug::stopTimer( ) method:
To attach the result of the query to the message, use the add_to_log() function:
See Attaching Data To Messages for details over the PtcDebug::addToBuffer( ) method.
The method PtcDebug::setErrorHandler() is used to force the replacement of the built in php error handler even if the debug panel is not shown (see replace_error_handler). This is usefull for production environment, for the simple reason that if an error occurs the user will not see information about the error. This method could also be used with the parameter set to "false"
, so that if fatal error occurs, script execution will still continue.
"true"
(see die_on_error).To check if a given variable has changed, the method PtcDebug::watch() with the option enable_inspector set to "true"
can be used:
The code coverage analysis tool can check which lines of code where executed in your application, included files will be added to the report aswell.
To use this features, the methods PtcDebug::startCoverage( ) and PtcDebug::stopCoverage( ) can be used:
"full"
.The PtcDebug class is also capable of tracing function calls, creating a report showing the backtrace and arguments passed to each function call made in your application.
To use this feature, the methods PtcDebug::startTrace( ) and PtcDebug::stopTrace( ) can be used:
"full"
.Data can be attached to a message after PtcDebug::bufferLog( ) and PtcDebug::bufferSql() methods are called, with the PtcDebug::addToBuffer( ) method. This is quite usefull if you are timing the execution of some code or a database query. The parameter "$reference"
is the equivalent of the "$statement"
parameter for PtcDebug::bufferLog( ) or PtcDebug::bufferSql() methods which should be called before PtcDebug::addToBuffer( ).
See Execution Timing for practical examples.
The PtcDebug class has a built-in engine for reading source code. This option is automatically available if session_start()
is used, the file names in the panels will have an anchor that will open a popup window with the content of the file and the highlighted line of code if any.
The PtcDebug class has a built-in engine for searching for a string recursively inside a given path. This option is automatically available if session_start()
is used, the file analysis panel will have a form that will open a popup with the search results upon submission.
To load the interface faster, panels that are not required can be hidden. This can be done either by using the options available on initialization (see show_messages, show_globals, show_sql, show_w3c) or by sending the "$_GET"
parameter "hidepanels"
in the url.
These are all the possibile options for the "hidepanels"
parameter:
The following sections will demonstrate 3 different setup examples depending on the circumstances.
This example,demostrates how to configure and load the interface under testing environment:
This example demostrates how to configure and safely load the interface under production environment:
This example demostrates how to configure and show the buffer messages in the "Chrome Browser"
js console, which is particularly usefull to debug ajax scripts. Keep in mind that this setup needs phpConsole
class and the "Chorme Browser" addOn installed to work. Make sure you have downloaded the phpConsole
class from the repository, and install the Chrome Browser addOn before starting to use this class to send messages to the console.