|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace allejo\stakx\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use allejo\stakx\Document\DynamicPageView; |
|
6
|
|
|
use allejo\stakx\Document\RepeaterPageView; |
|
7
|
|
|
use allejo\stakx\Document\StaticPageView; |
|
8
|
|
|
use allejo\stakx\Event\PageViewAdded; |
|
9
|
|
|
use allejo\stakx\RedirectMapper; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
11
|
|
|
|
|
12
|
|
|
class RedirectSubscriber implements EventSubscriberInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var RedirectMapper */ |
|
15
|
|
|
private $redirectMapper; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(RedirectMapper $redirectMapper) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->redirectMapper = $redirectMapper; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function registerRedirect(PageViewAdded $event) |
|
23
|
|
|
{ |
|
24
|
|
|
$pageView = $event->getPageView(); |
|
25
|
|
|
|
|
26
|
|
|
if ($pageView instanceof StaticPageView || $pageView instanceof DynamicPageView) |
|
27
|
|
|
{ |
|
28
|
|
|
$redirects = $pageView->getRedirects(); |
|
29
|
|
|
|
|
30
|
|
|
foreach ($redirects as $redirect) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->redirectMapper->registerRedirect($redirect, $pageView->getPermalink()); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
elseif ($pageView instanceof RepeaterPageView) |
|
36
|
|
|
{ |
|
37
|
|
|
$permalinks = $pageView->getRepeaterPermalinks(); |
|
38
|
|
|
|
|
39
|
|
|
foreach ($pageView->getRepeaterRedirects() as $repeaterRedirect) |
|
40
|
|
|
{ |
|
41
|
|
|
foreach ($repeaterRedirect as $index => $redirect) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->redirectMapper->registerRedirect( |
|
44
|
|
|
$redirect->getEvaluated(), |
|
45
|
|
|
$permalinks[$index]->getEvaluated() |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function getSubscribedEvents() |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
PageViewAdded::NAME => 'registerRedirect', |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|