1 | <?php |
||
2 | |||
3 | namespace Drupal\graphql; |
||
4 | |||
5 | use Drupal\Core\Language\LanguageDefault; |
||
0 ignored issues
–
show
|
|||
6 | use Drupal\Core\Language\LanguageManagerInterface; |
||
7 | use Drupal\Core\StringTranslation\TranslationManager; |
||
0 ignored issues
–
show
The type
Drupal\Core\StringTranslation\TranslationManager was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
8 | |||
9 | /** |
||
10 | * Simple service that stores the current GraphQL language state. |
||
11 | */ |
||
12 | class GraphQLLanguageContext { |
||
13 | |||
14 | /** |
||
15 | * Indicates if the GraphQL context language is currently active. |
||
16 | * |
||
17 | * @var bool |
||
18 | */ |
||
19 | protected $isActive; |
||
20 | |||
21 | /** |
||
22 | * The current language context. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $currentLanguage; |
||
27 | |||
28 | /** |
||
29 | * @var \SplStack |
||
30 | */ |
||
31 | protected $languageStack; |
||
32 | |||
33 | /** |
||
34 | * The language manager service. |
||
35 | * |
||
36 | * @var \Drupal\Core\Language\LanguageManagerInterface |
||
37 | */ |
||
38 | protected $languageManager; |
||
39 | |||
40 | /** |
||
41 | * The string translation service |
||
42 | * |
||
43 | * @var \Drupal\Core\StringTranslation\TranslationManager |
||
44 | */ |
||
45 | protected $translationManager; |
||
46 | |||
47 | /** |
||
48 | * GraphQLLanguageContext constructor. |
||
49 | * |
||
50 | * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager |
||
51 | * The language manager service. |
||
52 | */ |
||
53 | public function __construct(LanguageManagerInterface $languageManager, TranslationManager $translationManager) { |
||
54 | $this->languageManager = $languageManager; |
||
55 | $this->translationManager = $translationManager; |
||
56 | $this->languageStack = new \SplStack(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Retrieve the current language. |
||
61 | * |
||
62 | * @return string|null |
||
63 | * The current language code, or null if the context is not active. |
||
64 | */ |
||
65 | public function getCurrentLanguage() { |
||
66 | return $this->isActive |
||
67 | ? ($this->currentLanguage ?: $this->languageManager->getDefaultLanguage()->getId()) |
||
68 | : NULL; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Executes a callable in a defined language context. |
||
73 | * |
||
74 | * @param callable $callable |
||
75 | * The callable to be executed. |
||
76 | * @param string $language |
||
77 | * The langcode to be set. |
||
78 | * |
||
79 | * @return mixed |
||
80 | * The callables result. |
||
81 | * |
||
82 | * @throws \Exception |
||
83 | * Any exception caught while executing the callable. |
||
84 | */ |
||
85 | public function executeInLanguageContext(callable $callable, $language) { |
||
86 | $this->languageStack->push($this->currentLanguage); |
||
87 | $this->currentLanguage = $language; |
||
88 | $this->isActive = TRUE; |
||
89 | $this->languageManager->reset(); |
||
90 | // This is needed to be able to use the string translation with the |
||
91 | // requested language. |
||
92 | $this->translationManager->setDefaultLangcode($language); |
||
93 | // Override the configuration language so that config entities (like menus) |
||
94 | // are loaded using the proper translation. |
||
95 | $currentConfigLanguage = $this->languageManager->getConfigOverrideLanguage(); |
||
96 | if ($currentConfigLanguage->getId() !== $language) { |
||
97 | $configLanguage = $this->languageManager->getLanguage($language); |
||
98 | $this->languageManager->setConfigOverrideLanguage($configLanguage); |
||
99 | } |
||
100 | // Extract the result array. |
||
101 | try { |
||
102 | return call_user_func($callable); |
||
103 | } |
||
104 | catch (\Exception $exc) { |
||
105 | throw $exc; |
||
106 | } |
||
107 | finally { |
||
108 | // In any case, set the language context back to null. |
||
109 | $this->currentLanguage = $this->languageStack->pop(); |
||
110 | $this->isActive = FALSE; |
||
111 | $this->languageManager->reset(); |
||
112 | // Restore the languages for the translation and language managers. |
||
113 | $defaultLangcode = !empty($this->currentLanguage) |
||
114 | ? $this->currentLanguage |
||
115 | : $this->languageManager->getDefaultLanguage()->getId(); |
||
116 | $this->translationManager->setDefaultLangcode($defaultLangcode); |
||
117 | $this->languageManager->setConfigOverrideLanguage($currentConfigLanguage); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | } |
||
122 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths