1
|
|
|
<?php |
2
|
|
|
namespace MOC\Redirects\Routing; |
3
|
|
|
|
4
|
|
|
use Doctrine\ORM\QueryBuilder; |
5
|
|
|
use Neos\Flow\Annotations as Flow; |
6
|
|
|
use Neos\Flow\Core\Bootstrap; |
7
|
|
|
use Neos\Flow\Http\Uri; |
8
|
|
|
use Neos\Flow\Mvc\Routing\DynamicRoutePart; |
9
|
|
|
use Neos\ContentRepository\Domain\Model\NodeData; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A route part handler for finding nodes specifically in the website's frontend. |
13
|
|
|
*/ |
14
|
|
|
class RedirectFrontendNodeRoutePartHandler extends DynamicRoutePart |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @Flow\Inject |
19
|
|
|
* @var \Doctrine\Common\Persistence\ObjectManager |
20
|
|
|
*/ |
21
|
|
|
protected $entityManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @Flow\Inject |
25
|
|
|
* @var Bootstrap |
26
|
|
|
*/ |
27
|
|
|
protected $bootstrap; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $requestPath The request path to be matched |
31
|
|
|
* @return string value to match |
32
|
|
|
*/ |
33
|
|
|
protected function findValueToMatch($requestPath) |
34
|
|
|
{ |
35
|
|
|
return $requestPath; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $requestPath |
40
|
|
|
* @return boolean TRUE if the $requestPath could be matched, otherwise FALSE |
41
|
|
|
*/ |
42
|
|
|
protected function matchValue($requestPath) |
43
|
|
|
{ |
44
|
|
|
/** @var Uri $uri */ |
45
|
|
|
$uri = $this->bootstrap->getActiveRequestHandler()->getHttpRequest()->getUri(); |
|
|
|
|
46
|
|
|
$relativeUrl = rtrim($uri->getPath(), '/'); |
47
|
|
|
$relativeUrlWithQueryString = $relativeUrl . ($uri->getQuery() ? '?' . $uri->getQuery() : ''); |
48
|
|
|
$absoluteUrl = $uri->getHost() . $relativeUrl; |
49
|
|
|
$absoluteUrlWithQueryString = $uri->getHost() . $relativeUrlWithQueryString; |
50
|
|
|
|
51
|
|
|
if (empty($relativeUrl)) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** @var QueryBuilder $queryBuilder */ |
56
|
|
|
$queryBuilder = $this->entityManager->createQueryBuilder(); |
|
|
|
|
57
|
|
|
$queryBuilder->select('n') |
58
|
|
|
->distinct() |
59
|
|
|
->from('Neos\ContentRepository\Domain\Model\NodeData', 'n') |
60
|
|
|
->where('n.workspace = :workspace') |
61
|
|
|
->setParameter('workspace', 'live') |
62
|
|
|
->andWhere('n.properties LIKE :relativeUrl') |
63
|
|
|
->setParameter('relativeUrl', '%"redirectUrl"%' . str_replace('/', '\\\\\\/', ltrim($relativeUrl, '/')) . '%'); |
64
|
|
|
|
65
|
|
|
$query = $queryBuilder->getQuery(); |
66
|
|
|
$nodes = $query->getResult(); |
67
|
|
|
if (empty($nodes)) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($nodes as $node) { |
72
|
|
|
/** @var NodeData $node */ |
73
|
|
|
// Prevent partial matches |
74
|
|
|
$redirectUrl = trim(preg_replace('#^https?://#', '', $node->getProperty('redirectUrl')), '/'); |
75
|
|
|
if (in_array($redirectUrl, [$relativeUrl, $relativeUrlWithQueryString, $absoluteUrl, $absoluteUrlWithQueryString], true)) { |
76
|
|
|
$matchingNode = $node; |
77
|
|
|
break; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (!isset($matchingNode)) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->setName('node'); |
86
|
|
|
$this->value = $matchingNode->getPath(); |
87
|
|
|
|
88
|
|
|
return true; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $value value to resolve |
93
|
|
|
* @return boolean TRUE if value could be resolved successfully, otherwise FALSE. |
94
|
|
|
*/ |
95
|
|
|
protected function resolveValue($value) |
96
|
|
|
{ |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: