ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Whoops\Util\TemplateHelper Class Reference

Exposes useful tools for working with/in templates. More...

+ Collaboration diagram for Whoops\Util\TemplateHelper:

Public Member Functions

 __construct ()
 
 escape ($raw)
 Escapes a string for output in an HTML document. More...
 
 escapeButPreserveUris ($raw)
 Escapes a string for output in an HTML document, but preserves URIs within it, and converts them to clickable anchor elements. More...
 
 breakOnDelimiter ($delimiter, $s)
 Makes sure that the given string breaks on the delimiter. More...
 
 shorten ($path)
 Replace the part of the path that all files have in common. More...
 
 dump ($value)
 Format the given value into a human readable string. More...
 
 dumpArgs (Frame $frame)
 Format the args of the given Frame as a human readable html string. More...
 
 slug ($original)
 Convert a string to a slug version of itself. More...
 
 render ($template, array $additionalVariables=null)
 Given a template path, render it within its own scope. More...
 
 setVariables (array $variables)
 Sets the variables to be passed to all templates rendered by this template helper. More...
 
 setVariable ($variableName, $variableValue)
 Sets a single template variable, by its name: More...
 
 getVariable ($variableName, $defaultValue=null)
 Gets a single template variable, by its name, or $defaultValue if the variable does not exist. More...
 
 delVariable ($variableName)
 Unsets a single template variable, by its name. More...
 
 getVariables ()
 Returns all variables for this helper. More...
 
 setCloner ($cloner)
 Set the cloner used for dumping variables. More...
 
 getCloner ()
 Get the cloner used for dumping variables. More...
 
 setApplicationRootPath ($applicationRootPath)
 Set the application root path. More...
 
 getApplicationRootPath ()
 Return the application root path. More...
 

Private Member Functions

 getDumper ()
 

Private Attributes

 $variables = []
 
 $htmlDumper
 
 $htmlDumperOutput
 
 $cloner
 
 $applicationRootPath
 

Detailed Description

Exposes useful tools for working with/in templates.

Definition at line 18 of file TemplateHelper.php.

Constructor & Destructor Documentation

◆ __construct()

Whoops\Util\TemplateHelper::__construct ( )

Definition at line 46 of file TemplateHelper.php.

47  {
48  // root path for ordinary composer projects
49  $this->applicationRootPath = dirname(dirname(dirname(dirname(dirname(dirname(__DIR__))))));
50  }

Member Function Documentation

◆ breakOnDelimiter()

Whoops\Util\TemplateHelper::breakOnDelimiter (   $delimiter,
  $s 
)

Makes sure that the given string breaks on the delimiter.

Parameters
string$delimiter
string$s
Returns
string

Definition at line 103 of file TemplateHelper.php.

References $delimiter, and $s.

104  {
105  $parts = explode($delimiter, $s);
106  foreach ($parts as &$part) {
107  $part = '<div class="delimiter">' . $part . '</div>';
108  }
109 
110  return implode($delimiter, $parts);
111  }
$delimiter
Definition: showstats.php:16
$s
Definition: pwgen.php:45

◆ delVariable()

Whoops\Util\TemplateHelper::delVariable (   $variableName)

Unsets a single template variable, by its name.

Parameters
string$variableName

Definition at line 295 of file TemplateHelper.php.

296  {
297  unset($this->variables[$variableName]);
298  }

◆ dump()

Whoops\Util\TemplateHelper::dump (   $value)

Format the given value into a human readable string.

Parameters
mixed$value
Returns
string

Definition at line 161 of file TemplateHelper.php.

References Sabre\VObject\$output, Whoops\Util\TemplateHelper\getCloner(), and Whoops\Util\TemplateHelper\getDumper().

Referenced by Whoops\Util\TemplateHelper\dumpArgs().

162  {
163  $dumper = $this->getDumper();
164 
165  if ($dumper) {
166  // re-use the same DumpOutput instance, so it won't re-render the global styles/scripts on each dump.
167  // exclude verbose information (e.g. exception stack traces)
168  if (class_exists('Symfony\Component\VarDumper\Caster\Caster')) {
169  $cloneVar = $this->getCloner()->cloneVar($value, Caster::EXCLUDE_VERBOSE);
170  // Symfony VarDumper 2.6 Caster class dont exist.
171  } else {
172  $cloneVar = $this->getCloner()->cloneVar($value);
173  }
174 
175  $dumper->dump(
176  $cloneVar,
177  $this->htmlDumperOutput
178  );
179 
180  $output = $this->htmlDumperOutput->getOutput();
181  $this->htmlDumperOutput->clear();
182 
183  return $output;
184  }
185 
186  return htmlspecialchars(print_r($value, true));
187  }
getCloner()
Get the cloner used for dumping variables.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dumpArgs()

Whoops\Util\TemplateHelper::dumpArgs ( Frame  $frame)

Format the args of the given Frame as a human readable html string.

