PR CI / ledger (pull_request) Successful in 4s
PR CI / flow (pull_request) Successful in 4s
PR CI / ui (pull_request) Successful in 16s
PR CI / test (lowest) (pull_request) Successful in 44s
PR CI / test (stable) (pull_request) Successful in 49s
PR CI / security (pull_request) Successful in 50s
PR CI / pr-ci (pull_request) Successful in 2s
PR CI / release-automerge (pull_request) Successful in 4s
123 lines
4.8 KiB
PHP
123 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Support;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model as Eloquent;
|
|
use IriksIT\Manager\Models\Record;
|
|
|
|
/**
|
|
* The resources of the site (config `manager.resources`): lists of records the screens make a form for,
|
|
* without code of the site's own. A resource is its fields (see `Fields`) plus where they are kept: in a
|
|
* model of the site (`model`, the field keys are its attributes) or, without one, in the package's own
|
|
* table of records.
|
|
*
|
|
* 'services' => ['label' => 'Services', 'singular' => 'Service', 'model' => App\Models\Service::class,
|
|
* 'columns' => ['name', 'price'], 'order' => ['position', 'asc'], 'fields' => [...]]
|
|
*/
|
|
final class Resources
|
|
{
|
|
/** Names the permissions of modules that exist already would clash with. */
|
|
private const RESERVED = ['posts', 'pages', 'media', 'accounts', 'groups', 'audit', 'statistics', 'reviews', 'labels', 'profile', 'manager', 'records'];
|
|
|
|
/**
|
|
* @return array<string, array{key: string, label: string, singular: string, model: ?string, fields: array<string, array<string, mixed>>, columns: list<string>, order: array{0: string, 1: string}, per_page: int}>
|
|
*/
|
|
public static function all(): array
|
|
{
|
|
$out = [];
|
|
|
|
foreach ((array) config('manager.resources', []) as $key => $definition) {
|
|
if (! is_string($key) || ! preg_match('/^[a-z][a-z0-9_]{0,39}$/', $key) || in_array($key, self::RESERVED, true) || ! is_array($definition)) {
|
|
continue;
|
|
}
|
|
|
|
$fields = Fields::normalize((array) ($definition['fields'] ?? []));
|
|
$model = $definition['model'] ?? null;
|
|
$columns = array_values(array_filter((array) ($definition['columns'] ?? array_slice(array_keys($fields), 0, 3)), fn ($column) => isset($fields[$column])));
|
|
$order = (array) ($definition['order'] ?? []);
|
|
|
|
$out[$key] = [
|
|
'key' => $key,
|
|
'label' => (string) ($definition['label'] ?? ucfirst(str_replace('_', ' ', $key))),
|
|
'singular' => (string) ($definition['singular'] ?? $definition['label'] ?? $key),
|
|
'model' => is_string($model) && is_subclass_of($model, Eloquent::class) ? $model : null,
|
|
'fields' => $fields,
|
|
'columns' => $columns,
|
|
'order' => [preg_match('/^[a-z_][a-z0-9_]*$/', (string) ($order[0] ?? '')) ? (string) $order[0] : 'id', ($order[1] ?? 'desc') === 'asc' ? 'asc' : 'desc'],
|
|
'per_page' => max(5, min(200, (int) ($definition['per_page'] ?? 50))),
|
|
];
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
|
|
/** @return ?array<string, mixed> */
|
|
public static function find(string $key): ?array
|
|
{
|
|
return self::all()[$key] ?? null;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $resource
|
|
* @return Builder<Eloquent>
|
|
*/
|
|
public static function query(array $resource): Builder
|
|
{
|
|
[$column, $direction] = $resource['order'];
|
|
|
|
if ($resource['model'] !== null) {
|
|
return $resource['model']::query()->orderBy($column, $direction)->orderBy('id', 'desc');
|
|
}
|
|
|
|
// The package's own table keeps the values in one JSON column.
|
|
$query = Record::query()->where('resource', $resource['key']);
|
|
|
|
return $column !== 'id' && isset($resource['fields'][$column]) ? $query->orderBy('data->'.$column, $direction) : $query->orderBy('id', $column === 'id' ? $direction : 'desc');
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $resource
|
|
* @return array<string, mixed>
|
|
*/
|
|
public static function row(array $resource, Eloquent $model): array
|
|
{
|
|
$stored = $resource['model'] === null ? (array) $model->getAttribute('data') : null;
|
|
$values = [];
|
|
|
|
foreach ($resource['fields'] as $key => $field) {
|
|
$values[$key] = $stored !== null ? ($stored[$key] ?? $field['default']) : ($model->getAttribute($key) ?? $field['default']);
|
|
}
|
|
|
|
return ['id' => $model->getKey(), 'values' => $values];
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $resource
|
|
* @param array<string, mixed> $values cleaned by `Fields::clean`
|
|
*/
|
|
public static function save(array $resource, ?Eloquent $model, array $values): Eloquent
|
|
{
|
|
if ($resource['model'] === null) {
|
|
$model ??= new Record(['resource' => $resource['key']]);
|
|
$model->data = $values;
|
|
} else {
|
|
$model ??= new $resource['model'];
|
|
// Only the keys the site defined: the definition is the whitelist.
|
|
$model->forceFill($values);
|
|
}
|
|
|
|
$model->save();
|
|
|
|
return $model;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $resource
|
|
*/
|
|
public static function record(array $resource, int|string $id): ?Eloquent
|
|
{
|
|
return self::query($resource)->reorder()->whereKey($id)->first();
|
|
}
|
|
}
|