9922e610634cf7c6c5280fcc35c583bd5b932479
[yaffs-website] / web / modules / contrib / devel / src / Twig / Extension / Debug.php
1 <?php
2
3 namespace Drupal\devel\Twig\Extension;
4
5 use Drupal\devel\DevelDumperManagerInterface;
6
7 /**
8  * Provides the Devel debugging function within Twig templates.
9  *
10  * NOTE: This extension doesn't do anything unless twig_debug is enabled.
11  * The twig_debug setting is read from the Twig environment, not Drupal
12  * Settings, so a container rebuild is necessary when toggling twig_debug on
13  * and off.
14  */
15 class Debug extends \Twig_Extension {
16
17   /**
18    * The devel dumper service.
19    *
20    * @var \Drupal\devel\DevelDumperManagerInterface
21    */
22   protected $dumper;
23
24   /**
25    * Constructs a Debug object.
26    *
27    * @param \Drupal\devel\DevelDumperManagerInterface $dumper
28    *   The devel dumper service.
29    */
30   public function __construct(DevelDumperManagerInterface $dumper) {
31     $this->dumper = $dumper;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function getName() {
38     return 'devel_debug';
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getFunctions() {
45     $functions = [];
46
47     foreach (['devel_dump', 'kpr'] as $function) {
48       $functions[] = new \Twig_SimpleFunction($function, [$this, 'dump'], [
49         'is_safe' => ['html'],
50         'needs_environment' => TRUE,
51         'needs_context' => TRUE,
52         'is_variadic' => TRUE,
53       ]);
54     }
55
56     foreach (['devel_message', 'dpm', 'dsm'] as $function) {
57       $functions[] = new \Twig_SimpleFunction($function, [$this, 'message'], [
58         'is_safe' => ['html'],
59         'needs_environment' => TRUE,
60         'needs_context' => TRUE,
61         'is_variadic' => TRUE,
62       ]);
63     }
64
65     foreach (['devel_breakpoint'] as $function) {
66       $functions[] = new \Twig_SimpleFunction($function, [$this, 'breakpoint'], [
67         'needs_environment' => TRUE,
68         'needs_context' => TRUE,
69         'is_variadic' => TRUE,
70       ]);
71     }
72
73     return $functions;
74   }
75
76   /**
77    * Provides debug function to Twig templates.
78    *
79    * Handles 0, 1, or multiple arguments.
80    *
81    * @param \Twig_Environment $env
82    *   The twig environment instance.
83    * @param array $context
84    *   An array of parameters passed to the template.
85    * @param array $args
86    *   An array of parameters passed the function.
87    *
88    * @return string
89    *   String representation of the input variables.
90    *
91    * @see \Drupal\devel\DevelDumperManager::dump()
92    */
93   public function dump(\Twig_Environment $env, array $context, array $args = []) {
94     if (!$env->isDebug()) {
95       return;
96     }
97
98     ob_start();
99
100     // No arguments passed, display full Twig context.
101     if (empty($args)) {
102       $context_variables = $this->getContextVariables($context);
103       $this->dumper->dump($context_variables, 'Twig context');
104     }
105     else {
106       foreach ($args as $variable) {
107         $this->dumper->dump($variable);
108       }
109     }
110
111     return ob_get_clean();
112   }
113
114   /**
115    * Provides debug function to Twig templates.
116    *
117    * Handles 0, 1, or multiple arguments.
118    *
119    * @param \Twig_Environment $env
120    *   The twig environment instance.
121    * @param array $context
122    *   An array of parameters passed to the template.
123    * @param array $args
124    *   An array of parameters passed the function.
125    *
126    * @return void
127    *
128    * @see \Drupal\devel\DevelDumperManager::message()
129    */
130   public function message(\Twig_Environment $env, array $context, array $args = []) {
131     if (!$env->isDebug()) {
132       return;
133     }
134
135     // No arguments passed, display full Twig context.
136     if (empty($args)) {
137       $context_variables = $this->getContextVariables($context);
138       $this->dumper->message($context_variables, 'Twig context');
139     }
140     else {
141       foreach ($args as $variable) {
142         $this->dumper->message($variable);
143       }
144     }
145
146   }
147
148   /**
149    * Provides XDebug integration for Twig templates.
150    *
151    * To use this features simply put the following statement in the template
152    * of interest:
153    *
154    * @code
155    * {{ devel_breakpoint() }}
156    * @endcode
157    *
158    * When the template is evaluated is made a call to a dedicated method in
159    * devel twig debug extension in which is used xdebug_break(), that emits a
160    * breakpoint to the debug client (the debugger break on the specific line as
161    * if a normal file/line breakpoint was set on this line).
162    * In this way you'll be able to inspect any variables available in the
163    * template (environment, context, specific variables etc..) in your IDE.
164    *
165    * @param \Twig_Environment $env
166    *   The twig environment instance.
167    * @param array $context
168    *   An array of parameters passed to the template.
169    * @param array $args
170    *   An array of parameters passed the function.
171    */
172   public function breakpoint(\Twig_Environment $env, array $context, array $args = []) {
173     if (!$env->isDebug()) {
174       return;
175     }
176
177     if (function_exists('xdebug_break')) {
178       xdebug_break();
179     }
180   }
181
182   /**
183    * Filters the Twig context variable.
184    *
185    * @param array $context
186    *  The Twig context.
187    *
188    * @return array
189    *   An array Twig context variables.
190    */
191   protected function getContextVariables(array $context) {
192     $context_variables = [];
193     foreach ($context as $key => $value) {
194       if (!$value instanceof \Twig_Template) {
195         $context_variables[$key] = $value;
196       }
197     }
198     return $context_variables;
199   }
200
201 }