['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>, columns: list, 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; } // A resource of a feature that is switched off is not there: no menu item, no permissions, no screens. if (isset($definition['feature']) && ! Features::on((string) $definition['feature'])) { 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))), 'hint' => isset($definition['hint']) ? (string) $definition['hint'] : null, 'singular' => (string) ($definition['singular'] ?? $definition['label'] ?? $key), 'simple_label' => Fields::simpleText($definition, 'label'), 'simple_singular' => Fields::simpleText($definition, 'singular'), 'model' => is_string($model) && is_subclass_of($model, Eloquent::class) ? $model : null, 'fields' => $fields, // A page layout for the page of one record: `true` for every block type, or a list of the types it may use. 'template' => ($definition['template'] ?? false) === true ? true : (is_array($definition['template'] ?? null) ? array_values(array_filter($definition['template'], 'is_string')) : false), '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 */ public static function find(string $key): ?array { return self::all()[$key] ?? null; } /** * @param array $resource * @return Builder */ 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 $resource * @return array */ 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 $resource * @param array $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 $resource */ public static function record(array $resource, int|string $id): ?Eloquent { return self::query($resource)->reorder()->whereKey($id)->first(); } }