Upgrade to Pro — share decks privately, control downloads, hide ads and more …

debug() + Symfony

debug() + Symfony

var_dump() est encore bien souvent notre meilleur ami pour comprendre ce qui se passe au cœur de nos applications. Que diriez-vous de disposer d'une nouvelle fonction debug() dans Symfony2, qui remplace avantageusement les outils actuels ? Une fonction qui pourrait afficher l'état d'une variable dans la Web Debug Toolbar sans casser le flux de sortie d'origine, afficher une sortie couleur en mode console, fonctionner dans les templates Twig, s'adapter aux types d'objets rencontrés et n'afficher que les infos pertinentes (exit l'EntityManager sur les entités), avoir connaissance des références internes à un graph d'objets pour n'afficher qu'une fois chacun d'eux en permettant d'inspecter leur structure en finesse. Cette conférence sera l'occasion d'apprendre à faire vos propres onglets dans la Web Debug Toolbar de Symfony2 et de passer en revue les nouveautés du composant Debug.

Nicolas Grekas

April 07, 2014
Tweet

More Decks by Nicolas Grekas

Other Decks in Programming

Transcript

  1. Symfony 2.5 : Nouveautés du composant Debug Symfony 2.next :

    debug() au quotidien Comment ça marche ?
  2. Fini les erreurs @-silencieuses • $a = @new Foo\Bar(); •

    Error handler – $loggers['scream']->log(...); • Debug autoloader – error_reporting() – E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR
  3. Symfony 2.5 : Nouveautés du composant Debug Symfony 2.next :

    debug() au quotidien Comment ça marche ?
  4. var_dump() /** * @Route("/hello/{name}", name="_demo_hello") * @Template() */ public function

    helloAction($name) { var_dump($this); return array('name' => $name); }
  5. var_dump() $obj = new stdClass(); $array = [$obj, &$obj, &$obj];

    var_dump($array); array(3) { [0] => class stdClass#1 (0) { } [1] => class stdClass#1 (0) { } [2] => class stdClass#1 (0) { } }
  6. debug() • Exacte, avec références hard et soft • Détaillée,

    avec infos spécialisées • Pretty et pratique • Just works : robuste, rapide et memory efficient
  7. debug() class foo { public $fileHandler; protected $bar = 123;

    private $baz = 456; } $h = fopen(__FILE__, 'rb'); $f = new foo(); $f->fileHandler = $h; debug($f);
  8. Symfony 2.5 : Nouveautés du composant Debug Symfony 2.next :

    debug() au quotidien Comment ça marche ?
  9. Mécanisme interne • Copie de la variable – Indépendante en

    terme de références – Limitée en nombre d'items si nécessaire – Adaptée aux objets rencontrés • Représentation de la copie – CLI, HTML ou JSON
  10. Intégration dans Symfony • un DataCollector pour les copies extends

    \Symfony\Component\HttpKernel\DataCollector\DataCollector <tag name="data_collector" template="@WebProfiler/Collector/var_debug.html.twig" id="var_debug" /> {% extends '@WebProfiler/Profiler/layout.html.twig' %} • JSON comme intermédiaire • une Twig extension pour la fonction debug()
  11. Merci ! Bridge/Twig/Extension/VarDebugExtension.php | 57 +++++++++++++++++++++++++++ Bundle/FrameworkBundle/DependencyInjection/Configuration.php | 24 ++++++++++++

    Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php | 11 ++++++ Bundle/FrameworkBundle/FrameworkBundle.php | 7 ++++ Bundle/FrameworkBundle/Resources/config/collectors.xml | 6 +++ Bundle/FrameworkBundle/Resources/config/var_debug.xml | 20 ++++++++++ Bundle/TwigBundle/Resources/config/twig.xml | 5 +++ Bundle/WebProfilerBundle/Resources/public/css/var_debug.css | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Bundle/WebProfilerBundle/Resources/public/js/var_debug.js | 245 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Bundle/WebProfilerBundle/Resources/views/Collector/var_debug.html.twig | 66 +++++++++++++++++++++++++++++++ Bundle/WebProfilerBundle/WebProfilerBundle.php | 9 +++++ Component/HttpKernel/DataCollector/VarDebugDataCollector.php | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Caster/BaseCaster.php | 68 ++++++++++++++++++++++++++++++++ Component/VarDebug/Caster/DoctrineCaster.php | 44 +++++++++++++++++++++ Component/VarDebug/Caster/ExceptionCaster.php | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Caster/PdoCaster.php | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Caster/SplCaster.php | 54 ++++++++++++++++++++++++++ Component/VarDebug/Collector/AbstractCollector.php | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Collector/CollectorInterface.php | 20 ++++++++++ Component/VarDebug/Collector/Data.php | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Collector/ExtCollector.php | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Collector/PhpCollector.php | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Dumper/AbstractDumper.php | 75 +++++++++++++++++++++++++++++++++++ Component/VarDebug/Dumper/CliDumper.php | 333 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Dumper/Cursor.php | 34 ++++++++++++++++ Component/VarDebug/Dumper/DumperInterface.php | 32 +++++++++++++++ Component/VarDebug/Dumper/HtmlDumper.php | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Dumper/JsonDumper.php | 236 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Exception/ThrowingCasterException.php | 26 +++++++++++++ Component/VarDebug/LICENSE | 19 +++++++++ Component/VarDebug/README.md | 22 +++++++++++ Component/VarDebug/Resources/doc/json-spec.md | 410 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Resources/ext/README.rst | 71 +++++++++++++++++++++++++++++++++ Component/VarDebug/Resources/ext/config.m4 | 63 ++++++++++++++++++++++++++++++ Component/VarDebug/Resources/ext/config.w32 | 13 +++++++ Component/VarDebug/Resources/ext/php_symfony_debug.h | 55 ++++++++++++++++++++++++++ Component/VarDebug/Resources/ext/symfony_debug.c | 223 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Resources/ext/tests/001.phpt | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Resources/functions/debug.php | 60 ++++++++++++++++++++++++++++ Component/VarDebug/Tests/CliDumperTest.php | 96 +++++++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/Tests/Fixtures/dumb-var.php | 30 ++++++++++++++ Component/VarDebug/Tests/JsonDumperTest.php | 86 ++++++++++++++++++++++++++++++++++++++++ Component/VarDebug/composer.json | 25 ++++++++++++ Component/VarDebug/phpunit.xml.dist | 29 ++++++++++++++