-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathResource.php
180 lines (161 loc) · 4.77 KB
/
Resource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Lomkit\Rest\Http;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable;
use Lomkit\Rest\Actions\Actionable;
use Lomkit\Rest\Concerns\Authorizable;
use Lomkit\Rest\Concerns\PerformsModelOperations;
use Lomkit\Rest\Concerns\PerformsQueries;
use Lomkit\Rest\Concerns\Resource\ConfiguresRestParameters;
use Lomkit\Rest\Concerns\Resource\HasResourceHooks;
use Lomkit\Rest\Concerns\Resource\Operatable;
use Lomkit\Rest\Concerns\Resource\Paginable;
use Lomkit\Rest\Concerns\Resource\Relationable;
use Lomkit\Rest\Concerns\Resource\Rulable;
use Lomkit\Rest\Concerns\Resource\Scoutable;
use Lomkit\Rest\Http\Requests\RestRequest;
use Lomkit\Rest\Instructions\Instructionable;
class Resource implements \JsonSerializable
{
use PerformsQueries;
use PerformsModelOperations;
use Relationable;
use Paginable;
use Scoutable;
use Rulable;
use ConfiguresRestParameters;
use Authorizable;
use Actionable;
use Instructionable;
use HasResourceHooks;
use Operatable;
/**
* The model the entry corresponds to.
*
* @var class-string<Model>
*/
public static $model;
/**
* The reponse the entry corresponds to.
*
* @var class-string<Response>
*/
public static $response = Response::class;
/**
* Get a fresh instance of the model represented by the resource.
*
* @return Model
*/
public static function newModel()
{
/** @var Model $model */
$model = static::$model;
return new $model();
}
/**
* Get a fresh instance of the model represented by the resource.
*
* @return Response
*/
public static function newResponse()
{
/** @var Response $response */
$response = static::$response;
return new $response();
}
/**
* Return the default ordering for resource queries.
*
* @param RestRequest $request
*
* @return array
*/
public function defaultOrderBy(RestRequest $request): array
{
return [
'id' => 'desc',
];
}
/**
* Check if gating is enabled for this resource.
*
* @return bool
*/
public function isGatingEnabled(): bool
{
return config('rest.gates.enabled', true);
}
/**
* Check if authorizations are enabled for this resource.
*
* @return bool
*/
public function isAuthorizingEnabled(): bool
{
return config('rest.authorizations.enabled', true);
}
/**
* Check if authorization cache is enabled for this resource.
*
* @return bool
*/
public function isAuthorizationCacheEnabled(): bool
{
return config('rest.authorizations.cache.enabled', true);
}
/**
* Get the authorization cache key.
*
* @param RestRequest $request
*
* @return string
*/
public function getAuthorizationCacheKey(RestRequest $request, string $identifier)
{
$class = Str::snake((new \ReflectionClass($this))->getShortName());
return sprintf(
'rest.authorization.%s.%s.%s',
$class,
$identifier,
$request->user()?->getKey()
);
}
/**
* Determine for how much time the authorization cache should be keeped.
*
* @return \DateTimeInterface|\DateInterval
*/
public function cacheAuthorizationFor()
{
return now()->addMinutes(config('rest.authorizations.cache.default', 5));
}
public function isModelSearchable()
{
return in_array(Searchable::class, class_uses_recursive(static::$model));
}
/**
* Serialize the resource into a JSON-serializable format.
*
* @return mixed
*/
public function jsonSerialize(): mixed
{
$request = app(RestRequest::class);
return [
'actions' => collect($this->getActions($request))->map->jsonSerialize()->toArray(),
'instructions' => collect($this->getInstructions($request))->map->jsonSerialize()->toArray(),
'scout_instructions' => collect($this->getScoutInstructions($request))->map->jsonSerialize()->toArray(),
'fields' => $this->getFields($request),
'scout_fields' => $this->getScoutFields($request),
'limits' => $this->getLimits($request),
'scopes' => $this->getScopes($request),
'relations' => collect($this->getRelations($request))->map->jsonSerialize()->toArray(),
'rules' => [
'all' => $this->rules($request),
'create' => $this->createRules($request),
'update' => $this->updateRules($request),
],
];
}
}