| 199 |
lars |
1 |
<?php namespace Clockwork\Support\Lumen;
|
|
|
2 |
|
|
|
3 |
use Clockwork\Clockwork;
|
|
|
4 |
use Clockwork\Support\Lumen\ClockworkSupport;
|
|
|
5 |
|
|
|
6 |
use Illuminate\Http\JsonResponse;
|
|
|
7 |
use Illuminate\Http\RedirectResponse;
|
|
|
8 |
use Illuminate\Http\Request;
|
|
|
9 |
use Laravel\Lumen\Routing\Controller as LumenController;
|
|
|
10 |
use Laravel\Telescope\Telescope;
|
|
|
11 |
|
|
|
12 |
// Clockwork api and app controller
|
|
|
13 |
class Controller extends LumenController
|
|
|
14 |
{
|
|
|
15 |
// Clockwork and support instances
|
|
|
16 |
public $clockwork;
|
|
|
17 |
public $clockworkSupport;
|
|
|
18 |
|
|
|
19 |
public function __construct(Clockwork $clockwork, ClockworkSupport $clockworkSupport)
|
|
|
20 |
{
|
|
|
21 |
$this->clockwork = $clockwork;
|
|
|
22 |
$this->clockworkSupport = $clockworkSupport;
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
// Authantication endpoint
|
|
|
26 |
public function authenticate(Request $request)
|
|
|
27 |
{
|
|
|
28 |
$this->ensureClockworkIsEnabled();
|
|
|
29 |
|
|
|
30 |
$token = $this->clockwork->authenticator()->attempt(
|
|
|
31 |
$request->only([ 'username', 'password' ])
|
|
|
32 |
);
|
|
|
33 |
|
|
|
34 |
return new JsonResponse([ 'token' => $token ], $token ? 200 : 403);
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
// Metadata retrieving endpoint
|
|
|
38 |
public function getData(Request $request, $id = null, $direction = null, $count = null)
|
|
|
39 |
{
|
|
|
40 |
$this->ensureClockworkIsEnabled();
|
|
|
41 |
|
|
|
42 |
return $this->clockworkSupport->getData(
|
|
|
43 |
$id, $direction, $count, $request->only([ 'only', 'except' ])
|
|
|
44 |
);
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
// Extended metadata retrieving endpoint
|
|
|
48 |
public function getExtendedData(Request $request, $id = null)
|
|
|
49 |
{
|
|
|
50 |
$this->ensureClockworkIsEnabled();
|
|
|
51 |
|
|
|
52 |
return $this->clockworkSupport->getExtendedData(
|
|
|
53 |
$id, $request->only([ 'only', 'except' ])
|
|
|
54 |
);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
// Metadata updating endpoint
|
|
|
58 |
public function updateData(Request $request, $id = null)
|
|
|
59 |
{
|
|
|
60 |
$this->ensureClockworkIsEnabled();
|
|
|
61 |
|
|
|
62 |
return $this->clockworkSupport->updateData($id, $request->json()->all());
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// App index
|
|
|
66 |
public function webIndex(Request $request)
|
|
|
67 |
{
|
|
|
68 |
$this->ensureClockworkIsEnabled();
|
|
|
69 |
|
|
|
70 |
return $this->clockworkSupport->getWebAsset('index.html');
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// App assets serving
|
|
|
74 |
public function webAsset($path)
|
|
|
75 |
{
|
|
|
76 |
$this->ensureClockworkIsEnabled();
|
|
|
77 |
|
|
|
78 |
return $this->clockworkSupport->getWebAsset($path);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
// App redirect (/clockwork -> /clockwork/app)
|
|
|
82 |
public function webRedirect(Request $request)
|
|
|
83 |
{
|
|
|
84 |
$this->ensureClockworkIsEnabled();
|
|
|
85 |
|
|
|
86 |
return new RedirectResponse('/' . $request->path() . '/app');
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
// Ensure Clockwork is still enabled at this point and stop Telescope recording if present
|
|
|
90 |
protected function ensureClockworkIsEnabled()
|
|
|
91 |
{
|
|
|
92 |
if (class_exists(Telescope::class)) Telescope::stopRecording();
|
|
|
93 |
|
|
|
94 |
if (! $this->clockworkSupport->isEnabled()) abort(404);
|
|
|
95 |
}
|
|
|
96 |
}
|