1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MOC\NotFound\Fusion\Eel\Helper; |
4
|
|
|
|
5
|
|
|
use Neos\Flow\Annotations as Flow; |
6
|
|
|
use Neos\Eel\ProtectedContextAwareInterface; |
7
|
|
|
|
8
|
|
|
class ContextHelper implements ProtectedContextAwareInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @Flow\InjectConfiguration(path="contentDimensions", package="Neos.ContentRepository") |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $contentDimensionsConfiguration; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Returns a context array with matched dimension values per dimension for given request uri path. If nothing |
18
|
|
|
* matches, it returns a context array with default dimension values per dimension. |
19
|
|
|
* |
20
|
|
|
* @param $requestUriPath |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
|
|
public function ofRequestUriPath($requestUriPath) |
24
|
|
|
{ |
25
|
|
|
// No dimensions configured, context is empty |
26
|
|
|
if (count($this->contentDimensionsConfiguration) === 0) { |
27
|
|
|
return []; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$uriSegments = $this->getUriSegments($requestUriPath); |
31
|
|
|
$dimensionValues = $this->getDimensionValuesForUriSegments($uriSegments); |
32
|
|
|
if (!$dimensionValues) { |
|
|
|
|
33
|
|
|
$dimensionValues = $this->getDefaultDimensionValues(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$targetDimensionValues = array_map(function ($dimensionValues) { |
37
|
|
|
return reset($dimensionValues); // Default target dimension value is first dimension value |
38
|
|
|
}, $dimensionValues); |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
return [ |
42
|
|
|
'dimensions' => $dimensionValues, |
43
|
|
|
'targetDimensions' => $targetDimensionValues |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $uriSegments |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
protected function getDimensionValuesForUriSegments($uriSegments) |
52
|
|
|
{ |
53
|
|
|
if (count($uriSegments) !== count($this->contentDimensionsConfiguration)) { |
54
|
|
|
return []; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$index = 0; |
58
|
|
|
$dimensionValues = []; |
59
|
|
|
foreach ($this->contentDimensionsConfiguration as $dimensionName => $dimensionConfiguration) { |
60
|
|
|
$uriSegment = $uriSegments[$index++]; |
61
|
|
|
foreach ($dimensionConfiguration['presets'] as $preset) { |
62
|
|
|
if ($uriSegment === $preset['uriSegment']) { |
63
|
|
|
$dimensionValues[$dimensionName] = $preset['values']; |
64
|
|
|
continue 2; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (count($uriSegments) !== count($dimensionValues)) { |
70
|
|
|
return []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $dimensionValues; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns default dimension values per dimension. |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
protected function getDefaultDimensionValues() |
82
|
|
|
{ |
83
|
|
|
$dimensionValues = []; |
84
|
|
|
foreach ($this->contentDimensionsConfiguration as $dimensionName => $dimensionConfiguration) { |
85
|
|
|
$dimensionValues[$dimensionName] = [$dimensionConfiguration['default']]; |
86
|
|
|
} |
87
|
|
|
return $dimensionValues; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function getUriSegments($requestUriPath) |
91
|
|
|
{ |
92
|
|
|
$pathParts = explode('/', trim($requestUriPath, '/'), 2); |
93
|
|
|
return explode('_', $pathParts[0]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
function allowsCallOfMethod($methodName) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.