<?php declare(strict_types=1);
namespace App\Base\Sitemap;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Service\UrlContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
abstract class Sitemap implements EventSubscriberInterface
{
public function __construct(
protected UrlGeneratorInterface $urlGenerator,
protected EntityManagerInterface $entityManager
) {
}
/**
* Получить репозиторий по классу
*/
protected function getRepository(string $className): ObjectRepository
{
return $this->entityManager->getRepository($className);
}
/**
* Получить все объекты класса с возможностью фильтрации
*/
protected function findAll(string $className, array $criteria = []): array
{
return $this->getRepository($className)->findBy($criteria);
}
protected function url(string $name, array $parameters = []): string
{
return $this->urlGenerator->generate(
$name,
$parameters,
UrlGeneratorInterface::ABSOLUTE_URL
);
}
/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
SitemapPopulateEvent::ON_SITEMAP_POPULATE => 'populate',
];
}
public function populate(SitemapPopulateEvent $event): void
{
$this->registerRoutes($event->getUrlContainer());
}
abstract public function registerRoutes(UrlContainerInterface $urls): void;
}