Files
laravel-manager-packagist/src/Support/Features.php
T
2026-12-04 14:33:04 +00:00

57 lines
2.5 KiB
PHP

<?php
namespace IriksIT\Manager\Support;
/**
* The features of a site that can be switched on and off from the settings screen (config `manager.features`): services, projects,
* reviews, requests, a contact form. A feature is one switch that the whole site follows:
*
* 'features' => ['services' => ['label' => 'Services', 'enabled' => true, 'env' => 'SITE_SERVICES', 'hint' => 'The page with what you offer']]
*
* - the manager: a `resources` entry with `'feature' => 'services'` is left out when it is off (menu, permissions, screens, routes),
* - the public site: `Features::on('services')`, the route middleware `manager.feature:services` (a 404 when off), the Blade
* condition `@feature('services') … @endfeature`, and `Features::flags()` for what a page needs to know (links, sitemap).
*
* Off hides, it does not delete: the data stays where it is and comes back when the switch goes on again. The value lives at
* `manager.features.<key>.enabled`, which is also the path the settings screen changes.
*/
final class Features
{
/** @return array<string, array{key: string, label: string, hint: ?string, enabled: bool, env: ?string}> */
public static function all(): array
{
$out = [];
foreach ((array) config('manager.features', []) as $key => $definition) {
if (! is_string($key) || preg_match('/^[a-z][a-z0-9_]{0,39}$/', $key) !== 1) {
continue;
}
$definition = is_array($definition) ? $definition : ['enabled' => (bool) $definition];
$env = $definition['env'] ?? null;
$out[$key] = [
'key' => $key,
'label' => (string) ($definition['label'] ?? ucfirst(str_replace('_', ' ', $key))),
'hint' => isset($definition['hint']) ? (string) $definition['hint'] : null,
'enabled' => filter_var($definition['enabled'] ?? false, FILTER_VALIDATE_BOOLEAN),
'env' => is_string($env) && preg_match('/^[A-Z][A-Z0-9_]*$/', $env) === 1 ? $env : null,
];
}
return $out;
}
/** Whether a feature is on. A name that is not a feature is off: nothing shows up that nobody declared. */
public static function on(string $key): bool
{
return (self::all()[$key]['enabled'] ?? false) === true;
}
/** @return array<string, bool> every feature and whether it is on, to hand to a page */
public static function flags(): array
{
return array_map(fn (array $feature) => $feature['enabled'], self::all());
}
}