ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Errors.php
Go to the documentation of this file.
1 <?php
2 
13 namespace SimpleSAML\XML;
14 
15 use LibXMLError;
16 
17 class Errors
18 {
19 
23  private static $errorStack = array();
24 
28  private static $xmlErrorState;
29 
30 
34  private static function addErrors()
35  {
36  $currentErrors = libxml_get_errors();
37  libxml_clear_errors();
38 
39  $level = count(self::$errorStack) - 1;
40  self::$errorStack[$level] = array_merge(self::$errorStack[$level], $currentErrors);
41  }
42 
43 
50  public static function begin()
51  {
52 
53  // Check whether the error access functions are present
54  if (!function_exists('libxml_use_internal_errors')) {
55  return;
56  }
57 
58  if (count(self::$errorStack) === 0) {
59  // No error logging is currently in progress. Initialize it.
60  self::$xmlErrorState = libxml_use_internal_errors(true);
61  libxml_clear_errors();
62  } else {
63  /* We have already started error logging. Append the current errors to the
64  * list of errors in this level.
65  */
66  self::addErrors();
67  }
68 
69  // Add a new level to the error stack
70  self::$errorStack[] = array();
71  }
72 
73 
79  public static function end()
80  {
81 
82  // Check whether the error access functions are present
83  if (!function_exists('libxml_use_internal_errors')) {
84  // Pretend that no errors occurred
85  return array();
86  }
87 
88  // Add any errors which may have occurred
89  self::addErrors();
90 
91 
92  $ret = array_pop(self::$errorStack);
93 
94  if (count(self::$errorStack) === 0) {
95  // Disable our error logging and restore the previous state
96  libxml_use_internal_errors(self::$xmlErrorState);
97  }
98 
99  return $ret;
100  }
101 
102 
111  public static function formatError($error)
112  {
113  assert($error instanceof LibXMLError);
114  return 'level=' . $error->level . ',code=' . $error->code . ',line=' . $error->line . ',col=' . $error->column .
115  ',msg=' . trim($error->message);
116  }
117 
118 
129  public static function formatErrors($errors)
130  {
131  assert(is_array($errors));
132 
133  $ret = '';
134  foreach ($errors as $error) {
135  $ret .= self::formatError($error) . "\n";
136  }
137 
138  return $ret;
139  }
140 }
static formatError($error)
Format an error as a string.
Definition: Errors.php:111
static formatErrors($errors)
Format a list of errors as a string.
Definition: Errors.php:129
static begin()
Start error logging.
Definition: Errors.php:50
static addErrors()
Append current XML errors to the current stack level.
Definition: Errors.php:34
$errors
Definition: index.php:6
static $xmlErrorState
Definition: Errors.php:28
$ret
Definition: parser.php:6
static end()
End error logging.
Definition: Errors.php:79