Commit b67cd63f authored by willzyx's avatar willzyx

Issue #2788089 by willzyx: Add xdebug support for Twig template debugging

parent e20410c1
......@@ -62,6 +62,14 @@ class Debug extends \Twig_Extension {
]);
}
foreach (['devel_breakpoint'] as $function) {
$functions[] = new \Twig_SimpleFunction($function, [$this, 'breakpoint'], [
'needs_environment' => TRUE,
'needs_context' => TRUE,
'is_variadic' => TRUE,
]);
}
return $functions;
}
......@@ -137,6 +145,40 @@ class Debug extends \Twig_Extension {
}
/**
* Provides XDebug integration for Twig templates.
*
* To use this features simply put the following statement in the template
* of interest:
*
* @code
* {{ devel_breakpoint() }}
* @endcode
*
* When the template is evaluated is made a call to a dedicated method in
* devel twig debug extension in which is used xdebug_break(), that emits a
* breakpoint to the debug client (the debugger break on the specific line as
* if a normal file/line breakpoint was set on this line).
* In this way you'll be able to inspect any variables available in the
* template (environment, context, specific variables etc..) in your IDE.
*
* @param \Twig_Environment $env
* The twig environment instance.
* @param array $context
* An array of parameters passed to the template.
* @param array $args
* An array of parameters passed the function.
*/
public function breakpoint(\Twig_Environment $env, array $context, array $args = []) {
if (!$env->isDebug()) {
return;
}
if (function_exists('xdebug_break')) {
xdebug_break();
}
}
/**
* Filters the Twig context variable.
*
......
......@@ -68,16 +68,16 @@ class DevelTwigExtensionTest extends KernelTestBase {
/**
* Tests that Twig extension loads appropriately.
*/
function testTwigExtensionLoaded() {
public function testTwigExtensionLoaded() {
$twig_service = \Drupal::service('twig');
$extension = $twig_service->getExtension('devel_debug');
$this->assertEquals(get_class($extension), Debug::class, 'Debug Extension loaded successfully.');
}
/**
* Tests that theTwig functions are registered properly.
* Tests that the Twig dump functions are registered properly.
*/
function testTwigExtensionFunctionsRegistered() {
public function testDumpFunctionsRegistered() {
/** @var \Twig_SimpleFunction[] $functions */
$functions = \Drupal::service('twig')->getFunctions();
......@@ -101,13 +101,27 @@ class DevelTwigExtensionTest extends KernelTestBase {
$this->assertEquals($callable, 'Drupal\devel\Twig\Extension\Debug::message');
}
}
}
/**
* Tests that the Twig function for XDebug integration is registered properly.
*/
public function testXDebugIntegrationFunctionsRegistered() {
/** @var \Twig_SimpleFunction $function */
$function = \Drupal::service('twig')->getFunction('devel_breakpoint');
$this->assertTrue($function instanceof \Twig_SimpleFunction);
$this->assertEquals($function->getName(), 'devel_breakpoint');
$this->assertTrue($function->needsContext());
$this->assertTrue($function->needsEnvironment());
$this->assertTrue($function->isVariadic());
is_callable($function->getCallable(), TRUE, $callable);
$this->assertEquals($callable, 'Drupal\devel\Twig\Extension\Debug::breakpoint');
}
/**
* Tests that the Twig extension's function produces expected output.
* Tests that the Twig extension's dump functions produce the expected output.
*/
function testTwigExtensionFunctions() {
public function testDumpFunctions() {
$template = 'test-with-context {{ twig_string }} {{ twig_array.first }} {{ twig_array.second }}{{ devel_dump() }}';
$expected_template_output = 'test-with-context context! first value second value';
......
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