|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file has been created by developers from BitBag. |
|
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
|
6
|
|
|
* another great project. |
|
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
|
8
|
|
|
* an email on [email protected]. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace BitBag\SyliusCmsPlugin\SitemapProvider; |
|
14
|
|
|
|
|
15
|
|
|
use BitBag\SyliusCmsPlugin\Entity\SectionInterface; |
|
16
|
|
|
use BitBag\SyliusCmsPlugin\Entity\SectionTranslationInterface; |
|
17
|
|
|
use BitBag\SyliusCmsPlugin\Repository\SectionRepositoryInterface; |
|
18
|
|
|
use Doctrine\Common\Collections\Collection; |
|
19
|
|
|
use SitemapPlugin\Factory\SitemapUrlFactoryInterface; |
|
20
|
|
|
use SitemapPlugin\Model\ChangeFrequency; |
|
21
|
|
|
use SitemapPlugin\Model\SitemapUrlInterface; |
|
22
|
|
|
use SitemapPlugin\Provider\UrlProviderInterface; |
|
23
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
|
24
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
25
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
|
26
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
|
27
|
|
|
use Sylius\Component\Resource\Model\TranslationInterface; |
|
28
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
29
|
|
|
|
|
30
|
|
|
final class SectionUrlProvider implements UrlProviderInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var SectionRepositoryInterface */ |
|
33
|
|
|
private $sectionRepository; |
|
34
|
|
|
|
|
35
|
|
|
/** @var RouterInterface */ |
|
36
|
|
|
private $router; |
|
37
|
|
|
|
|
38
|
|
|
/** @var SitemapUrlFactoryInterface */ |
|
39
|
|
|
private $sitemapUrlFactory; |
|
40
|
|
|
|
|
41
|
|
|
/** @var LocaleContextInterface */ |
|
42
|
|
|
private $localeContext; |
|
43
|
|
|
|
|
44
|
|
|
/** @var ChannelContextInterface */ |
|
45
|
|
|
private $channelContext; |
|
46
|
|
|
|
|
47
|
|
|
public function __construct( |
|
48
|
|
|
SectionRepositoryInterface $sectionRepository, |
|
49
|
|
|
RouterInterface $router, |
|
50
|
|
|
SitemapUrlFactoryInterface $sitemapUrlFactory, |
|
51
|
|
|
LocaleContextInterface $localeContext, |
|
52
|
|
|
ChannelContextInterface $channelContext |
|
53
|
|
|
) { |
|
54
|
|
|
$this->sectionRepository = $sectionRepository; |
|
55
|
|
|
$this->router = $router; |
|
56
|
|
|
$this->sitemapUrlFactory = $sitemapUrlFactory; |
|
57
|
|
|
$this->localeContext = $localeContext; |
|
58
|
|
|
$this->channelContext = $channelContext; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getName(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return 'cms_sections'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function generate(): iterable |
|
67
|
|
|
{ |
|
68
|
|
|
$urls = []; |
|
69
|
|
|
|
|
70
|
|
|
foreach ($this->getSections() as $section) { |
|
71
|
|
|
$urls[] = $this->createSectionUrl($section); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $urls; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function getTranslations(SectionInterface $section): Collection |
|
78
|
|
|
{ |
|
79
|
|
|
return $section->getTranslations()->filter(function (TranslationInterface $translation) { |
|
80
|
|
|
return $this->localeInLocaleCodes($translation); |
|
81
|
|
|
}); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function localeInLocaleCodes(TranslationInterface $translation): bool |
|
85
|
|
|
{ |
|
86
|
|
|
return in_array($translation->getLocale(), $this->getLocaleCodes()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function getSections(): iterable |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->sectionRepository->findAll(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function getLocaleCodes(): array |
|
95
|
|
|
{ |
|
96
|
|
|
/** @var ChannelInterface $channel */ |
|
97
|
|
|
$channel = $this->channelContext->getChannel(); |
|
98
|
|
|
|
|
99
|
|
|
return $channel->getLocales()->map(function (LocaleInterface $locale) { |
|
100
|
|
|
return $locale->getCode(); |
|
101
|
|
|
})->toArray(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function createSectionUrl(SectionInterface $section): SitemapUrlInterface |
|
105
|
|
|
{ |
|
106
|
|
|
$url = $this->sitemapUrlFactory->createNew(); |
|
107
|
|
|
|
|
108
|
|
|
$url->setChangeFrequency(ChangeFrequency::daily()); |
|
109
|
|
|
$url->setPriority(0.7); |
|
110
|
|
|
|
|
111
|
|
|
if ($section->getUpdatedAt()) { |
|
112
|
|
|
$url->setLastModification($section->getUpdatedAt()); |
|
113
|
|
|
} elseif ($section->getCreatedAt()) { |
|
114
|
|
|
$url->setLastModification($section->getCreatedAt()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** @var SectionTranslationInterface $translation */ |
|
118
|
|
|
foreach ($this->getTranslations($section) as $translation) { |
|
119
|
|
|
if (!$translation->getLocale() || !$this->localeInLocaleCodes($translation)) { |
|
120
|
|
|
continue; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$location = $this->router->generate('bitbag_sylius_cms_plugin_shop_section_show', [ |
|
124
|
|
|
'slug' => $section->getCode(), |
|
125
|
|
|
'_locale' => $translation->getLocale(), |
|
126
|
|
|
]); |
|
127
|
|
|
|
|
128
|
|
|
if ($translation->getLocale() === $this->localeContext->getLocaleCode()) { |
|
129
|
|
|
$url->setLocalization($location); |
|
130
|
|
|
|
|
131
|
|
|
continue; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$url->addAlternative($location, $translation->getLocale()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $url; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|