-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.php
More file actions
106 lines (84 loc) · 1.83 KB
/
adapter.php
File metadata and controls
106 lines (84 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Created by PhpStorm.
* User: netAir
* Date: 17-7-15
* Time: 下午1:28
*/
class ErrorObject
{
private $error;
public function __construct(string $error)
{
$this->error = $error;
}
public function getError()
{
return $this->error;
}
}
/*
* 原有日志类,直接调用ErrorObject
*/
class LogToConsole
{
private $errorObject;
public function __construct(ErrorObject $errorObject)
{
$this->errorObject = $errorObject;
}
public function write()
{
fwrite(STDERR, $this->errorObject->getError());
}
}
/*
* 新日志类,无法直接使用ErrorObject
*/
class LogToCSV
{
const CSV_LOCATION = 'log.csv';
private $errorObject;
public function __construct(LogToCSVAdapter $errorObject)
{
$this->errorObject = $errorObject;
}
public function write()
{
$line = $this->errorObject->getErrorNumber();
$line .= ',';
$line .= $this->errorObject->getErrorText();
$line .= "\n";
file_put_contents(self::CSV_LOCATION, $line, FILE_APPEND);
}
}
/*
* 适配器类,将errorObject转换为LogToCSV可调用类
*/
class LogToCSVAdapter extends ErrorObject
{
private $errorNumber;
private $errorText;
public function __construct(string $error)
{
parent::__construct($error);
$parts = explode(':', $error);
$this->errorNumber = $parts[0];
$this->errorText = $parts[1];
}
public function getErrorNumber()
{
return $this->errorNumber;
}
public function getErrorText()
{
return $this->errorText;
}
}
$error = '404:NF';
$errorObject = new ErrorObject($error);
$log = new LogToConsole($errorObject);
$log->write();
$errorObject=new LogToCSVAdapter($error);
$log=new LogToCSV($errorObject);
$log->write();