-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMap.php
138 lines (116 loc) · 2.9 KB
/
Map.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\Core\MVC\Symfony\SiteAccess\Matcher;
use Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest;
use Ibexa\Core\MVC\Symfony\SiteAccess\VersatileMatcher;
abstract class Map implements VersatileMatcher
{
/**
* String that will be looked up in the map.
*
* @var string
*/
protected $key;
/**
* Map used for the matching.
*
* @var array
*/
protected $map = [];
/**
* Map used for reverse matching.
*
* @var array
*/
protected $reverseMap = [];
/** @var \Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest */
protected $request;
/**
* Constructor.
*
* @param array $map Map used for matching.
*/
public function __construct(array $map)
{
$this->map = $map;
}
/**
* Do not serialize the Siteaccess configuration in order to reduce ESI request URL size.
*
* @see https://issues.ibexa.co/browse/EZP-23168
*
* @return array
*/
public function __sleep()
{
$this->map = [];
$this->reverseMap = [];
return ['map', 'reverseMap', 'key'];
}
public function setRequest(SimplifiedRequest $request)
{
$this->request = $request;
}
public function getRequest()
{
return $this->request;
}
/**
* Injects the key that will be used for matching against the map configuration.
*
* @param string $key
*/
public function setMapKey($key)
{
$this->key = $key;
}
/**
* @return string
*/
public function getMapKey()
{
return $this->key;
}
/**
* Returns matching Siteaccess.
*
* @return string|false Siteaccess matched or false.
*/
public function match()
{
return isset($this->map[$this->key])
? $this->map[$this->key]
: false;
}
/**
* @param string $siteAccessName
*
* @return \Ibexa\Core\MVC\Symfony\SiteAccess\Matcher|Map|null
*/
public function reverseMatch($siteAccessName)
{
$reverseMap = $this->getReverseMap($siteAccessName);
if (!isset($reverseMap[$siteAccessName])) {
return null;
}
$this->setMapKey($reverseMap[$siteAccessName]);
return $this;
}
private function getReverseMap($defaultSiteAccess)
{
if (!empty($this->reverseMap)) {
return $this->reverseMap;
}
$map = $this->map;
foreach ($map as &$value) {
// $value can be true in the case of the use of a Compound matcher
if ($value === true) {
$value = $defaultSiteAccess;
}
}
return $this->reverseMap = array_flip($map);
}
}