8
8
9
9
namespace Magento \Framework \Translate ;
10
10
11
- class Inline implements \Magento \Framework \Translate \InlineInterface
11
+ use Magento \Framework \App \Area ;
12
+ use Magento \Framework \App \ObjectManager ;
13
+ use Magento \Framework \App \ScopeInterface ;
14
+ use Magento \Framework \App \ScopeResolverInterface ;
15
+ use Magento \Framework \App \State ;
16
+ use Magento \Framework \Exception \LocalizedException ;
17
+ use Magento \Framework \Translate \Inline \ConfigInterface ;
18
+ use Magento \Framework \Translate \Inline \ParserInterface ;
19
+ use Magento \Framework \Translate \Inline \StateInterface ;
20
+ use Magento \Framework \UrlInterface ;
21
+ use Magento \Framework \View \Element \Template ;
22
+ use Magento \Framework \View \LayoutInterface ;
23
+
24
+ /**
25
+ * Translate Inline Class
26
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
+ */
28
+ class Inline implements InlineInterface
12
29
{
13
30
/**
14
31
* Indicator to hold state of whether inline translation is allowed
@@ -18,7 +35,7 @@ class Inline implements \Magento\Framework\Translate\InlineInterface
18
35
protected $ isAllowed ;
19
36
20
37
/**
21
- * @var \Magento\Framework\Translate\Inline\ ParserInterface
38
+ * @var ParserInterface
22
39
*/
23
40
protected $ parser ;
24
41
@@ -30,22 +47,22 @@ class Inline implements \Magento\Framework\Translate\InlineInterface
30
47
protected $ isScriptInserted = false ;
31
48
32
49
/**
33
- * @var \Magento\Framework\ UrlInterface
50
+ * @var UrlInterface
34
51
*/
35
52
protected $ url ;
36
53
37
54
/**
38
- * @var \Magento\Framework\View\ LayoutInterface
55
+ * @var LayoutInterface
39
56
*/
40
57
protected $ layout ;
41
58
42
59
/**
43
- * @var \Magento\Framework\Translate\Inline\ ConfigInterface
60
+ * @var ConfigInterface
44
61
*/
45
62
protected $ config ;
46
63
47
64
/**
48
- * @var \Magento\Framework\App\ ScopeResolverInterface
65
+ * @var ScopeResolverInterface
49
66
*/
50
67
protected $ scopeResolver ;
51
68
@@ -72,52 +89,48 @@ class Inline implements \Magento\Framework\Translate\InlineInterface
72
89
/**
73
90
* @var array
74
91
*/
75
- private $ allowedAreas = [
76
- \Magento \Framework \App \Area::AREA_FRONTEND ,
77
- \Magento \Framework \App \Area::AREA_ADMINHTML
78
- ];
92
+ private $ allowedAreas = [Area::AREA_FRONTEND , Area::AREA_ADMINHTML ];
79
93
80
94
/**
81
- * @var \Magento\Framework\App\ State
95
+ * @var State
82
96
*/
83
97
private $ appState ;
84
98
85
99
/**
86
- * Initialize inline translation model
87
- *
88
- * @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver
89
- * @param \Magento\Framework\UrlInterface $url
90
- * @param \Magento\Framework\View\LayoutInterface $layout
100
+ * @param ScopeResolverInterface $scopeResolver
101
+ * @param UrlInterface $url
102
+ * @param LayoutInterface $layout
91
103
* @param Inline\ConfigInterface $config
92
104
* @param Inline\ParserInterface $parser
93
105
* @param Inline\StateInterface $state
94
- * @param \Magento\Framework\App\State $appState
95
106
* @param string $templateFileName
96
107
* @param string $translatorRoute
97
- * @param null $scope
108
+ * @param string|null $scope
109
+ * @param State|null $appState
110
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
98
111
*/
99
112
public function __construct (
100
- \Magento \Framework \App \ScopeResolverInterface $ scopeResolver ,
101
- \Magento \Framework \UrlInterface $ url ,
102
- \Magento \Framework \View \LayoutInterface $ layout ,
103
- \Magento \Framework \Translate \Inline \ConfigInterface $ config ,
104
- \Magento \Framework \Translate \Inline \ParserInterface $ parser ,
105
- \Magento \Framework \Translate \Inline \StateInterface $ state ,
106
- \Magento \Framework \App \State $ appState ,
113
+ ScopeResolverInterface $ scopeResolver ,
114
+ UrlInterface $ url ,
115
+ LayoutInterface $ layout ,
116
+ ConfigInterface $ config ,
117
+ ParserInterface $ parser ,
118
+ StateInterface $ state ,
107
119
$ templateFileName = '' ,
108
120
$ translatorRoute = '' ,
109
- $ scope = null
121
+ $ scope = null ,
122
+ ?State $ appState = null
110
123
) {
111
124
$ this ->scopeResolver = $ scopeResolver ;
112
125
$ this ->url = $ url ;
113
126
$ this ->layout = $ layout ;
114
127
$ this ->config = $ config ;
115
128
$ this ->parser = $ parser ;
116
129
$ this ->state = $ state ;
117
- $ this ->appState = $ appState ;
118
130
$ this ->templateFileName = $ templateFileName ;
119
131
$ this ->translatorRoute = $ translatorRoute ;
120
132
$ this ->scope = $ scope ;
133
+ $ this ->appState = $ appState ?: ObjectManager::getInstance ()->get (State::class);
121
134
}
122
135
123
136
/**
@@ -128,13 +141,13 @@ public function __construct(
128
141
public function isAllowed ()
129
142
{
130
143
if ($ this ->isAllowed === null ) {
131
- if (!$ this ->scope instanceof \Magento \Framework \App \ScopeInterface) {
132
- $ scope = $ this ->scopeResolver ->getScope ($ this ->scope );
133
- }
144
+ $ scope = $ this ->scope instanceof ScopeInterface ? null : $ this ->scopeResolver ->getScope ($ this ->scope );
145
+
134
146
$ this ->isAllowed = $ this ->config ->isActive ($ scope )
135
147
&& $ this ->config ->isDevAllowed ($ scope )
136
148
&& $ this ->isAreaAllowed ();
137
149
}
150
+
138
151
return $ this ->state ->isEnabled () && $ this ->isAllowed ;
139
152
}
140
153
@@ -151,7 +164,7 @@ public function getParser()
151
164
/**
152
165
* Replace translation templates with HTML fragments
153
166
*
154
- * @param array|string & $body
167
+ * @param array|string $body
155
168
* @param bool $isJson
156
169
* @return $this
157
170
*/
@@ -206,7 +219,9 @@ protected function addInlineScript()
206
219
return ;
207
220
}
208
221
if (!$ this ->isScriptInserted ) {
209
- $ this ->getParser ()->setContent (str_ireplace ('</body> ' , $ this ->getInlineScript () . '</body> ' , $ content ));
222
+ $ this ->getParser ()->setContent (
223
+ str_ireplace ('</body> ' , $ this ->getInlineScript () . '</body> ' , $ content )
224
+ );
210
225
$ this ->isScriptInserted = true ;
211
226
}
212
227
}
@@ -221,8 +236,8 @@ protected function addInlineScript()
221
236
*/
222
237
protected function getInlineScript ()
223
238
{
224
- /** @var $block \Magento\Framework\View\Element\ Template */
225
- $ block = $ this ->layout ->createBlock (\ Magento \ Framework \ View \ Element \ Template::class);
239
+ /** @var $block Template */
240
+ $ block = $ this ->layout ->createBlock (Template::class);
226
241
227
242
$ block ->setAjaxUrl ($ this ->getAjaxUrl ());
228
243
$ block ->setTemplate ($ this ->templateFileName );
@@ -255,15 +270,10 @@ protected function stripInlineTranslations(&$body)
255
270
foreach ($ body as &$ part ) {
256
271
$ this ->stripInlineTranslations ($ part );
257
272
}
258
- } else {
259
- if (is_string ($ body )) {
260
- $ body = preg_replace (
261
- '# ' . \Magento \Framework \Translate \Inline \ParserInterface::REGEXP_TOKEN . '# ' ,
262
- '$1 ' ,
263
- $ body
264
- );
265
- }
273
+ } elseif (is_string ($ body )) {
274
+ $ body = preg_replace ('# ' . ParserInterface::REGEXP_TOKEN . '# ' , '$1 ' , $ body );
266
275
}
276
+
267
277
return $ this ;
268
278
}
269
279
@@ -272,14 +282,11 @@ protected function stripInlineTranslations(&$body)
272
282
*
273
283
* @return bool
274
284
*/
275
- private function isAreaAllowed ()
285
+ private function isAreaAllowed (): bool
276
286
{
277
287
try {
278
- return in_array (
279
- $ this ->appState ->getAreaCode (),
280
- $ this ->allowedAreas
281
- );
282
- } catch (\Magento \Framework \Exception \LocalizedException $ e ) {
288
+ return in_array ($ this ->appState ->getAreaCode (), $ this ->allowedAreas , true );
289
+ } catch (LocalizedException $ e ) {
283
290
return false ;
284
291
}
285
292
}
0 commit comments