Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
199 lars 1
<?php namespace Clockwork\Support\Slim\Legacy;
2
 
3
use Clockwork\Clockwork;
4
use Clockwork\Authentication\NullAuthenticator;
5
use Clockwork\DataSource\PsrMessageDataSource;
6
use Clockwork\Storage\FileStorage;
7
use Clockwork\Helpers\ServerTiming;
8
 
9
use Psr\Http\Message\ResponseInterface as Response;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
 
12
// Slim 3 middleware
13
class ClockworkMiddleware
14
{
15
	protected $clockwork;
16
	protected $startTime;
17
 
18
	public function __construct($storagePathOrClockwork, $startTime = null)
19
	{
20
		$this->clockwork = $storagePathOrClockwork instanceof Clockwork
21
			? $storagePathOrClockwork : $this->createDefaultClockwork($storagePathOrClockwork);
22
		$this->startTime = $startTime ?: microtime(true);
23
	}
24
 
25
	public function __invoke(Request $request, Response $response, callable $next)
26
	{
27
		return $this->process($request, $response, $next);
28
	}
29
 
30
	public function process(Request $request, Response $response, callable $next)
31
	{
32
		$authUri = '#/__clockwork/auth#';
33
		if (preg_match($authUri, $request->getUri()->getPath(), $matches)) {
34
			return $this->authenticate($response, $request);
35
		}
36
 
37
		$clockworkDataUri = '#/__clockwork(?:/(?<id>[0-9-]+))?(?:/(?<direction>(?:previous|next)))?(?:/(?<count>\d+))?#';
38
		if (preg_match($clockworkDataUri, $request->getUri()->getPath(), $matches)) {
39
			$matches = array_merge([ 'id' => null, 'direction' => null, 'count' => null ], $matches);
40
			return $this->retrieveRequest($response, $request, $matches['id'], $matches['direction'], $matches['count']);
41
		}
42
 
43
		$response = $next($request, $response);
44
 
45
		return $this->logRequest($request, $response);
46
	}
47
 
48
	protected function authenticate(Response $response, Request $request)
49
	{
50
		$token = $this->clockwork->authenticator()->attempt($request->getParsedBody());
51
 
52
		return $response->withJson([ 'token' => $token ])->withStatus($token ? 200 : 403);
53
	}
54
 
55
	protected function retrieveRequest(Response $response, Request $request, $id, $direction, $count)
56
	{
57
		$authenticator = $this->clockwork->authenticator();
58
		$storage = $this->clockwork->storage();
59
 
60
		$authenticated = $authenticator->check(current($request->getHeader('X-Clockwork-Auth')));
61
 
62
		if ($authenticated !== true) {
63
			return $response
64
				->withJson([ 'message' => $authenticated, 'requires' => $authenticator->requires() ])
65
				->withStatus(403);
66
		}
67
 
68
		if ($direction == 'previous') {
69
			$data = $storage->previous($id, $count);
70
		} elseif ($direction == 'next') {
71
			$data = $storage->next($id, $count);
72
		} elseif ($id == 'latest') {
73
			$data = $storage->latest();
74
		} else {
75
			$data = $storage->find($id);
76
		}
77
 
78
		return $response
79
			->withHeader('Content-Type', 'application/json')
80
			->withJson($data);
81
	}
82
 
83
	protected function logRequest(Request $request, Response $response)
84
	{
85
		$this->clockwork->timeline()->finalize($this->startTime);
86
		$this->clockwork->addDataSource(new PsrMessageDataSource($request, $response));
87
 
88
		$this->clockwork->resolveRequest();
89
		$this->clockwork->storeRequest();
90
 
91
		$clockworkRequest = $this->clockwork->request();
92
 
93
		$response = $response
94
			->withHeader('X-Clockwork-Id', $clockworkRequest->id)
95
			->withHeader('X-Clockwork-Version', Clockwork::VERSION);
96
 
97
		if ($basePath = $request->getUri()->getBasePath()) {
98
			$response = $response->withHeader('X-Clockwork-Path', $basePath);
99
		}
100
 
101
		return $response->withHeader('Server-Timing', ServerTiming::fromRequest($clockworkRequest)->value());
102
	}
103
 
104
	protected function createDefaultClockwork($storagePath)
105
	{
106
		$clockwork = new Clockwork();
107
 
108
		$clockwork->storage(new FileStorage($storagePath));
109
		$clockwork->authenticator(new NullAuthenticator);
110
 
111
		return $clockwork;
112
	}
113
}