Skip to content

Instantly share code, notes, and snippets.

@widnyana
Created June 21, 2016 10:23
Show Gist options
  • Select an option

  • Save widnyana/38e4b506be37cc1fe2a2eb7f3dfcad96 to your computer and use it in GitHub Desktop.

Select an option

Save widnyana/38e4b506be37cc1fe2a2eb7f3dfcad96 to your computer and use it in GitHub Desktop.
Silex 1.3 VarDumperServiceProvider
<?php
namespace App\Providers;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\VarDumper;
class VarDumperServiceProvider implements ServiceProviderInterface {
/**
* Registers services on the given app.
*
* This method should only be used to configure services and parameters.
* It should not get services.
* @param Application $app
*/
public function register(Application $app)
{
$app['var_dumper.cli_dumper'] = function ($app) {
return new CliDumper($app['var_dumper.dump_destination'], $app['charset']);
};
$app['var_dumper.cloner'] = function ($app) {
return new VarCloner();
};
$app['var_dumper.dump_destination'] = null;
}
/**
* Bootstraps the application.
*
* This method is called after all services are registered
* and should be used for "dynamic" configuration (whenever
* a service must be requested).
* @param Application $app
*/
public function boot(Application $app)
{
if (!$app['debug']) {
return;
}
// This code is here to lazy load the dump stack. This default
// configuration for CLI mode is overridden in HTTP mode on
// 'kernel.request' event
VarDumper::setHandler(function ($var) use ($app) {
VarDumper::setHandler($handler = function ($var) use ($app) {
$app['var_dumper.cli_dumper']->dump($app['var_dumper.cloner']->cloneVar($var));
});
$handler($var);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment