Sage - Insightful PHP debugging assistant ☯

Overview

Sage - Insightful PHP debugging assistant

At first glance Sage is just a pretty replacement for var_dump() and debug_backtrace().

However, it's much, much more.


🔀 How is it different or better than symfony/var-dumper?

For an overview of Sage's outstanding features jump to the F.A.Q.

Installation and Usage

Download the phar and simply


require 'sage.phar';

d('hello 🌎!');

Or, if you use Composer:

composer require php-sage/sage --dev

Basic usage:

########## DUMP VARIABLE ###########################

sage($GLOBALS, $_SERVER); // any number of parameters

# or you can go shorter:
d($GLOBALS, $_SERVER);

# or you can go the verbose way, it's all the same:
Sage::dump($GLOBALS, $_SERVER); 




# s() will display a more basic, javascript-free display (but with colors)
s($GLOBALS, $_SERVER);

# prepending a tilde will make the output even more basic (rich->basic and basic->plain text)
~d($GLOBALS, $_SERVER); // how this works: https://stackoverflow.com/a/69890023/179104



########## DEBUG BACKTRACE #########################
Sage::trace();
// or via shorthand:
d(1);
// you can even pass the result of debug_trace and it will be recognized
Sage::dump( debug_backtrace() );



########## DUMP AND DIE #########################
dd($GLOBALS, $_SERVER); // dd() might be taken by your framework
ddd($GLOBALS, $_SERVER); // so here are some equivalent altenratives
saged($GLOBALS, $_SERVER);

sd($GLOBALS, $_SERVER); // for plain display



########## MISCELLANEOUS ###########################
# this will disable Sage completely
Sage::enabled(false);

ddd('Get off my lawn!'); // no effect

Tips & Tricks

This section will have to do until I muster up the motivation to do proper documentation.

  • Sage can provide a plain-text version of its output and does so automatically when invoked via PHP running in command line mode.

  • Sage::enabled(Sage::MODE_PLAIN); will switch to a js-free version, and Sage::enabled(Sage::MODE_TEXT_ONLY); will give you plain text output which you can save or pass around by first setting Sage::$returnOutput = true;

  • You can create your wrapper function with custom configuration and what not and Sage will treat it like one of its own:

function MY_dump($args)
{
    Sage::enabled(Sage::MODE_TEXT_ONLY);
    Sage::$returnOutput = true; // this configuration will persist for ALL subsequent dumps BTW!
    return d(...func_get_args());
}
Sage::$aliases = 'my_dump'; // let Sage know about it. In lowercase please.
  • Double clicking the [+] sign in the output will expand/collapse ALL nodes; triple clicking a big block of text will select it all.

  • Clicking the tiny arrow on the right of the output will open it in a separate window where you can keep it for comparison.

  • If a variable is an object, its classname can be clicked to open the class in your IDE.

  • There are a couple of real-time modifiers you can use:

    • ~d($var) this call will output in plain text format.
    • +d($var) will disregard depth level limits and output everything (careful, this can hang your browser on huge objects)
    • !d($var) will show uncollapsed rich output.
    • -d($var) will attempt to ob_clean the previous output - useful when Sage is obscured by already present HTML.

    Here's a little bit on how it works.

  • To change the theme, use Sage::$theme, available options are:

    • Sage::$theme = Sage::THEME_ORIGINAL;
    • Sage::$theme = Sage::THEME_LIGHT;
    • Sage::$theme = Sage::THEME_SOLARIZED;
    • Sage::$theme = Sage::THEME_SOLARIZED_DARK;

  • Sage also includes a naïve profiler you may find handy. It's for determining relatively which code blocks take longer than others:

Sage::dump( microtime() ); // just pass microtime()
sleep( 1 );
Sage::dump( microtime(), 'after sleep(1)' );
sleep( 2 );
ddd( microtime(), 'final call, after sleep(2)' );


Configuration

Sage is designed with the utmost care to be as usable and useful out of the box, however there are several configuration options for you to customize.

Where to store configuration?

If you use the phar version it does not get simpler:

require 'sage.phar';
Sage::$theme = Sage::THEME_LIGHT;

For composer you have several options:

  1. Create a separate PHP config file and ask composer to autoload it for you:

    Add this entry to the autoload.files configuration key in composer.json:

"autoload": {
  /* ... */
  "files": [
    "config/sage.php" /* <--------------- this line */
  ]
},
  1. Put settings inside of php.ini:
; change sage theme:
sage.theme = solarized-dark
; always display full trace:
sage.maxLevels = 0
; it's been 10 years, and this is still not working, Jetbrains, PLEASE!:
sage.fileLinkFormat = phpstorm://open?file=%f&line=%l
; and so on
  1. Include the desired settings in your bootstrap process anywhere™.

F.A.Q.

💬 What sets Sage apart from symfony/var-dumper?

  • Visible Variable name
  • Keyboard shortcuts. Type d and the rest is just self-explanatory.
  • Debug backtraces with full insight of arguments, callee objects and more. Trace view
  • Custom display for a lot of recognized types:
  • Has text-only, plain and rich views, is trivial to configure, has several visual themes - actually created by a pro designer.
  • A huge amount of small usability enhancements - like the (clickable) call trace in the footer of each output.
  • Supports convenience modifiers, for example @sage($var); will return instead of outputting, -sage($var); will ob_clean all output to be the only thing on page.
  • Supports PHP 5.1+. That's the lowest physically possible version to extend compatibility to. Next time you headbang on something incredibly legacy, remember Sage!
  • Is way less complex - to read if you want to learn a bit of PHP internals.

💬 How is it worse?

  • Does not come pre-bundled with your cool framework (but it is zero-setup!)
  • Although Sage predates var-dumper, and I'm pretty sure it "inspired" the widespread use of the wonderful shorthand dd, I stepped down to let var-dumper use this name. To dump & die with Sage you can ddd() or saged()
  • There's no such feature as a dump server, at least until someone convinces me it's actually useful.
  • It's not made by Symfony foundation nor does it have industry-grade backing & support. It's made buy just this one guy (and contributors) since pre-2012.

💬 How is var_dump - style debugging still relevant when we have Xdebug?

  1. In practice, Xdebug is quite often very difficult and time-consuming to install and configure.
  2. There's many usecases where dump&die is just faster to bring up.
  3. There is no way you can visualise a timeline of changed data with XDebug. For example, all values dumped from within a loop.
  4. And there's more subtle usecases, eg. if you stepped over something there's no way to go back, but with var-dumping the values of interest are still there in the output...

I use xdebug almost daily, by the way. Side by side with Sage.

💬 Why does this look so much like Kint?

Because it IS Kint, and I am its author, however the project was blatantly stolen from me by a malicious contributor!

Instead of fighting windmills, I chose to fork and rename the last good version and continue under a new name!


Author

Rokas Šleinius (Raveren)

License

Licensed under the MIT License


Hope you love using Sage as much as I love creating it!

You might also like...
Debug bar for PHP
Debug bar for PHP

PHP Debug Bar Displays a debug bar in the browser with information from php. No more var_dump() in your code! Features: Generic debug bar Easy to inte

PHP Benchmarking framework
PHP Benchmarking framework

PHPBench is a benchmark runner for PHP analogous to PHPUnit but for performance rather than correctness. Features include: Revolutions: Repeat your co

The Interactive PHP Debugger

The interactive PHP debugger Implemented as a SAPI module, phpdbg can exert complete control over the environment without impacting the functionality

Dontbug is a reverse debugger for PHP
Dontbug is a reverse debugger for PHP

