Commit 27a37f52 authored by Fabien Potencier's avatar Fabien Potencier

added a provider for VarDumper

parent 24054b7b
...@@ -4,6 +4,7 @@ Changelog ...@@ -4,6 +4,7 @@ Changelog
2.0.0 (2015-XX-XX) 2.0.0 (2015-XX-XX)
------------------ ------------------
* added support for the Symfony VarDumper Component
* [BC BREAK] The app variable exposed in Twig is now an AppVariable instance * [BC BREAK] The app variable exposed in Twig is now an AppVariable instance
* [BC BREAK] CSRF has been moved to a standalone provider (``form.secret`` is not available anymore) * [BC BREAK] CSRF has been moved to a standalone provider (``form.secret`` is not available anymore)
* added support for the Symfony HttpFoundation Twig bridge extension * added support for the Symfony HttpFoundation Twig bridge extension
......
...@@ -19,3 +19,4 @@ Silex ...@@ -19,3 +19,4 @@ Silex
remember_me remember_me
serializer serializer
service_controller service_controller
var-dumper
VarDumperServiceProvider
========================
The *VarDumperServiceProvider* provides a mechanism that allows exploring then
dumping any PHP variable.
Parameters
----------
* **var_dumper.dump_destination**: A stream URL where dumps should be written
to (defaults to ``null``).
Services
--------
* n/a
Registering
-----------
.. code-block:: php
$app->register(new Silex\Provider\VarDumperServiceProvider());
.. note::
Add the Symfony VarDumper Component as a dependency:
.. code-block:: bash
composer require symfony/var-dumper
Usage
-----
Adding the VarDumper component as a Composer dependency gives you access to the
``dump()`` PHP function anywhere in your code.
Ig you are using Twig, it also provides a ``dump()`` Twig function and a
``dump`` Twig tag.
The VarDumperServiceProvider is also useful when used with the Silex
WebProfiler as the dumps are made available in the web debug toolbar and in the
web profiler.
...@@ -15,6 +15,7 @@ use Pimple\Container; ...@@ -15,6 +15,7 @@ use Pimple\Container;
use Pimple\ServiceProviderInterface; use Pimple\ServiceProviderInterface;
use Symfony\Bridge\Twig\AppVariable; use Symfony\Bridge\Twig\AppVariable;
use Symfony\Bridge\Twig\Extension\AssetExtension; use Symfony\Bridge\Twig\Extension\AssetExtension;
use Symfony\Bridge\Twig\Extension\DumpExtension;
use Symfony\Bridge\Twig\Extension\RoutingExtension; use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension; use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Extension\FormExtension; use Symfony\Bridge\Twig\Extension\FormExtension;
...@@ -103,6 +104,10 @@ class TwigServiceProvider implements ServiceProviderInterface ...@@ -103,6 +104,10 @@ class TwigServiceProvider implements ServiceProviderInterface
$path = dirname($reflected->getFileName()).'/../Resources/views/Form'; $path = dirname($reflected->getFileName()).'/../Resources/views/Form';
$app['twig.loader']->addLoader(new \Twig_Loader_Filesystem($path)); $app['twig.loader']->addLoader(new \Twig_Loader_Filesystem($path));
} }
if (isset($app['var_dumper.cloner'])) {
$twig->addExtension(new DumpExtension($app['var_dumper.cloner']));
}
} }
return $twig; return $twig;
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Application;
use Silex\Api\BootableProviderInterface;
use Symfony\Component\VarDumper\VarDumper;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
/**
* Symfony Var Dumper component Provider.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class VarDumperServiceProvider implements ServiceProviderInterface, BootableProviderInterface
{
public function register(Container $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;
}
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);
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment