1<?php
2
3/*
4 * This file is part of the Silex framework.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Silex\Provider;
13
14use Pimple\Container;
15use Pimple\ServiceProviderInterface;
16use Silex\Application;
17use Silex\Api\BootableProviderInterface;
18use Symfony\Component\VarDumper\VarDumper;
19use Symfony\Component\VarDumper\Cloner\VarCloner;
20use Symfony\Component\VarDumper\Dumper\CliDumper;
21
22/**
23 * Symfony Var Dumper component Provider.
24 *
25 * @author Fabien Potencier <fabien@symfony.com>
26 */
27class VarDumperServiceProvider implements ServiceProviderInterface, BootableProviderInterface
28{
29    public function register(Container $app)
30    {
31        $app['var_dumper.cli_dumper'] = function ($app) {
32            return new CliDumper($app['var_dumper.dump_destination'], $app['charset']);
33        };
34
35        $app['var_dumper.cloner'] = function ($app) {
36            return new VarCloner();
37        };
38
39        $app['var_dumper.dump_destination'] = null;
40    }
41
42    public function boot(Application $app)
43    {
44        if (!$app['debug']) {
45            return;
46        }
47
48        // This code is here to lazy load the dump stack. This default
49        // configuration for CLI mode is overridden in HTTP mode on
50        // 'kernel.request' event
51        VarDumper::setHandler(function ($var) use ($app) {
52            VarDumper::setHandler($handler = function ($var) use ($app) {
53                $app['var_dumper.cli_dumper']->dump($app['var_dumper.cloner']->cloneVar($var));
54            });
55            $handler($var);
56        });
57    }
58}
59