| 199 |
lars |
1 |
<?php namespace Clockwork\Storage;
|
|
|
2 |
|
|
|
3 |
use Clockwork\Request\Request;
|
|
|
4 |
use Clockwork\Request\RequestType;
|
|
|
5 |
|
|
|
6 |
// Rules for searching requests
|
|
|
7 |
class Search
|
|
|
8 |
{
|
|
|
9 |
// Search parameters
|
|
|
10 |
public $uri = [];
|
|
|
11 |
public $controller = [];
|
|
|
12 |
public $method = [];
|
|
|
13 |
public $status = [];
|
|
|
14 |
public $time = [];
|
|
|
15 |
public $received = [];
|
|
|
16 |
public $name = [];
|
|
|
17 |
public $type = [];
|
|
|
18 |
|
|
|
19 |
// Whether to stop search on the first not matching request
|
|
|
20 |
public $stopOnFirstMismatch = false;
|
|
|
21 |
|
|
|
22 |
// Create a new instance, takes search parameters and additional options
|
|
|
23 |
public function __construct($search = [], $options = [])
|
|
|
24 |
{
|
|
|
25 |
foreach ([ 'uri', 'controller', 'method', 'status', 'time', 'received', 'name', 'type' ] as $condition) {
|
|
|
26 |
$this->$condition = isset($search[$condition]) ? $search[$condition] : [];
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
foreach ([ 'stopOnFirstMismatch' ] as $option) {
|
|
|
30 |
$this->$option = isset($options[$option]) ? $options[$option] : $this->$condition;
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
$this->method = array_map('strtolower', $this->method);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
// Create a new instance from request input
|
|
|
37 |
public static function fromRequest($data = [])
|
|
|
38 |
{
|
|
|
39 |
return new static($data);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
// Check whether the request matches current search parameters
|
|
|
43 |
public function matches(Request $request)
|
|
|
44 |
{
|
|
|
45 |
if ($request->type == RequestType::COMMAND) {
|
|
|
46 |
return $this->matchesCommand($request);
|
|
|
47 |
} elseif ($request->type == RequestType::QUEUE_JOB) {
|
|
|
48 |
return $this->matchesQueueJob($request);
|
|
|
49 |
} elseif ($request->type == RequestType::TEST) {
|
|
|
50 |
return $this->matchesTest($request);
|
|
|
51 |
} else {
|
|
|
52 |
return $this->matchesRequest($request);
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
// Check whether a request type request matches
|
|
|
57 |
protected function matchesRequest(Request $request)
|
|
|
58 |
{
|
|
|
59 |
return $this->matchesString($this->type, RequestType::REQUEST)
|
|
|
60 |
&& $this->matchesString($this->uri, $request->uri)
|
|
|
61 |
&& $this->matchesString($this->controller, $request->controller)
|
|
|
62 |
&& $this->matchesExact($this->method, strtolower($request->method))
|
|
|
63 |
&& $this->matchesNumber($this->status, $request->responseStatus)
|
|
|
64 |
&& $this->matchesNumber($this->time, $request->responseDuration)
|
|
|
65 |
&& $this->matchesDate($this->received, $request->time);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
// Check whether a command type request matches
|
|
|
69 |
protected function matchesCommand(Request $request)
|
|
|
70 |
{
|
|
|
71 |
return $this->matchesString($this->type, RequestType::COMMAND)
|
|
|
72 |
&& $this->matchesString($this->name, $request->commandName)
|
|
|
73 |
&& $this->matchesNumber($this->status, $request->commandExitCode)
|
|
|
74 |
&& $this->matchesNumber($this->time, $request->responseDuration)
|
|
|
75 |
&& $this->matchesDate($this->received, $request->time);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
// Check whether a queue-job type request matches
|
|
|
79 |
protected function matchesQueueJob(Request $request)
|
|
|
80 |
{
|
|
|
81 |
return $this->matchesString($this->type, RequestType::QUEUE_JOB)
|
|
|
82 |
&& $this->matchesString($this->name, $request->jobName)
|
|
|
83 |
&& $this->matchesString($this->status, $request->jobStatus)
|
|
|
84 |
&& $this->matchesNumber($this->time, $request->responseDuration)
|
|
|
85 |
&& $this->matchesDate($this->received, $request->time);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// Check whether a test type request matches
|
|
|
89 |
protected function matchesTest(Request $request)
|
|
|
90 |
{
|
|
|
91 |
return $this->matchesString($this->type, RequestType::TEST)
|
|
|
92 |
&& $this->matchesString($this->name, $request->testName)
|
|
|
93 |
&& $this->matchesString($this->status, $request->testStatus)
|
|
|
94 |
&& $this->matchesNumber($this->time, $request->responseDuration)
|
|
|
95 |
&& $this->matchesDate($this->received, $request->time);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
// Check if there are no search parameters specified
|
|
|
99 |
public function isEmpty()
|
|
|
100 |
{
|
|
|
101 |
return ! count($this->uri) && ! count($this->controller) && ! count($this->method) && ! count($this->status)
|
|
|
102 |
&& ! count($this->time) && ! count($this->received) && ! count($this->name) && ! count($this->type);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
// Check if there are some search parameters specified
|
|
|
106 |
public function isNotEmpty()
|
|
|
107 |
{
|
|
|
108 |
return ! $this->isEmpty();
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
// Check if the value matches date type search parameter
|
|
|
112 |
protected function matchesDate($inputs, $value)
|
|
|
113 |
{
|
|
|
114 |
if (! count($inputs)) return true;
|
|
|
115 |
|
|
|
116 |
foreach ($inputs as $input) {
|
|
|
117 |
if (preg_match('/^<(.+)$/', $input, $match)) {
|
|
|
118 |
if ($value < strtotime($match[1])) return true;
|
|
|
119 |
} elseif (preg_match('/^>(.+)$/', $input, $match)) {
|
|
|
120 |
if ($value > strtotime($match[1])) return true;
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
return false;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
// Check if the value matches exact type search parameter
|
|
|
128 |
protected function matchesExact($inputs, $value)
|
|
|
129 |
{
|
|
|
130 |
if (! count($inputs)) return true;
|
|
|
131 |
|
|
|
132 |
return in_array($value, $inputs);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
// Check if the value matches number type search parameter
|
|
|
136 |
protected function matchesNumber($inputs, $value)
|
|
|
137 |
{
|
|
|
138 |
if (! count($inputs)) return true;
|
|
|
139 |
|
|
|
140 |
foreach ($inputs as $input) {
|
|
|
141 |
if (preg_match('/^<(\d+(?:\.\d+)?)$/', $input, $match)) {
|
|
|
142 |
if ($value < $match[1]) return true;
|
|
|
143 |
} elseif (preg_match('/^>(\d+(?:\.\d+)?)$/', $input, $match)) {
|
|
|
144 |
if ($value > $match[1]) return true;
|
|
|
145 |
} elseif (preg_match('/^(\d+(?:\.\d+)?)-(\d+(?:\.\d+)?)$/', $input, $match)) {
|
|
|
146 |
if ($match[1] < $value && $value < $match[2]) return true;
|
|
|
147 |
} else {
|
|
|
148 |
if ($value == $input) return true;
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
return false;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
// Check if the value matches string type search parameter
|
|
|
156 |
protected function matchesString($inputs, $value)
|
|
|
157 |
{
|
|
|
158 |
if (! count($inputs)) return true;
|
|
|
159 |
|
|
|
160 |
foreach ($inputs as $input) {
|
|
|
161 |
if (strpos($value, $input) !== false) return true;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
return false;
|
|
|
165 |
}
|
|
|
166 |
}
|