Parameters
Frame$frame
Returns
string the rendered html

Definition at line 195 of file TemplateHelper.php.

References $html, Whoops\Util\TemplateHelper\dump(), Whoops\Exception\Frame\getArgs(), and Whoops\Util\TemplateHelper\getDumper().

196  {
197  // we support frame args only when the optional dumper is available
198  if (!$this->getDumper()) {
199  return '';
200  }
201 
202  $html = '';
203  $numFrames = count($frame->getArgs());
204 
205  if ($numFrames > 0) {
206  $html = '<ol class="linenums">';
207  foreach ($frame->getArgs() as $j => $frameArg) {
208  $html .= '<li>'. $this->dump($frameArg) .'</li>';
209  }
210  $html .= '</ol>';
211  }
212 
213  return $html;
214  }
dump($value)
Format the given value into a human readable string.
$html
Definition: example_001.php:87
+ Here is the call graph for this function:

◆ escape()

Whoops\Util\TemplateHelper::escape (   $raw)

Escapes a string for output in an HTML document.

Parameters
string$raw
Returns
string

Definition at line 58 of file TemplateHelper.php.

Referenced by Whoops\Util\TemplateHelper\escapeButPreserveUris().

59  {
60  $flags = ENT_QUOTES;
61 
62  // HHVM has all constants defined, but only ENT_IGNORE
63  // works at the moment
64  if (defined("ENT_SUBSTITUTE") && !defined("HHVM_VERSION")) {
65  $flags |= ENT_SUBSTITUTE;
66  } else {
67  // This is for 5.3.
68  // The documentation warns of a potential security issue,
69  // but it seems it does not apply in our case, because
70  // we do not blacklist anything anywhere.
71  $flags |= ENT_IGNORE;
72  }
73 
74  $raw = str_replace(chr(9), ' ', $raw);
75 
76  return htmlspecialchars($raw, $flags, "UTF-8");
77  }
+ Here is the caller graph for this function:

◆ escapeButPreserveUris()

Whoops\Util\TemplateHelper::escapeButPreserveUris (   $raw)

Escapes a string for output in an HTML document, but preserves URIs within it, and converts them to clickable anchor elements.

Parameters
string$raw
Returns
string

Definition at line 86 of file TemplateHelper.php.

References Whoops\Util\TemplateHelper\escape().

87  {
88  $escaped = $this->escape($raw);
89  return preg_replace(
90  "@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@",
91  "<a href=\"$1\" target=\"_blank\" rel=\"noreferrer noopener\">$1</a>",
92  $escaped
93  );
94  }
escape($raw)
Escapes a string for output in an HTML document.
+ Here is the call graph for this function:

◆ getApplicationRootPath()

Whoops\Util\TemplateHelper::getApplicationRootPath ( )

Return the application root path.

Returns
string

Definition at line 348 of file TemplateHelper.php.

References Whoops\Util\TemplateHelper\$applicationRootPath.

349  {
351  }

◆ getCloner()

Whoops\Util\TemplateHelper::getCloner ( )

Get the cloner used for dumping variables.

Returns
AbstractCloner

Definition at line 325 of file TemplateHelper.php.

References Whoops\Util\TemplateHelper\$cloner.

Referenced by Whoops\Util\TemplateHelper\dump().

326  {
327  if (!$this->cloner) {
328  $this->cloner = new VarCloner();
329  }
330  return $this->cloner;
331  }
+ Here is the caller graph for this function:

◆ getDumper()

Whoops\Util\TemplateHelper::getDumper ( )
private

Definition at line 128 of file TemplateHelper.php.

References Whoops\Util\TemplateHelper\$htmlDumper.

Referenced by Whoops\Util\TemplateHelper\dump(), and Whoops\Util\TemplateHelper\dumpArgs().

129  {
130  if (!$this->htmlDumper && class_exists('Symfony\Component\VarDumper\Cloner\VarCloner')) {
131  $this->htmlDumperOutput = new HtmlDumperOutput();
132  // re-use the same var-dumper instance, so it won't re-render the global styles/scripts on each dump.
133  $this->htmlDumper = new HtmlDumper($this->htmlDumperOutput);
134 
135  $styles = [
136  'default' => 'color:#FFFFFF; line-height:normal; font:12px "Inconsolata", "Fira Mono", "Source Code Pro", Monaco, Consolas, "Lucida Console", monospace !important; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:99999; word-break: normal',
137  'num' => 'color:#BCD42A',
138  'const' => 'color: #4bb1b1;',
139  'str' => 'color:#BCD42A',
140  'note' => 'color:#ef7c61',
141  'ref' => 'color:#A0A0A0',
142  'public' => 'color:#FFFFFF',
143  'protected' => 'color:#FFFFFF',
144  'private' => 'color:#FFFFFF',
145  'meta' => 'color:#FFFFFF',
146  'key' => 'color:#BCD42A',
147  'index' => 'color:#ef7c61',
148  ];
149  $this->htmlDumper->setStyles($styles);
150  }
151 
152  return $this->htmlDumper;
153  }
+ Here is the caller graph for this function:

