|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Listener; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Presta\SitemapBundle\Event\SitemapPopulateEvent; |
|
7
|
|
|
use Presta\SitemapBundle\Service\SitemapListenerInterface; |
|
8
|
|
|
use Presta\SitemapBundle\Sitemap\Url\GoogleMultilangUrlDecorator; |
|
9
|
|
|
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete; |
|
10
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
11
|
|
|
|
|
12
|
|
|
class SitemapListener implements SitemapListenerInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private $entityManager; |
|
15
|
|
|
private $router; |
|
16
|
|
|
private $defaultLocale; |
|
17
|
|
|
private $locales; |
|
18
|
|
|
private $baseUrl; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(RouterInterface $router, EntityManager $entityManager, $defaultLocale, $locales, $baseUrl) |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$this->entityManager = $entityManager; |
|
23
|
|
|
$this->router = $router; |
|
24
|
|
|
$this->defaultLocale = $defaultLocale; |
|
25
|
|
|
$this->locales = $locales; |
|
26
|
|
|
$this->baseUrl = $baseUrl; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function populateSitemap(SitemapPopulateEvent $event) |
|
30
|
|
|
{ |
|
31
|
|
|
$section = $event->getSection(); |
|
32
|
|
|
|
|
33
|
|
|
if (is_null($section) || $section == 'cms') { |
|
34
|
|
|
$nodeRepository = $this->entityManager->getRepository('AlpixelCMSBundle:Node'); |
|
35
|
|
|
$pages = $nodeRepository->findAllWithLocale($this->defaultLocale); |
|
36
|
|
|
|
|
37
|
|
|
foreach ($pages as $page) { |
|
38
|
|
|
$url = $this->router->generate('alpixel_cms', [ |
|
39
|
|
|
'slug' => $page->getSlug(), |
|
40
|
|
|
'_locale' => $page->getLocale(), |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
$url = new UrlConcrete( |
|
44
|
|
|
$url, |
|
45
|
|
|
$page->getDateUpdated(), |
|
46
|
|
|
UrlConcrete::CHANGEFREQ_MONTHLY, |
|
47
|
|
|
.7 |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$urlLang = new GoogleMultilangUrlDecorator($url); |
|
51
|
|
|
foreach ($this->locales as $locale) { |
|
52
|
|
|
if ($locale !== $this->defaultLocale) { |
|
53
|
|
|
$translatedPage = $nodeRepository->findTranslation($page, $locale); |
|
54
|
|
|
|
|
55
|
|
|
if($translatedPage !== null) { |
|
56
|
|
|
$url = $this->router->generate('alpixel_cms', [ |
|
57
|
|
|
'slug' => $translatedPage->getSlug(), |
|
58
|
|
|
'_locale' => $translatedPage->getLocale(), |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
|
|
$urlLang->addLink($url, $locale); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$event->getGenerator()->addUrl( |
|
67
|
|
|
$urlLang, |
|
68
|
|
|
'cms' |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.