Dontbug Debugger Dontbug is a reverse debugger (aka time travel debugger) for PHP. It allows you to record the execution of PHP scripts (in command li

PHP Debug Console
PHP Debug Console

PHP Console A web console to try your PHP code into Creating a test file or using php's interactive mode can be a bit cumbersome to try random php sni

Php Debugger to run in terminal to debug your code easily.
Php Debugger to run in terminal to debug your code easily.

What is Dephpugger? Dephpugger (read depugger) is an open source lib to make a debug in php direct in terminal, without necessary configure an IDE. Th

PCOV - CodeCoverage compatible driver for PHP

PCOV A self contained CodeCoverage compatible driver for PHP Requirements and Installation See INSTALL.md API /** * Shall start recording coverage in

Low-overhead sampling profiler for PHP 7+
Low-overhead sampling profiler for PHP 7+

phpspy phpspy is a low-overhead sampling profiler for PHP. For now, it works with Linux 3.2+ x86_64 non-ZTS PHP 7.0+ with CLI, Apache, and FPM SAPIs.

The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. It provides a better dump() function that you can use instead of var_dump().

VarDumper Component The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. It provides a better dump() function t

Comments
  • DOM functions show no dumped data

    DOM functions show no dumped data

    Trying to use sage when debugging DOM manipulation with https://www.php.net/manual/en/book.dom.php functions, dump shows no real info.

    DOMNodeList

    print_r output:

    DOMNodeList Object
    (
        [length] => 1
    )
    

    sage output (omitted header with variable name and trace string):

    DOMNodeList (0)
    

    DOMElement

    print_r output:

    DOMElement Object
    (
        [tagName] => span
        [schemaTypeInfo] =>
        [nodeName] => span
        [nodeValue] => This item is currently unavailable in your region
        [nodeType] => 1
        [parentNode] => (object value omitted)
        [childNodes] => (object value omitted)
        [firstChild] => (object value omitted)
        [lastChild] => (object value omitted)
        [previousSibling] => (object value omitted)
        [nextSibling] => (object value omitted)
        [attributes] => (object value omitted)
        [ownerDocument] => (object value omitted)
        [namespaceURI] =>
        [prefix] =>
        [localName] => span
        [baseURI] =>
        [textContent] => This item is currently unavailable in your region
    )
    
    

    sage output (omitted header with variable name and trace string):

    DOMElement (0)
    
    opened by tfedor 5
  • Simple dump functions are not defined (Composer installation)

    Simple dump functions are not defined (Composer installation)

    Hi, I've installed sage with Composer in a pre-existing project, the setup is PHP 8.1 with some Symfony components (so dd is taken).

    I tested it by using the sage() and saged() functions and it works very well (backtrace dump works too). However, it seems that their shorthand versions - s() and sd() respectively - are actually returning simple dumps, while explicitly calling the simple dump functions - i.e., ssage() and ssaged() - raises a fatal error because they seem to be undefined.

    As far as I understand, the library injects functions in the global namespace, so it shouldn't be necessary to explicitly require the class and/or individual functions with use statements, correct?

    This is a sample output of calling saged('testing'):

    image

    while this is the output of sd('testing'):

    image

    Is this the expected behaviour?

    opened by nicospor 1
Releases(v1.3.0)
Owner
null
An elegant debug assistant for the Laravel framework.

Introduction Laravel Telescope is an elegant debug assistant for the Laravel framework. Telescope provides insight into the requests coming into your

The Laravel Framework 4.4k Dec 27, 2022
Xdebug — Step Debugger and Debugging Aid for PHP

Xdebug Xdebug is a debugging tool for PHP. It provides step-debugging and a whole range of development aids, such as stack traces, a code profiler, fe

Xdebug 2.8k Jan 3, 2023
Kint - a powerful and modern PHP debugging tool.

Kint - debugging helper for PHP developers What am I looking at? At first glance Kint is just a pretty replacement for var_dump(), print_r() and debug

null 2.7k Dec 25, 2022
Buggregator is a beautiful, lightweight web server built on Laravel and VueJs that helps debugging your app.

A server for debugging more than just Laravel applications. Buggregator is a beautiful, lightweight web server built on Laravel and VueJs that helps d

Buggregator 311 Jan 4, 2023
A collection of helper methods for testing and debugging API endpoints.

Laravel API Test Helpers This is a collection of helper methods for testing and debugging API endpoints. Installation You can install the package via

Stephen Jude 49 Jul 26, 2022
WordPress debugging made simple.

Loginator Debugging WordPress can sometimes be a pain, our goal is to make it easy, which is why Loginator was built with this in mind. From creating

Poly Plugins 2 Feb 12, 2022
The ultimate debugging and development tool for ProcessWire

Tracy Debugger for ProcessWire The ultimate “swiss army knife” debugging and development tool for the ProcessWire CMF/CMS Integrates and extends Nette

Adrian Jones 80 Oct 5, 2022
Handle PHP errors, dump variables, execute PHP code remotely in Google Chrome

PHP Console server library PHP Console allows you to handle PHP errors & exceptions, dump variables, execute PHP code remotely and many other things u

Sergey 1.4k Dec 25, 2022
PHP APM (Alternative PHP Monitor)

APM (Alternative PHP Monitor) APM (Alternative PHP Monitor) is a monitoring extension enabling native Application Performance Management (APM) for PHP

Patrick Allaert 310 Dec 4, 2022
Zipkin PHP is the official PHP Tracer implementation for Zipkin

Zipkin PHP is the official PHP Tracer implementation for Zipkin, supported by the OpenZipkin community. Installation composer require openz

Open Zipkin 250 Nov 12, 2022