◆ getVariable()

Whoops\Util\TemplateHelper::getVariable (   $variableName,
  $defaultValue = null 
)

Gets a single template variable, by its name, or $defaultValue if the variable does not exist.

Parameters
string$variableName
mixed$defaultValue
Returns
mixed

Definition at line 284 of file TemplateHelper.php.

285  {
286  return isset($this->variables[$variableName]) ?
287  $this->variables[$variableName] : $defaultValue;
288  }

◆ getVariables()

Whoops\Util\TemplateHelper::getVariables ( )

Returns all variables for this helper.

Returns
array

Definition at line 305 of file TemplateHelper.php.

References Whoops\Util\TemplateHelper\$variables.

Referenced by Whoops\Util\TemplateHelper\render().

306  {
307  return $this->variables;
308  }
+ Here is the caller graph for this function:

◆ render()

Whoops\Util\TemplateHelper::render (   $template,
array  $additionalVariables = null 
)

Given a template path, render it within its own scope.

This method also accepts an array of additional variables to be passed to the template.

Parameters
string$template
array$additionalVariables

Definition at line 237 of file TemplateHelper.php.

References $template, Whoops\Util\TemplateHelper\$variables, and Whoops\Util\TemplateHelper\getVariables().

238  {
239  $variables = $this->getVariables();
240 
241  // Pass the helper to the template:
242  $variables["tpl"] = $this;
243 
244  if ($additionalVariables !== null) {
245  $variables = array_replace($variables, $additionalVariables);
246  }
247 
248  call_user_func(function () {
249  extract(func_get_arg(1));
250  require func_get_arg(0);
251  }, $template, $variables);
252  }
$template
getVariables()
Returns all variables for this helper.
+ Here is the call graph for this function:

◆ setApplicationRootPath()

Whoops\Util\TemplateHelper::setApplicationRootPath (   $applicationRootPath)

Set the application root path.

Parameters
string$applicationRootPath

Definition at line 338 of file TemplateHelper.php.

References Whoops\Util\TemplateHelper\$applicationRootPath.

339  {
340  $this->applicationRootPath = $applicationRootPath;
341  }

◆ setCloner()

Whoops\Util\TemplateHelper::setCloner (   $cloner)

Set the cloner used for dumping variables.

Parameters
AbstractCloner$cloner

Definition at line 315 of file TemplateHelper.php.

References Whoops\Util\TemplateHelper\$cloner.

316  {
317  $this->cloner = $cloner;
318  }

◆ setVariable()

Whoops\Util\TemplateHelper::setVariable (   $variableName,
  $variableValue 
)

Sets a single template variable, by its name:

Parameters
string$variableName
mixed$variableValue

Definition at line 271 of file TemplateHelper.php.

272  {
273  $this->variables[$variableName] = $variableValue;
274  }

◆ setVariables()

Whoops\Util\TemplateHelper::setVariables ( array  $variables)

Sets the variables to be passed to all templates rendered by this template helper.

Parameters
array$variables

Definition at line 260 of file TemplateHelper.php.

References Whoops\Util\TemplateHelper\$variables.

261  {
262  $this->variables = $variables;
263  }

◆ shorten()

Whoops\Util\TemplateHelper::shorten (   $path)

Replace the part of the path that all files have in common.

Parameters
string$path
Returns
string

Definition at line 119 of file TemplateHelper.php.

References $path.

120  {
121  if ($this->applicationRootPath != "/") {
122  $path = str_replace($this->applicationRootPath, '&hellip;', $path);
123  }
124 
125  return $path;
126  }
$path
Definition: aliased.php:25

◆ slug()

Whoops\Util\TemplateHelper::slug (   $original)

Convert a string to a slug version of itself.

Parameters
string$original
Returns
string

Definition at line 222 of file TemplateHelper.php.

223  {
224  $slug = str_replace(" ", "-", $original);
225  $slug = preg_replace('/[^\w\d\-\_]/i', '', $slug);
226  return strtolower($slug);
227  }

Field Documentation

◆ $applicationRootPath

Whoops\Util\TemplateHelper::$applicationRootPath
private

◆ $cloner

Whoops\Util\TemplateHelper::$cloner
private

◆ $htmlDumper

Whoops\Util\TemplateHelper::$htmlDumper
private

Definition at line 29 of file TemplateHelper.php.

Referenced by Whoops\Util\TemplateHelper\getDumper().

◆ $htmlDumperOutput

Whoops\Util\TemplateHelper::$htmlDumperOutput
private

Definition at line 34 of file TemplateHelper.php.

◆ $variables

Whoops\Util\TemplateHelper::$variables = []
private

The documentation for this class was generated from the following file: