Completed
Push — master ( 69b760...69b5a2 )
by P.R.
05:40
created

HtmlVarWriter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Plaisio\ErrorLogger;
5
6
use Plaisio\Debug\VarWriter;
7
use Plaisio\Helper\Html;
8
9
/**
10
 * Writes a var dump in HTML to a stream.
11
 */
12
class HtmlVarWriter implements VarWriter
13
{
14
  //--------------------------------------------------------------------------------------------------------------------
15
  /**
16
   * The output handle.
17
   *
18
   * @var resource
19
   */
20
  protected $handle;
21
22
  //--------------------------------------------------------------------------------------------------------------------
23
  /**
24
   * Object constructor.
25
   *
26
   * @param resource $handle The handle to write the var dump to.
27
   */
28 1
  public function __construct($handle)
29
  {
30 1
    $this->handle = $handle;
31 1
  }
32
33
  //--------------------------------------------------------------------------------------------------------------------
34
  /**
35
   * Does nothing.
36
   */
37 1
  public function start(): void
38
  {
39 1
    fwrite($this->handle, '<table class="var-dump">');
40 1
  }
41
42
  //--------------------------------------------------------------------------------------------------------------------
43
  /**
44
   * Does nothing.
45
   */
46 1
  public function stop(): void
47
  {
48 1
    fwrite($this->handle, '</table>');
49 1
  }
50
51
  //--------------------------------------------------------------------------------------------------------------------
52
  /**
53
   * {@inheritdoc}
54
   */
55 1
  public function writeArrayClose(int $id, $name): void
56
  {
57 1
    if ($name!==null)
58
    {
59 1
      fwrite($this->handle, '</table>');
60 1
      fwrite($this->handle, '</td>');
61 1
      fwrite($this->handle, '</tr>');
62
    }
63 1
  }
64
65
  //--------------------------------------------------------------------------------------------------------------------
66
  /**
67
   * {@inheritdoc}
68
   */
69 1
  public function writeArrayOpen(int $id, $name): void
70
  {
71 1
    if ($name!==null)
72
    {
73 1
      fwrite($this->handle, '<tr>');
74 1
      $this->writeName($name, $id);
75 1
      fwrite($this->handle, '<td>');
76 1
      fwrite($this->handle, Html::generateElement('div', ['class' => 'array'], 'array').'<br/>');
77 1
      fwrite($this->handle, '<table>');
78
    }
79 1
  }
80
81
  //--------------------------------------------------------------------------------------------------------------------
82
  /**
83
   * {@inheritdoc}
84
   */
85
  public function writeArrayReference(int $ref, $name): void
86
  {
87
    $html = Html::generateElement('span', ['class' => 'array'], 'array');
88
    $html .= ', ';
89
    $html .= Html::generateElement('a', ['href' => '#'.$ref], 'see '.$ref);
90
91
    fwrite($this->handle, '<tr>');
92
    $this->writeName($name);
93
    fwrite($this->handle, Html::generateElement('td', [], $html, true));
94
    fwrite($this->handle, '</tr>');
95
  }
96
97
  //--------------------------------------------------------------------------------------------------------------------
98
  /**
99
   * {@inheritdoc}
100
   */
101
  public function writeBool(?int $id, ?int $ref, bool &$value, $name): void
102
  {
103
    $this->writeScalar($id, $ref, $name, ($value) ? 'true' : 'false', 'keyword');
104
  }
105
106
  //--------------------------------------------------------------------------------------------------------------------
107
  /**
108
   * {@inheritdoc}
109
   */
110
  public function writeFloat(?int $id, ?int $ref, float &$value, $name): void
111
  {
112
    $this->writeScalar($id, $ref, $name, (string)$value, 'number');
113
  }
114
115
  //--------------------------------------------------------------------------------------------------------------------
116
  /**
117
   * {@inheritdoc}
118
   */
119
  public function writeInt(?int $id, ?int $ref, int &$value, $name): void
120
  {
121
    $this->writeScalar($id, $ref, $name, (string)$value, 'number');
122
  }
123
124
  //--------------------------------------------------------------------------------------------------------------------
125
  /**
126
   * {@inheritdoc}
127
   */
128
  public function writeNull(?int $id, ?int $ref, $name): void
129
  {
130
    $this->writeScalar($id, $ref, $name, 'null', 'keyword');
131
  }
132
133
  //--------------------------------------------------------------------------------------------------------------------
134
  /**
135
   * {@inheritdoc}
136
   */
137 1
  public function writeObjectClose(int $id, $name, string $class): void
138
  {
139 1
    if ($name!==null)
140
    {
141 1
      fwrite($this->handle, '</table>');
142 1
      fwrite($this->handle, '</td>');
143 1
      fwrite($this->handle, '</tr>');
144
    }
145 1
  }
146
147
  //--------------------------------------------------------------------------------------------------------------------
148
  /**
149
   * {@inheritdoc}
150
   */
151 1
  public function writeObjectOpen(int $id, $name, string $class): void
152
  {
153 1
    if ($name!==null)
154
    {
155 1
      fwrite($this->handle, '<tr>');
156 1
      $this->writeName($name, $id);
157 1
      fwrite($this->handle, '<td>');
158 1
      fwrite($this->handle, Html::generateElement('div', ['class' => 'class'], $class).'<br/>');
159 1
      fwrite($this->handle, '<table>');
160
    }
161 1
  }
162
163
  //--------------------------------------------------------------------------------------------------------------------
164
  /**
165
   * {@inheritdoc}
166
   */
167
  public function writeObjectReference(int $ref, $name, string $class): void
168
  {
169
    $html = Html::generateElement('span', ['class' => 'class'], $class);
170
    $html .= ', ';
171
    $html .= Html::generateElement('a', ['href' => '#'.(string)$ref], 'see '.(string)$ref);
172
173
    fwrite($this->handle, '<tr>');
174
    $this->writeName($name);
175
    fwrite($this->handle, Html::generateElement('td', [], $html, true));
176
    fwrite($this->handle, '</tr>');
177
  }
178
179
  //--------------------------------------------------------------------------------------------------------------------
180
  /**
181
   * {@inheritdoc}
182
   */
183
  public function writeResource(?int $id, ?int $ref, $name, string $type): void
184
  {
185
    $this->writeScalar($id, $ref, $name, $type, 'keyword');
186
  }
187
188
  //--------------------------------------------------------------------------------------------------------------------
189
  /**
190
   * {@inheritdoc}
191
   */
192 1
  public function writeString(?int $id, ?int $ref, string &$value, $name): void
193
  {
194 1
    $text  = mb_strimwidth($value, 0, 80, '...');
195 1
    $title = ($text!=$value) ? mb_strimwidth($value, 0, 512, '...') : null;
196
197 1
    $this->writeScalar($id, $ref, $name, $text, 'string', $title);
198 1
  }
199
200
  //--------------------------------------------------------------------------------------------------------------------
201
  /**
202
   * {@inheritdoc}
203
   */
204
  public function writeUninitialized($name): void
205
  {
206
    $this->writeScalar(null, null, $name, 'uninitialized', 'uninitialized');
207
  }
208
209
  //--------------------------------------------------------------------------------------------------------------------
210
  /**
211
   * Writes the name of a variable.
212
   *
213
   * @param string|int|null $name The name of the variable.
214
   * @param int|null        $id   The ID of the value.
215
   */
216 1
  private function writeName($name, ?int $id = null): void
217
  {
218 1
    if ($name===null || $name==='')
219
    {
220 1
      fwrite($this->handle, Html::generateElement('th', ['class' => 'id', 'id' => $id], $id));
221
    }
222
    else
223
    {
224 1
      $title = null;
225
226 1
      if (is_int($name))
227
      {
228
        $text  = (string)$name;
229
        $class = 'number';
230
      }
231 1
      elseif (is_string($name))
232
      {
233 1
        $class = 'string';
234 1
        $text  = mb_strimwidth((string)$name, 0, 20, '...');
235 1
        if ($text!=$name)
236
        {
237 1
          $title = mb_strimwidth((string)$name, 0, 512, '...');
238
        }
239
      }
240
      else
241
      {
242
        throw new \InvalidArgumentException(sprintf('$name has unexpected type %s', gettype($name)));
243
      }
244
245 1
      fwrite($this->handle, Html::generateElement('th', ['class' => 'id'], $id));
246
247 1
      fwrite($this->handle, Html::generateElement('th',
248 1
                                                  ['class' => $class,
249 1
                                                   'id'    => $id,
250 1
                                                   'title' => $title],
251 1
                                                  $text));
252
    }
253 1
  }
254
255
  //--------------------------------------------------------------------------------------------------------------------
256
  /**
257
   * Dumps a scalar value.
258
   *
259
   * @param int|null        $id    The ID of the value.
260
   * @param int|null        $ref   The ID of the value if the variable is a reference to a value that has been dumped
261
   *                               already.
262
   * @param string|int|null $name  The name of the variable.
263
   * @param string          $text  The text for displaying the value.
264
   * @param string          $class The class of the value.
265
   * @param string|null     $title The title for the value.
266
   */
267 1
  private function writeScalar(?int $id, ?int $ref, $name, string $text, string $class, ?string $title = null)
268
  {
269 1
    $html = Html::generateElement('span', ['class' => $class, 'title' => $title], $text);
270 1
    if ($ref!==null)
271
    {
272
      $html .= ', ';
273
      $html .= Html::generateElement('a', ['href' => '#'.$ref], 'see '.$ref);
274
    }
275
276 1
    fwrite($this->handle, '<tr>');
277 1
    $this->writeName($name, $id);
278 1
    fwrite($this->handle, Html::generateElement('td', [], $html, true));
279 1
    fwrite($this->handle, '</tr>');
280 1
  }
281
}
282
283
//----------------------------------------------------------------------------------------------------------------------
284