<?php
namespace App\EventListener;
use App\Repository\VehiculeRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Service\UrlContainerInterface;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
class SitemapSubscriber implements EventSubscriberInterface
{
/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;
/**
* @var VehiculeRepository
*/
private $VehiculeRepository;
/**
* @param UrlGeneratorInterface $urlGenerator
* @param VehiculeRepository $VehiculeRepository
*/
public function __construct(UrlGeneratorInterface $urlGenerator, VehiculeRepository $VehiculeRepository)
{
$this->urlGenerator = $urlGenerator;
$this->VehiculeRepository = $VehiculeRepository;
}
/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
SitemapPopulateEvent::ON_SITEMAP_POPULATE => 'populate',
];
}
/**
* @param SitemapPopulateEvent $event
*/
public function populate(SitemapPopulateEvent $event): void
{
$this->registerBlogPostsUrls($event->getUrlContainer());
}
/**
* @param UrlContainerInterface $urls
*/
public function registerBlogPostsUrls(UrlContainerInterface $urls): void
{
$posts = $this->VehiculeRepository->findAll();
foreach ($posts as $post) {
$urls->addUrl(
new UrlConcrete(
$this->urlGenerator->generate(
'fiche_produit',
['slug' => $post->getUrl()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'fiche_produit'
);
}
}
}