| 199 |
lars |
1 |
<?php namespace Clockwork\Support\Slim\Old;
|
|
|
2 |
|
|
|
3 |
use Clockwork\Clockwork;
|
|
|
4 |
use Clockwork\DataSource\PhpDataSource;
|
|
|
5 |
use Clockwork\DataSource\SlimDataSource;
|
|
|
6 |
use Clockwork\Helpers\ServerTiming;
|
|
|
7 |
use Clockwork\Storage\FileStorage;
|
|
|
8 |
|
|
|
9 |
use Slim\Middleware;
|
|
|
10 |
|
|
|
11 |
// Slim 2 middleware
|
|
|
12 |
class ClockworkMiddleware extends Middleware
|
|
|
13 |
{
|
|
|
14 |
private $storagePathOrClockwork;
|
|
|
15 |
|
|
|
16 |
public function __construct($storagePathOrClockwork)
|
|
|
17 |
{
|
|
|
18 |
$this->storagePathOrClockwork = $storagePathOrClockwork;
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
public function call()
|
|
|
22 |
{
|
|
|
23 |
$this->app->container->singleton('clockwork', function () {
|
|
|
24 |
if ($this->storagePathOrClockwork instanceof Clockwork) {
|
|
|
25 |
return $this->storagePathOrClockwork;
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
$clockwork = new Clockwork();
|
|
|
29 |
|
|
|
30 |
$clockwork->addDataSource(new PhpDataSource())
|
|
|
31 |
->addDataSource(new SlimDataSource($this->app))
|
|
|
32 |
->storage(new FileStorage($this->storagePathOrClockwork));
|
|
|
33 |
|
|
|
34 |
return $clockwork;
|
|
|
35 |
});
|
|
|
36 |
|
|
|
37 |
$originalLogWriter = $this->app->getLog()->getWriter();
|
|
|
38 |
$clockworkLogWriter = new ClockworkLogWriter($this->app->clockwork, $originalLogWriter);
|
|
|
39 |
|
|
|
40 |
$this->app->getLog()->setWriter($clockworkLogWriter);
|
|
|
41 |
|
|
|
42 |
$clockworkDataUri = '#/__clockwork(?:/(?<id>[0-9-]+))?(?:/(?<direction>(?:previous|next)))?(?:/(?<count>\d+))?#';
|
|
|
43 |
if ($this->app->config('debug') && preg_match($clockworkDataUri, $this->app->request()->getPathInfo(), $matches)) {
|
|
|
44 |
$matches = array_merge([ 'direction' => null, 'count' => null ], $matches);
|
|
|
45 |
return $this->retrieveRequest($matches['id'], $matches['direction'], $matches['count']);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
try {
|
|
|
49 |
$this->next->call();
|
|
|
50 |
$this->logRequest();
|
|
|
51 |
} catch (Exception $e) {
|
|
|
52 |
$this->logRequest();
|
|
|
53 |
throw $e;
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public function retrieveRequest($id = null, $direction = null, $count = null)
|
|
|
58 |
{
|
|
|
59 |
$storage = $this->app->clockwork->storage();
|
|
|
60 |
|
|
|
61 |
if ($direction == 'previous') {
|
|
|
62 |
$data = $storage->previous($id, $count);
|
|
|
63 |
} elseif ($direction == 'next') {
|
|
|
64 |
$data = $storage->next($id, $count);
|
|
|
65 |
} elseif ($id == 'latest') {
|
|
|
66 |
$data = $storage->latest();
|
|
|
67 |
} else {
|
|
|
68 |
$data = $storage->find($id);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
echo json_encode($data, \JSON_PARTIAL_OUTPUT_ON_ERROR);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
protected function logRequest()
|
|
|
75 |
{
|
|
|
76 |
$this->app->clockwork->resolveRequest();
|
|
|
77 |
$this->app->clockwork->storeRequest();
|
|
|
78 |
|
|
|
79 |
if ($this->app->config('debug')) {
|
|
|
80 |
$this->app->response()->header('X-Clockwork-Id', $this->app->clockwork->request()->id);
|
|
|
81 |
$this->app->response()->header('X-Clockwork-Version', Clockwork::VERSION);
|
|
|
82 |
|
|
|
83 |
$env = $this->app->environment();
|
|
|
84 |
if ($env['SCRIPT_NAME']) {
|
|
|
85 |
$this->app->response()->header('X-Clockwork-Path', $env['SCRIPT_NAME'] . '/__clockwork/');
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$request = $this->app->clockwork->request();
|
|
|
89 |
$this->app->response()->header('Server-Timing', ServerTiming::fromRequest($request)->value());
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
}
|