1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Language\LanguageManagerInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Simple service that stores the current GraphQL language state. |
9
|
|
|
*/ |
10
|
|
|
class GraphQLLanguageContext { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Indicates if the GraphQL context language is currently active. |
14
|
|
|
* |
15
|
|
|
* @var bool |
16
|
|
|
*/ |
17
|
|
|
protected $isActive; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The current language context. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $currentLanguage; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The language manager service. |
28
|
|
|
* |
29
|
|
|
* @var \Drupal\Core\Language\LanguageManagerInterface |
30
|
|
|
*/ |
31
|
|
|
protected $languageManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* GraphQLLanguageContext constructor. |
35
|
|
|
* |
36
|
|
|
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager |
37
|
|
|
* The language manager service. |
38
|
|
|
*/ |
39
|
|
|
public function __construct(LanguageManagerInterface $languageManager) { |
40
|
|
|
$this->languageManager = $languageManager; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set the current context language. |
45
|
|
|
* |
46
|
|
|
* @param string $langcode |
47
|
|
|
* The language to be set. |
48
|
|
|
*/ |
49
|
|
|
protected function setCurrentLanguage($langcode) { |
50
|
|
|
$this->currentLanguage = $langcode; |
51
|
|
|
$this->isActive = TRUE; |
52
|
|
|
$this->languageManager->reset(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Retrieve the current language. |
57
|
|
|
* |
58
|
|
|
* @return string|null |
59
|
|
|
* The current language code, or null if the context is not active. |
60
|
|
|
*/ |
61
|
|
|
public function getCurrentLanguage() { |
62
|
|
|
return $this->isActive |
63
|
|
|
? ($this->currentLanguage ?: $this->languageManager->getDefaultLanguage()->getId()) |
64
|
|
|
: NULL; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Executes a callable in a defined language context. |
69
|
|
|
* |
70
|
|
|
* @param callable $callable |
71
|
|
|
* The callable to be executed. |
72
|
|
|
* @param string $language |
73
|
|
|
* The langcode to be set. |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
* The callables result. |
77
|
|
|
* |
78
|
|
|
* @throws \Exception |
79
|
|
|
* Any exception caught while executing the callable. |
80
|
|
|
*/ |
81
|
|
|
public function executeInLanguageContext(callable $callable, $language) { |
82
|
|
|
$this->setCurrentLanguage($language); |
83
|
|
|
// Extract the result array. |
84
|
|
|
try { |
85
|
|
|
return $callable(); |
86
|
|
|
} |
87
|
|
|
catch (\Exception $exc) { |
88
|
|
|
throw $exc; |
89
|
|
|
} |
90
|
|
|
finally { |
91
|
|
|
// In any case, set the language context back to null. |
92
|
|
|
$this->reset(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Reset the context.. |
98
|
|
|
*/ |
99
|
|
|
public function reset() { |
100
|
|
|
$this->currentLanguage = NULL; |
101
|
|
|
$this->isActive = FALSE; |
102
|
|
|
$this->languageManager->reset(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|