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