1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\SolrSearch\Fluent; |
4
|
|
|
|
5
|
|
|
use Firesphere\SolrSearch\Interfaces\SiteStateInterface; |
6
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
7
|
|
|
use Firesphere\SolrSearch\States\SiteState; |
8
|
|
|
use ReflectionException; |
9
|
|
|
use TractorCow\Fluent\Extension\FluentExtension; |
|
|
|
|
10
|
|
|
use TractorCow\Fluent\Model\Locale; |
11
|
|
|
use TractorCow\Fluent\State\FluentState; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class FluentSiteState |
15
|
|
|
* |
16
|
|
|
* @package Firesphere\SolrSearch\Fluent |
17
|
|
|
*/ |
18
|
|
|
class FluentSiteState extends SiteState implements SiteStateInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Does the state apply to this class |
22
|
|
|
* |
23
|
|
|
* @param $class |
24
|
|
|
* @return bool |
25
|
|
|
* @throws ReflectionException |
26
|
|
|
*/ |
27
|
|
|
public function appliesTo($class): bool |
28
|
|
|
{ |
29
|
|
|
return $this->isEnabled() && |
30
|
|
|
SiteState::hasExtension($class, FluentExtension::class, true) && |
|
|
|
|
31
|
|
|
Locale::getCached()->count(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Is this state applicable to this extension |
36
|
|
|
* |
37
|
|
|
* @param string $state |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public function stateIsApplicable($state): bool |
41
|
|
|
{ |
42
|
|
|
$locales = Locale::get()->column('Locale'); |
43
|
|
|
|
44
|
|
|
return in_array($state, $locales, true); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Reset the SiteState to it's default state |
49
|
|
|
* |
50
|
|
|
* @param string|null $state |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public function setDefaultState($state = null) |
54
|
|
|
{ |
55
|
|
|
FluentState::singleton()->setLocale($state); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return the current state of the site |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function currentState(): string |
64
|
|
|
{ |
65
|
|
|
return FluentState::singleton()->getLocale(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Activate a given state. This should only be done if the state is applicable |
70
|
|
|
* |
71
|
|
|
* @param string $state |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function activateState($state) |
75
|
|
|
{ |
76
|
|
|
FluentState::singleton()->setLocale($state); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param BaseQuery $query |
81
|
|
|
*/ |
82
|
|
|
public function updateQuery(&$query) |
83
|
|
|
{ |
84
|
|
|
$locale = FluentState::singleton()->getLocale(); |
85
|
|
|
|
86
|
|
|
$this->updatePart($query, $locale, 'BoostedFields'); |
87
|
|
|
$this->updatePart($query, $locale, 'Filter'); |
88
|
|
|
$this->updatePart($query, $locale, 'Exclude'); |
89
|
|
|
|
90
|
|
|
$fields = []; |
91
|
|
|
foreach ($query->getFields() as $field) { |
92
|
|
|
$fields[] = $field . '_' . $locale; |
93
|
|
|
} |
94
|
|
|
$query->setFields($fields); |
95
|
|
|
|
96
|
|
|
$localisedTerms = []; |
97
|
|
|
foreach ($query->getTerms() as $term) { |
98
|
|
|
if (count($term['fields'])) { |
99
|
|
|
foreach ($term['fields'] as &$termField) { |
100
|
|
|
$termField .= '_' . $locale; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
$localisedTerms[] = $term; |
104
|
|
|
} |
105
|
|
|
$query->setTerms($localisedTerms); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $query |
110
|
|
|
* @param string $locale |
111
|
|
|
* @param string $method |
112
|
|
|
*/ |
113
|
|
|
protected function updatePart(&$query, string $locale, string $method): void |
114
|
|
|
{ |
115
|
|
|
$new = []; |
116
|
|
|
$getMethod = 'get' . $method; |
117
|
|
|
$setMethod = 'set' . $method; |
118
|
|
|
foreach ($query->$getMethod() as $filterField => $value) { |
119
|
|
|
$fieldName = $filterField . '_' . $locale; |
120
|
|
|
$new[$fieldName] = $value; |
121
|
|
|
} |
122
|
|
|
$query->$setMethod($new); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: