37 lines
911 B
PHP
37 lines
911 B
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Support;
|
|
|
|
use Closure;
|
|
|
|
/**
|
|
* Where media is used. A module or the host app registers a source: a closure that returns the
|
|
* texts which may contain the address of a media file (a post body, a cover, a page text).
|
|
* The media library counts the files whose address appears in them. No sources, no counts.
|
|
*/
|
|
final class MediaUsage
|
|
{
|
|
/** @var list<Closure(): iterable<mixed>> */
|
|
private array $sources = [];
|
|
|
|
/** @param Closure(): iterable<mixed> $source */
|
|
public function register(Closure $source): void
|
|
{
|
|
$this->sources[] = $source;
|
|
}
|
|
|
|
/** @return list<string> */
|
|
public function haystacks(): array
|
|
{
|
|
$texts = [];
|
|
|
|
foreach ($this->sources as $source) {
|
|
foreach ($source() as $text) {
|
|
$texts[] = is_string($text) ? $text : '';
|
|
}
|
|
}
|
|
|
|
return $texts;
|
|
}
|
|
}
|