1: <?php
2:
3: namespace gregoryv\logger;
4:
5: 6: 7:
8: class CachedWriter implements SeverityWriterInterface
9: {
10: 11: 12:
13: public $cache;
14:
15: private $messageLimit;
16:
17: 18: 19:
20: public function __construct($messageLimit=20)
21: {
22: $this->cache = [];
23: $this->messageLimit = $messageLimit;
24: }
25:
26: 27: 28:
29: public function swrite($severity, $value='')
30: {
31: if(sizeof($this->cache) == $this->messageLimit) {
32: array_shift($this->cache);
33: }
34: $this->cache[] = $value;
35: }
36:
37: 38: 39:
40: public function setLimit($limit)
41: {
42: if(!is_int($limit)) {
43: throw new \InvalidArgumentException("limit must be a int");
44: }
45: if($limit < 1) {
46: throw new \InvalidArgumentException("limit must be 1 or more");
47: }
48: $this->resizeCache($limit);
49: $this->messageLimit = $limit;
50: }
51:
52: public function resizeCache($limit)
53: {
54: while(sizeof($this->cache) != $limit) {
55: array_shift($this->cache);
56: }
57: }
58: }
59: