Skip to content

Symfony2 bundle for Ladybug library, the Simple and Extensible PHP Dumper

Notifications You must be signed in to change notification settings

henter/LadybugBundle

 
 

Repository files navigation

LadybugBundle

Build Status

This bundle provides an easy and extensible var_dump/print_r replacement for Symfony2 projects, both in controllers or Twig templates. For example, with this bundle, the following is possible:

<?php
    class UserController extends Controller
    {
        public function userAction($username) {
            ladybug_dump($username); // or just ld($username)
        }
    }
{{ user.username|ladybug_dump }}

Getting as a result:

string(10) "raulfraile"

Installation

Step 1: Install vendors

To install this bundle, you'll need both the Ladybug library and this bundle. Installation depends on your version of Symfony:

Symfony 2.0.x: bin/vendors.php method

If you're using the bin/vendors.php method to manage your vendor libraries, add the following entries to the deps in the root of your project file:

[Ladybug]
    git=http://github.com/raulfraile/Ladybug.git
    target=ladybug

[RaulFraileLadybugBundle]
    git=http://github.com/raulfraile/LadybugBundle.git
    target=/bundles/RaulFraile/Bundle/LadybugBundle

Next, update your vendors by running:

$ ./bin/vendors

Finally, add the following entries to your autoloader:

<?php
// app/autoload.php

$loader->registerNamespaces(array(
    // ...
    'Ladybug'           => __DIR__.'/../vendor/ladybug/lib',
    'RaulFraile'        => __DIR__.'/../vendor/bundles',
));

Symfony 2.1.x: Composer

Composer is a project dependency manager for PHP. You have to list your dependencies in a composer.json file:

{
    "require": {
        "raulfraile/ladybug-bundle": "dev-master"
    }
}

To actually install Ladybug in your project, download the composer binary and run it:

wget http://getcomposer.org/composer.phar
# or
curl -O http://getcomposer.org/composer.phar

php composer.phar install

Step 2: Enable the bundle

Finally, enable the bundle in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...

        new RaulFraile\Bundle\LadybugBundle\RaulFraileLadybugBundle(),
    );
}

Examples

It is possible to dump any variable, including arrays, objects and resources:

Dumping an array

<?php
    $var = array(1, 2, 3);
    ladybug_dump($var)

Dumping an object

<?php
    $var = new Foo();
    ladybug_dump($var)

Dumping a GD image

<?php
    $img = imagecreatefrompng(__DIR__ . '/images/ladybug.png');
    ladybug_dump($img);

The same can be accomplished using the Twig filter ladybug_dump.

Helpers

The are 5 helpers that can be used in any controller:

ladybug_dump($var1[, $var2[, ...]]): Dumps one or more variables

ladybug_dump_die($var1[, $var2[, ...]]): Dumps one or more variables and terminates the current script

ladybug_dump_return($format, $var1[, $var2[, ...]]): Dumps one or more variables and returns the dump in any of the following formats:

  • yml: Returns the dump in YAML
  • json: Returns the dump in JSON
  • xml: Returns the dump in XML
  • php: Returns the dump in PHP arrays

ladybug_dump_ini([$extension]): Dumps all configuration options

ladybug_dump_ext(): Dumps loaded extensions

There are also some shortcuts in case you are not using this function names:

ld($var1[, $var2[, ...]]): shortcut for ladybug_dump

ldd($var1[, $var2[, ...]]): shortcut for ladybug_dump_die

ldr($format, $var1[, $var2[, ...]]): shortcut for ladybug_return

Only ladybug_dump can be used inside Twig templates.

Symfony command

There are two Symfony commands to dump an instance of a given class or export it to a file, in JSON, YAML or XML format.

# php app/console ladybug:dump class_name [--all]
# php app/console ladybug:export class_name target [--format=yaml]

php app/console ladybug:dump "Symfony\Component\HttpFoundation\Request"
php app/console ladybug:export "Symfony\Component\HttpFoundation\Request" export.json --format=json

Symfony profiler integration

Instead of printing out the dump tree inside the HTML document, you can use the Ladybug logger and see the results in a tab of the Symfony profiler:

To make use of the Ladybug logger, grab the ladybug service from the DIC, and call the log method:

<?php
class TestController
{
    public function testAction()
    {
        $var = 1;
        $this->get('ladybug')->log($var);    
    }

API reference

Ladybug automatically detects Symfony, Doctrine, Twig and Silex classes, and link them to the official documentation.

Configuration

You can configure ladybug library directly in your config.yml file. Here are the defaults:

raul_fraile_ladybug:  
    general:              
        expanded:             true 
    object:               
        max_nesting_level:    3 
        show_data:            true 
        show_classinfo:       true 
        show_constants:       true 
        show_methods:         true 
        show_properties:      true 
    array:                
        max_nesting_level:    8 
    processor:            
        active:               true 
    bool:                 
        html_color:           #008 
        cli_color:            blue 
    float:                
        html_color:           #800 
        cli_color:            red 
    int:                  
        html_color:           #800 
        cli_color:            red 
    string:               
        html_color:           #080 
        cli_color:            green 
        show_quotes:          true 
    css:                  
        path:                 /Asset/tree.min.css 

About

Symfony2 bundle for Ladybug library, the Simple and Extensible PHP Dumper

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published