1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitBag\SyliusCmsPlugin\Sitemap\Provider; |
6
|
|
|
|
7
|
|
|
use BitBag\SyliusCmsPlugin\Entity\PageInterface; |
8
|
|
|
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface; |
9
|
|
|
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface; |
10
|
|
|
use Doctrine\Common\Collections\Collection; |
11
|
|
|
use SitemapPlugin\Factory\SitemapUrlFactoryInterface; |
12
|
|
|
use SitemapPlugin\Model\ChangeFrequency; |
13
|
|
|
use SitemapPlugin\Model\SitemapUrlInterface; |
14
|
|
|
use SitemapPlugin\Provider\UrlProviderInterface; |
15
|
|
|
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; |
16
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
17
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
18
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
19
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
20
|
|
|
use Sylius\Component\Resource\Model\TranslationInterface; |
21
|
|
|
use Symfony\Component\Routing\RouterInterface; |
22
|
|
|
|
23
|
|
|
class PageUrlProvider implements UrlProviderInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var PageRepositoryInterface|EntityRepository |
27
|
|
|
*/ |
28
|
|
|
private $pageRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RouterInterface |
32
|
|
|
*/ |
33
|
|
|
private $router; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var SitemapUrlFactoryInterface |
37
|
|
|
*/ |
38
|
|
|
private $sitemapUrlFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var LocaleContextInterface |
42
|
|
|
*/ |
43
|
|
|
private $localeContext; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ChannelContextInterface |
47
|
|
|
*/ |
48
|
|
|
private $channelContext; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $urls = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $channelLocaleCodes; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param PageRepositoryInterface $pageRepository |
62
|
|
|
* @param RouterInterface $router |
63
|
|
|
* @param SitemapUrlFactoryInterface $sitemapUrlFactory |
64
|
|
|
* @param LocaleContextInterface $localeContext |
65
|
|
|
* @param ChannelContextInterface $channelContext |
66
|
|
|
*/ |
67
|
|
|
public function __construct( |
68
|
|
|
PageRepositoryInterface $pageRepository, |
69
|
|
|
RouterInterface $router, |
70
|
|
|
SitemapUrlFactoryInterface $sitemapUrlFactory, |
71
|
|
|
LocaleContextInterface $localeContext, |
72
|
|
|
ChannelContextInterface $channelContext |
73
|
|
|
) { |
74
|
|
|
$this->pageRepository = $pageRepository; |
75
|
|
|
$this->router = $router; |
76
|
|
|
$this->sitemapUrlFactory = $sitemapUrlFactory; |
77
|
|
|
$this->localeContext = $localeContext; |
78
|
|
|
$this->channelContext = $channelContext; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getName(): string |
85
|
|
|
{ |
86
|
|
|
return 'cms_pages'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function generate(): iterable |
93
|
|
|
{ |
94
|
|
|
foreach ($this->getPages() as $product) { |
95
|
|
|
$this->urls[] = $this->createPageUrl($product); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->urls; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param PageInterface $page |
103
|
|
|
* |
104
|
|
|
* @return Collection|PageTranslationInterface[] |
105
|
|
|
*/ |
106
|
|
|
private function getTranslations(PageInterface $page): Collection |
107
|
|
|
{ |
108
|
|
|
return $page->getTranslations()->filter(function (TranslationInterface $translation) { |
109
|
|
|
return $this->localeInLocaleCodes($translation); |
110
|
|
|
}); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param TranslationInterface $translation |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
private function localeInLocaleCodes(TranslationInterface $translation): bool |
119
|
|
|
{ |
120
|
|
|
return in_array($translation->getLocale(), $this->getLocaleCodes()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return array|PageInterface[] |
125
|
|
|
*/ |
126
|
|
|
private function getPages(): iterable |
127
|
|
|
{ |
128
|
|
|
return $this->pageRepository->findByEnabled(true); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
private function getLocaleCodes(): array |
135
|
|
|
{ |
136
|
|
|
if (null === $this->channelLocaleCodes) { |
137
|
|
|
/** @var ChannelInterface $channel */ |
138
|
|
|
$channel = $this->channelContext->getChannel(); |
139
|
|
|
$this->channelLocaleCodes = $channel->getLocales()->map(function (LocaleInterface $locale) { |
140
|
|
|
return $locale->getCode(); |
141
|
|
|
})->toArray(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->channelLocaleCodes; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param PageInterface $page |
149
|
|
|
* |
150
|
|
|
* @return SitemapUrlInterface |
151
|
|
|
*/ |
152
|
|
|
private function createPageUrl(PageInterface $page): SitemapUrlInterface |
153
|
|
|
{ |
154
|
|
|
$pageUrl = $this->sitemapUrlFactory->createNew(); |
155
|
|
|
$pageUrl->setChangeFrequency(ChangeFrequency::daily()); |
156
|
|
|
$pageUrl->setPriority(0.7); |
157
|
|
|
if ($page->getUpdatedAt()) { |
158
|
|
|
$pageUrl->setLastModification($page->getUpdatedAt()); |
159
|
|
|
} elseif ($page->getCreatedAt()) { |
160
|
|
|
$pageUrl->setLastModification($page->getCreatedAt()); |
161
|
|
|
} |
162
|
|
|
/** @var PageTranslationInterface $translation */ |
163
|
|
|
foreach ($this->getTranslations($page) as $translation) { |
164
|
|
|
if (!$translation->getLocale()) { |
165
|
|
|
continue; |
166
|
|
|
} |
167
|
|
|
if (!$this->localeInLocaleCodes($translation)) { |
168
|
|
|
continue; |
169
|
|
|
} |
170
|
|
|
$location = $this->router->generate('bitbag_sylius_cms_plugin_shop_page_show', [ |
171
|
|
|
'slug' => $translation->getSlug(), |
172
|
|
|
'_locale' => $translation->getLocale(), |
173
|
|
|
]); |
174
|
|
|
if ($translation->getLocale() === $this->localeContext->getLocaleCode()) { |
175
|
|
|
$pageUrl->setLocalization($location); |
176
|
|
|
|
177
|
|
|
continue; |
178
|
|
|
} |
179
|
|
|
$pageUrl->addAlternative($location, $translation->getLocale()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $pageUrl; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|