How do I detect if Twig is in debug mode? I have set Twig to be in debug mode in  the development.services.yml file. I would like to alter the behaviour of a custom Twig function, based upon debug being enabled or not.

Is it possible to detect if Twig is in debug mode from the Twig_extension object?
I have found the solution to my question, by looking at the dump() function definition in the devel module. 

Passing the Twig_Environment object into my function, I can use the isDebug() method:

public function myfunction(\Twig_Environment $env, $string) {
  if($env-&gt;isDebug()) {
    $string = 'foobar';
  }
  return $string;
}