|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHP Mess Detector. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Manuel Pichler <[email protected]>. |
|
6
|
|
|
* All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under BSD License |
|
9
|
|
|
* For full copyright and license information, please see the LICENSE file. |
|
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Manuel Pichler <[email protected]> |
|
13
|
|
|
* @copyright Manuel Pichler. All rights reserved. |
|
14
|
|
|
* @license https://opensource.org/licenses/bsd-license.php BSD License |
|
15
|
|
|
* @link http://phpmd.org/ |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace PHPMD\Writer; |
|
19
|
|
|
|
|
20
|
|
|
use PHPMD\AbstractWriter; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* This writer uses PHP's stream api as its output target. |
|
24
|
|
|
*/ |
|
25
|
|
|
class StreamWriter extends AbstractWriter |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* The stream resource handle |
|
29
|
|
|
* |
|
30
|
|
|
* @var resource |
|
31
|
|
|
*/ |
|
32
|
|
|
private $stream = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Constructs a new stream writer instance. |
|
36
|
|
|
* |
|
37
|
|
|
* @param resource|string $streamResourceOrUri |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public function __construct($streamResourceOrUri) |
|
40
|
|
|
{ |
|
41
|
4 |
|
if (is_resource($streamResourceOrUri) === true) { |
|
42
|
|
|
$this->stream = $streamResourceOrUri; |
|
43
|
|
|
} else { |
|
44
|
4 |
|
$dirName = dirname($streamResourceOrUri); |
|
45
|
4 |
|
if (file_exists($dirName) === false) { |
|
46
|
|
|
mkdir($dirName, 0777, true); |
|
47
|
|
|
} |
|
48
|
4 |
|
if (file_exists($dirName) === false) { |
|
49
|
|
|
$message = 'Cannot find output directory "' . $dirName . '".'; |
|
50
|
|
|
throw new \RuntimeException($message); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
$this->stream = fopen($streamResourceOrUri, 'wb'); |
|
54
|
|
|
} |
|
55
|
4 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The dtor closes the open output resource. |
|
59
|
|
|
*/ |
|
60
|
4 |
|
public function __destruct() |
|
61
|
|
|
{ |
|
62
|
4 |
|
if ($this->stream !== STDOUT && is_resource($this->stream) === true) { |
|
63
|
4 |
|
@fclose($this->stream); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
4 |
|
$this->stream = null; |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Writes the given <b>$data</b> fragment to the wrapper output stream. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $data |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function write($data) |
|
75
|
|
|
{ |
|
76
|
4 |
|
fwrite($this->stream, $data); |
|
77
|
4 |
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: