| 199 |
lars |
1 |
<?php namespace Clockwork\Support\Symfony;
|
|
|
2 |
|
|
|
3 |
use Clockwork\Authentication\NullAuthenticator;
|
|
|
4 |
use Clockwork\Authentication\SimpleAuthenticator;
|
|
|
5 |
use Clockwork\Storage\Search;
|
|
|
6 |
use Clockwork\Web\Web;
|
|
|
7 |
|
|
|
8 |
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
9 |
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
10 |
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
11 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
12 |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
13 |
|
|
|
14 |
class ClockworkSupport
|
|
|
15 |
{
|
|
|
16 |
protected $container;
|
|
|
17 |
protected $config;
|
|
|
18 |
|
|
|
19 |
public function __construct(ContainerInterface $container, $config)
|
|
|
20 |
{
|
|
|
21 |
$this->container = $container;
|
|
|
22 |
$this->config = $config;
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
public function getConfig($key, $default = null)
|
|
|
26 |
{
|
|
|
27 |
return isset($this->config[$key]) ? $this->config[$key] : $default;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
public function getData(Request $request, $id = null, $direction = null, $count = null)
|
|
|
31 |
{
|
|
|
32 |
$authenticator = $this->container->get('clockwork')->authenticator();
|
|
|
33 |
$storage = $this->container->get('clockwork')->storage();
|
|
|
34 |
|
|
|
35 |
$authenticated = $authenticator->check($request->headers->get('X-Clockwork-Auth'));
|
|
|
36 |
|
|
|
37 |
if ($authenticated !== true) {
|
|
|
38 |
return new JsonResponse([ 'message' => $authenticated, 'requires' => $authenticator->requires() ], 403);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
if ($direction == 'previous') {
|
|
|
42 |
$data = $storage->previous($id, $count, Search::fromRequest($request->query->all()));
|
|
|
43 |
} elseif ($direction == 'next') {
|
|
|
44 |
$data = $storage->next($id, $count, Search::fromRequest($request->query->all()));
|
|
|
45 |
} elseif ($id == 'latest') {
|
|
|
46 |
$data = $storage->latest(Search::fromRequest($request->query->all()));
|
|
|
47 |
} else {
|
|
|
48 |
$data = $storage->find($id);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
$data = is_array($data)
|
|
|
52 |
? array_map(function ($request) { return $request->toArray(); }, $data)
|
|
|
53 |
: $data->toArray();
|
|
|
54 |
|
|
|
55 |
return new JsonResponse($data);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public function getWebAsset($path)
|
|
|
59 |
{
|
|
|
60 |
$web = new Web;
|
|
|
61 |
|
|
|
62 |
if ($asset = $web->asset($path)) {
|
|
|
63 |
return new BinaryFileResponse($asset['path'], 200, [ 'Content-Type' => $asset['mime'] ]);
|
|
|
64 |
} else {
|
|
|
65 |
throw new NotFoundHttpException;
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public function makeAuthenticator()
|
|
|
70 |
{
|
|
|
71 |
$authenticator = $this->getConfig('authentication');
|
|
|
72 |
|
|
|
73 |
if (is_string($authenticator)) {
|
|
|
74 |
return $this->container->get($authenticator);
|
|
|
75 |
} elseif ($authenticator) {
|
|
|
76 |
return new SimpleAuthenticator($this->getConfig('authentication_password'));
|
|
|
77 |
} else {
|
|
|
78 |
return new NullAuthenticator;
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public function isEnabled()
|
|
|
83 |
{
|
|
|
84 |
return $this->getConfig('enable', false);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public function isWebEnabled()
|
|
|
88 |
{
|
|
|
89 |
return $this->getConfig('web', true);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public function webPaths()
|
|
|
93 |
{
|
|
|
94 |
$path = $this->getConfig('web', true);
|
|
|
95 |
|
|
|
96 |
if (is_string($path)) return [ trim($path, '/') ];
|
|
|
97 |
|
|
|
98 |
return [ 'clockwork', '__clockwork' ];
|
|
|
99 |
}
|
|
|
100 |
}
|