scratch – Blame information for rev 120

Subversion Repositories:
Rev:
Rev Author Line No. Line
120 office 1 <?php
2  
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Monolog\Handler;
13  
14 /**
15 * Used for testing purposes.
16 *
17 * It records all records and gives you access to them for verification.
18 *
19 * @author Jordi Boggiano <j.boggiano@seld.be>
20 *
21 * @method bool hasEmergency($record)
22 * @method bool hasAlert($record)
23 * @method bool hasCritical($record)
24 * @method bool hasError($record)
25 * @method bool hasWarning($record)
26 * @method bool hasNotice($record)
27 * @method bool hasInfo($record)
28 * @method bool hasDebug($record)
29 *
30 * @method bool hasEmergencyRecords()
31 * @method bool hasAlertRecords()
32 * @method bool hasCriticalRecords()
33 * @method bool hasErrorRecords()
34 * @method bool hasWarningRecords()
35 * @method bool hasNoticeRecords()
36 * @method bool hasInfoRecords()
37 * @method bool hasDebugRecords()
38 *
39 * @method bool hasEmergencyThatContains($message)
40 * @method bool hasAlertThatContains($message)
41 * @method bool hasCriticalThatContains($message)
42 * @method bool hasErrorThatContains($message)
43 * @method bool hasWarningThatContains($message)
44 * @method bool hasNoticeThatContains($message)
45 * @method bool hasInfoThatContains($message)
46 * @method bool hasDebugThatContains($message)
47 *
48 * @method bool hasEmergencyThatMatches($message)
49 * @method bool hasAlertThatMatches($message)
50 * @method bool hasCriticalThatMatches($message)
51 * @method bool hasErrorThatMatches($message)
52 * @method bool hasWarningThatMatches($message)
53 * @method bool hasNoticeThatMatches($message)
54 * @method bool hasInfoThatMatches($message)
55 * @method bool hasDebugThatMatches($message)
56 *
57 * @method bool hasEmergencyThatPasses($message)
58 * @method bool hasAlertThatPasses($message)
59 * @method bool hasCriticalThatPasses($message)
60 * @method bool hasErrorThatPasses($message)
61 * @method bool hasWarningThatPasses($message)
62 * @method bool hasNoticeThatPasses($message)
63 * @method bool hasInfoThatPasses($message)
64 * @method bool hasDebugThatPasses($message)
65 */
66 class TestHandler extends AbstractProcessingHandler
67 {
68 protected $records = array();
69 protected $recordsByLevel = array();
70  
71 public function getRecords()
72 {
73 return $this->records;
74 }
75  
76 public function clear()
77 {
78 $this->records = array();
79 $this->recordsByLevel = array();
80 }
81  
82 public function hasRecords($level)
83 {
84 return isset($this->recordsByLevel[$level]);
85 }
86  
87 public function hasRecord($record, $level)
88 {
89 if (is_array($record)) {
90 $record = $record['message'];
91 }
92  
93 return $this->hasRecordThatPasses(function ($rec) use ($record) {
94 return $rec['message'] === $record;
95 }, $level);
96 }
97  
98 public function hasRecordThatContains($message, $level)
99 {
100 return $this->hasRecordThatPasses(function ($rec) use ($message) {
101 return strpos($rec['message'], $message) !== false;
102 }, $level);
103 }
104  
105 public function hasRecordThatMatches($regex, $level)
106 {
107 return $this->hasRecordThatPasses(function ($rec) use ($regex) {
108 return preg_match($regex, $rec['message']) > 0;
109 }, $level);
110 }
111  
112 public function hasRecordThatPasses($predicate, $level)
113 {
114 if (!is_callable($predicate)) {
115 throw new \InvalidArgumentException("Expected a callable for hasRecordThatSucceeds");
116 }
117  
118 if (!isset($this->recordsByLevel[$level])) {
119 return false;
120 }
121  
122 foreach ($this->recordsByLevel[$level] as $i => $rec) {
123 if (call_user_func($predicate, $rec, $i)) {
124 return true;
125 }
126 }
127  
128 return false;
129 }
130  
131 /**
132 * {@inheritdoc}
133 */
134 protected function write(array $record)
135 {
136 $this->recordsByLevel[$record['level']][] = $record;
137 $this->records[] = $record;
138 }
139  
140 public function __call($method, $args)
141 {
142 if (preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) {
143 $genericMethod = $matches[1] . ('Records' !== $matches[3] ? 'Record' : '') . $matches[3];
144 $level = constant('Monolog\Logger::' . strtoupper($matches[2]));
145 if (method_exists($this, $genericMethod)) {
146 $args[] = $level;
147  
148 return call_user_func_array(array($this, $genericMethod), $args);
149 }
150 }
151  
152 throw new \BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()');
153 }
154 }