43 lines
1.3 KiB
PHP
43 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Modules\Statistics;
|
|
|
|
use Closure;
|
|
|
|
/**
|
|
* What a page is called on the statistics and numbers screens, when it has no title of its own (a post has). The address is the fallback.
|
|
* A site names its pages in `manager.statistics.names` (`'/over-ons' => 'About us'`), and a kind of page with many of them (a service,
|
|
* a project) with a resolver: `app(PageNames::class)->register(fn (string $path) => ...)` returns the name, or null for "not mine".
|
|
*/
|
|
final class PageNames
|
|
{
|
|
/** @var list<Closure(string): ?string> */
|
|
private array $resolvers = [];
|
|
|
|
/** @param Closure(string): ?string $resolver gets an address like `/over-ons` */
|
|
public function register(Closure $resolver): void
|
|
{
|
|
$this->resolvers[] = $resolver;
|
|
}
|
|
|
|
public function name(string $path): ?string
|
|
{
|
|
$names = (array) config('manager.statistics.names', []);
|
|
$name = $names[$path] ?? $names['/'.trim($path, '/')] ?? null;
|
|
|
|
if (is_string($name) && $name !== '') {
|
|
return $name;
|
|
}
|
|
|
|
foreach ($this->resolvers as $resolver) {
|
|
$name = $resolver($path);
|
|
|
|
if (is_string($name) && $name !== '') {
|
|
return $name;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|