1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\HardBotter; |
4
|
|
|
|
5
|
|
|
use mpyw\Cowitter\ClientInterface; |
6
|
|
|
|
7
|
|
|
class Bot implements IBotEssential, IBotHelper |
8
|
|
|
{ |
9
|
|
|
private $client; |
10
|
|
|
private $file; |
11
|
|
|
private $prev; |
12
|
|
|
private $marked = []; |
13
|
|
|
private $mark_limit = 10000; |
14
|
|
|
private $back_limit = 3600; |
15
|
|
|
private $get_error_mode = self::ERRMODE_EXCEPTION; |
16
|
|
|
private $post_error_mode = self::ERRMODE_WARNING; |
17
|
|
|
|
18
|
|
|
use Traits\CollectorTrait; |
19
|
|
|
use Traits\FollowManagerTrait; |
20
|
|
|
use Traits\TweetManagerTrait; |
21
|
|
|
use Traits\InterceptorTrait; |
22
|
|
|
use Traits\SharedUtilityTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* コンストラクタ |
26
|
|
|
* 副作用が大量にあるので注意 |
27
|
|
|
*/ |
28
|
6 |
|
public function __construct( |
29
|
|
|
ClientInterface $client, $filename = 'stamp.json', |
30
|
|
|
$span = 0, $mark_limit = 10000, $back_limit = 3600 |
31
|
|
|
) { |
32
|
|
|
// ヘッダの送出 |
33
|
6 |
|
if (!headers_sent()) { |
34
|
6 |
|
header('Content-Type: text/html; charset=UTF-8'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// エラー表示の設定 |
38
|
6 |
|
error_reporting(-1); |
39
|
6 |
|
ini_set('log_errors', PHP_SAPI === 'cli'); |
40
|
6 |
|
ini_set('display_errors', PHP_SAPI !== 'cli'); |
41
|
|
|
|
42
|
|
|
// 重複起動防止 |
43
|
6 |
|
$file = new \SplFileObject($filename, 'a+b'); |
44
|
6 |
|
if (!$file->flock(LOCK_EX | LOCK_NB)) { |
45
|
1 |
|
throw new \RuntimeException('Failed to lock file.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// JSONとして保存してあるデータを取得 |
49
|
6 |
|
clearstatcache(); |
50
|
6 |
|
$json = $file->getSize() > 0 |
51
|
3 |
|
? json_decode($file->fread($file->getSize()), true) |
52
|
6 |
|
: []; |
53
|
|
|
|
54
|
|
|
// JSONに前回実行時刻が保存されていた時 |
55
|
6 |
|
if (isset($json['prev'])) { |
56
|
|
|
// 十分に時間が空いたかどうかをチェック |
57
|
3 |
|
if (!static::expired($json['prev'], $span)) { |
58
|
1 |
|
throw new \RuntimeException('Execution span is not enough.'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// JSONにマーク済みステータス一覧が記録されていたとき復元する |
63
|
6 |
|
if (isset($json['marked'])) { |
64
|
2 |
|
$this->marked = array_map('filter_var', (array)$json['marked']); |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
$this->client = $client; |
68
|
6 |
|
$this->file = $file; |
69
|
6 |
|
$this->prev = (new \DateTimeImmutable('now', new \DateTimeZone('UTC')))->format('r'); |
70
|
6 |
|
$this->mark_limit = $mark_limit; |
71
|
6 |
|
$this->back_limit = $back_limit; |
72
|
6 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* デストラクタ |
76
|
|
|
*/ |
77
|
6 |
|
public function __destruct() |
78
|
|
|
{ |
79
|
6 |
|
$this->file->ftruncate(0); |
80
|
6 |
|
$this->file->fwrite(json_encode([ |
81
|
6 |
|
'prev' => $this->prev, |
82
|
|
|
// 収まりきらない古い情報は破棄する |
83
|
6 |
|
'marked' => array_slice($this->marked, -$this->mark_limit, $this->mark_limit, true), |
84
|
|
|
])); |
85
|
6 |
|
$this->file->flock(LOCK_UN); |
86
|
6 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* マーク |
90
|
|
|
*/ |
91
|
1 |
|
public function mark(\stdClass $status) { |
92
|
1 |
|
$this->marked[$status->id_str] = true; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* セッター・ゲッター |
97
|
|
|
*/ |
98
|
1 |
|
public function setGetErrorMode($mode) { |
99
|
1 |
|
$this->get_error_mode = $mode; |
100
|
1 |
|
} |
101
|
1 |
|
public function setPostErrorMode($mode) { |
102
|
1 |
|
$this->post_error_mode = $mode; |
103
|
1 |
|
} |
104
|
1 |
|
public function getGetErrorMode() |
105
|
|
|
{ |
106
|
1 |
|
return $this->get_error_mode; |
107
|
|
|
} |
108
|
1 |
|
public function getPostErrorMode() |
109
|
|
|
{ |
110
|
1 |
|
return $this->post_error_mode; |
111
|
|
|
} |
112
|
1 |
|
public function getMarkedStatusIds() |
113
|
|
|
{ |
114
|
1 |
|
return $this->marked; |
115
|
|
|
} |
116
|
1 |
|
public function getBackLimitSeconds() |
117
|
|
|
{ |
118
|
1 |
|
return $this->back_limit; |
119
|
|
|
} |
120
|
1 |
|
public function getMarkLimitCounts() |
121
|
|
|
{ |
122
|
1 |
|
return $this->mark_limit; |
123
|
|
|
} |
124
|
1 |
|
public function getClient() |
125
|
|
|
{ |
126
|
1 |
|
return $this->client; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* clone および serialize 対策 |
131
|
|
|
*/ |
132
|
1 |
|
final public function __sleep() |
133
|
|
|
{ |
134
|
1 |
|
throw new \BadMethodCallException('Instances are not serializable.'); |
135
|
|
|
} |
136
|
1 |
|
final public function __clone() |
137
|
|
|
{ |
138
|
1 |
|
throw new \BadMethodCallException('Instances are not clonable.'); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|