Files
kupshop/bundles/KupShop/ContentBundle/Util/RecursiveTemplateFinderTrait.php
2025-08-02 16:30:27 +02:00

31 lines
1000 B
PHP

<?php
declare(strict_types=1);
namespace KupShop\ContentBundle\Util;
trait RecursiveTemplateFinderTrait
{
public function getTemplatesFromPath(string $path, string $templateName, string $folderName): iterable
{
// List all shop templates in target path
if (is_dir($path)) {
yield from $this->yieldTemplates($path, $templateName, $folderName);
}
}
private function yieldTemplates(string $path, string $templateName, string $folderName): iterable
{
foreach (new \FilesystemIterator($path) as $file) {
if (is_dir($file->getPathname())) {
yield from $this->yieldTemplates($file->getPathname(), $templateName, $folderName);
} else {
if ($file->getFilename() == $templateName) {
preg_match("#{$folderName}/[^/]+#", $file->getPathname(), $matches);
yield $matches[0].'/'.$file->getFilename();
}
}
}
}
}