Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
199 lars 1
<?php namespace Clockwork;
2
 
3
use Clockwork\Authentication\AuthenticatorInterface;
4
use Clockwork\Authentication\NullAuthenticator;
5
use Clockwork\DataSource\DataSourceInterface;
6
use Clockwork\Helpers\Serializer;
7
use Clockwork\Request\Log;
8
use Clockwork\Request\Request;
9
use Clockwork\Request\RequestType;
10
use Clockwork\Request\ShouldCollect;
11
use Clockwork\Request\ShouldRecord;
12
use Clockwork\Storage\StorageInterface;
13
 
14
// A central class implementing the core flow of the library
15
class Clockwork
16
{
17
	// Clockwork library version
18
	const VERSION = '5.1.12';
19
 
20
	// Array of data sources, these objects collect metadata for the current application run
21
	protected $dataSources = [];
22
 
23
	// Request object, data structure which stores metadata about the current application run
24
	protected $request;
25
 
26
	// Storage object, provides implementation for storing and retrieving request objects
27
	protected $storage;
28
 
29
	// Authenticator implementation, authenticates requests for clockwork metadata
30
	protected $authenticator;
31
 
32
	// An object specifying the rules for collecting requests
33
	protected $shouldCollect;
34
 
35
	// An object specifying the rules for recording requests
36
	protected $shouldRecord;
37
 
38
	// Create a new Clockwork instance with default request object, a storage implementation has to be additionally set
39
	public function __construct()
40
	{
41
		$this->request = new Request;
42
		$this->authenticator = new NullAuthenticator;
43
 
44
		$this->shouldCollect = new ShouldCollect;
45
		$this->shouldRecord = new ShouldRecord;
46
	}
47
 
48
	// Add a new data source
49
	public function addDataSource(DataSourceInterface $dataSource)
50
	{
51
		$this->dataSources[] = $dataSource;
52
		return $this;
53
	}
54
 
55
	// Resolve the current request, sending it through all data sources, finalizing log and timeline
56
	public function resolveRequest()
57
	{
58
		foreach ($this->dataSources as $dataSource) {
59
			$dataSource->resolve($this->request);
60
		}
61
 
62
		$this->request->log()->sort();
63
		$this->request->timeline()->finalize($this->request->time);
64
 
65
		return $this;
66
	}
67
 
68
	// Resolve the current request as a "command" type request with command-specific data
69
	public function resolveAsCommand($name, $exitCode = null, $arguments = [], $options = [], $argumentsDefaults = [], $optionsDefaults = [], $output = null)
70
	{
71
		$this->resolveRequest();
72
 
73
		$this->request->type = RequestType::COMMAND;
74
		$this->request->commandName = $name;
75
		$this->request->commandArguments = $arguments;
76
		$this->request->commandArgumentsDefaults = $argumentsDefaults;
77
		$this->request->commandOptions = $options;
78
		$this->request->commandOptionsDefaults = $optionsDefaults;
79
		$this->request->commandExitCode = $exitCode;
80
		$this->request->commandOutput = $output;
81
 
82
		return $this;
83
	}
84
 
85
	// Resolve the current request as a "queue-job" type request with queue-job-specific data
86
	public function resolveAsQueueJob($name, $description = null, $status = 'processed', $payload = [], $queue = null, $connection = null, $options = [])
87
	{
88
		$this->resolveRequest();
89
 
90
		$this->request->type = RequestType::QUEUE_JOB;
91
		$this->request->jobName = $name;
92
		$this->request->jobDescription = $description;
93
		$this->request->jobStatus = $status;
94
		$this->request->jobPayload = (new Serializer)->normalize($payload);
95
		$this->request->jobQueue = $queue;
96
		$this->request->jobConnection = $connection;
97
		$this->request->jobOptions = (new Serializer)->normalizeEach($options);
98
 
99
		return $this;
100
	}
101
 
102
	// Resolve the current request as a "test" type request with test-specific data, accepts test name, status, status
103
	// message in case of failure and array of ran asserts
104
	public function resolveAsTest($name, $status = 'passed', $statusMessage = null, $asserts = [])
105
	{
106
		$this->resolveRequest();
107
 
108
		$this->request->type = RequestType::TEST;
109
		$this->request->testName = $name;
110
		$this->request->testStatus = $status;
111
		$this->request->testStatusMessage = $statusMessage;
112
 
113
		foreach ($asserts as $assert) {
114
			$this->request->addTestAssert($assert['name'], $assert['arguments'], $assert['passed'], $assert['trace']);
115
		}
116
 
117
		return $this;
118
	}
119
 
120
	// Extends the request with an additional data form all data sources, which is not required for normal use
121
	public function extendRequest(Request $request = null)
122
	{
123
		foreach ($this->dataSources as $dataSource) {
124
			$dataSource->extend($request ?: $this->request);
125
		}
126
 
127
		return $this;
128
	}
129
 
130
	// Store the current request via configured storage implementation
131
	public function storeRequest()
132
	{
133
		return $this->storage->store($this->request);
134
	}
135
 
136
	// Reset all data sources to an empty state, clearing any collected data
137
	public function reset()
138
	{
139
		foreach ($this->dataSources as $dataSource) {
140
			$dataSource->reset();
141
		}
142
 
143
		return $this;
144
	}
145
 
146
	// Get or set the current request instance
147
	public function request(Request $request = null)
148
	{
149
		if (! $request) return $this->request;
150
 
151
		$this->request = $request;
152
		return $this;
153
	}
154
 
155
	// Get the log instance for the current request or log a new message
156
	public function log($level = null, $message = null, array $context = [])
157
	{
158
		if ($level) {
159
			return $this->request->log()->log($level, $message, $context);
160
		}
161
 
162
		return $this->request->log();
163
	}
164
 
165
	// Get the timeline instance for the current request
166
	public function timeline()
167
	{
168
		return $this->request->timeline();
169
	}
170
 
171
	// Shortcut to create a new event on the current timeline instance
172
	public function event($description, $data = [])
173
	{
174
		return $this->request->timeline()->event($description, $data);
175
	}
176
 
177
	// Configure which requests should be collected, can be called with arrey of options, a custom closure or with no
178
	// arguments for a fluent configuration api
179
	public function shouldCollect($shouldCollect = null)
180
	{
181
		if ($shouldCollect instanceof Closure) return $this->shouldCollect->callback($shouldCollect);
182
 
183
		if (is_array($shouldCollect)) return $this->shouldCollect->merge($shouldCollect);
184
 
185
		return $this->shouldCollect;
186
	}
187
 
188
	// Configure which requests should be recorded, can be called with arrey of options, a custom closure or with no
189
	// arguments for a fluent configuration api
190
	public function shouldRecord($shouldRecord = null)
191
	{
192
		if ($shouldRecord instanceof Closure) return $this->shouldRecord->callback($shouldRecord);
193
 
194
		if (is_array($shouldRecord)) return $this->shouldRecord->merge($shouldRecord);
195
 
196
		return $this->shouldRecord;
197
	}
198
 
199
	// Get or set all data sources at once
200
	public function dataSources($dataSources = null)
201
	{
202
		if (! $dataSources) return $this->dataSources;
203
 
204
		$this->dataSources = $dataSources;
205
		return $this;
206
	}
207
 
208
	// Get or set a storage implementation
209
	public function storage(StorageInterface $storage = null)
210
	{
211
		if (! $storage) return $this->storage;
212
 
213
		$this->storage = $storage;
214
		return $this;
215
	}
216
 
217
	// Get or set an authenticator implementation
218
	public function authenticator(AuthenticatorInterface $authenticator = null)
219
	{
220
		if (! $authenticator) return $this->authenticator;
221
 
222
		$this->authenticator = $authenticator;
223
		return $this;
224
	}
225
 
226
	// Forward any other method calls to the current request and log instances
227
	public function __call($method, $args)
228
	{
229
		if (in_array($method, [ 'emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug' ])) {
230
			return $this->request->log()->$method(...$args);
231
		}
232
 
233
		return $this->request->$method(...$args);
234
	}
235
 
236
	// DEPRECATED The following apis are deprecated and will be removed in a future version
237
 
238
	// Get all added data sources
239
	public function getDataSources()
240
	{
241
		return $this->dataSources;
242
	}
243
 
244
	// Get the current request instance
245
	public function getRequest()
246
	{
247
		return $this->request;
248
	}
249
 
250
	// Set the current request instance
251
	public function setRequest(Request $request)
252
	{
253
		$this->request = $request;
254
		return $this;
255
	}
256
 
257
	// Get a storage implementation
258
	public function getStorage()
259
	{
260
		return $this->storage;
261
	}
262
 
263
	// Set a storage implementation
264
	public function setStorage(StorageInterface $storage)
265
	{
266
		$this->storage = $storage;
267
		return $this;
268
	}
269
 
270
	// Get an authenticator implementation
271
	public function getAuthenticator()
272
	{
273
		return $this->authenticator;
274
	}
275
 
276
	// Set an authenticator implementation
277
	public function setAuthenticator(AuthenticatorInterface $authenticator)
278
	{
279
		$this->authenticator = $authenticator;
280
		return $this;
281
	}
282
}