Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Inline.php
Go to the documentation of this file.
1 <?php
10 
11 class Inline implements \Magento\Framework\Translate\InlineInterface
12 {
18  protected $isAllowed;
19 
23  protected $parser;
24 
30  protected $isScriptInserted = false;
31 
35  protected $url;
36 
40  protected $layout;
41 
45  protected $config;
46 
50  protected $scopeResolver;
51 
55  protected $templateFileName;
56 
60  protected $translatorRoute;
61 
65  protected $scope;
66 
70  protected $state;
71 
85  public function __construct(
86  \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
87  \Magento\Framework\UrlInterface $url,
88  \Magento\Framework\View\LayoutInterface $layout,
89  \Magento\Framework\Translate\Inline\ConfigInterface $config,
90  \Magento\Framework\Translate\Inline\ParserInterface $parser,
91  \Magento\Framework\Translate\Inline\StateInterface $state,
92  $templateFileName = '',
93  $translatorRoute = '',
94  $scope = null
95  ) {
96  $this->scopeResolver = $scopeResolver;
97  $this->url = $url;
98  $this->layout = $layout;
99  $this->config = $config;
100  $this->parser = $parser;
101  $this->state = $state;
102  $this->templateFileName = $templateFileName;
103  $this->translatorRoute = $translatorRoute;
104  $this->scope = $scope;
105  }
106 
112  public function isAllowed()
113  {
114  if ($this->isAllowed === null) {
115  if (!$this->scope instanceof \Magento\Framework\App\ScopeInterface) {
116  $scope = $this->scopeResolver->getScope($this->scope);
117  }
118  $this->isAllowed = $this->config->isActive($scope)
119  && $this->config->isDevAllowed($scope);
120  }
121  return $this->state->isEnabled() && $this->isAllowed;
122  }
123 
129  public function getParser()
130  {
131  return $this->parser;
132  }
133 
141  public function processResponseBody(&$body, $isJson = false)
142  {
143  if (!$this->isAllowed()) {
144  $this->stripInlineTranslations($body);
145  return $this;
146  }
147 
148  $this->getParser()->setIsJson($isJson);
149 
150  if (is_array($body)) {
151  foreach ($body as &$part) {
152  $this->processResponseBody($part, $isJson);
153  }
154  } elseif (is_string($body)) {
155  $this->getParser()->processResponseBodyString($body);
156  $this->addInlineScript();
157  $body = $this->getParser()->getContent();
158  }
159 
160  $this->getParser()->setIsJson(false);
161 
162  return $this;
163  }
164 
172  public function getAdditionalHtmlAttribute($tagName = null)
173  {
174  return null;
175  }
176 
185  protected function addInlineScript()
186  {
187  $content = $this->getParser()->getContent();
188  if (stripos($content, '</body>') === false) {
189  return;
190  }
191  if (!$this->isScriptInserted) {
192  $this->getParser()->setContent(str_ireplace('</body>', $this->getInlineScript() . '</body>', $content));
193  $this->isScriptInserted = true;
194  }
195  }
196 
205  protected function getInlineScript()
206  {
208  $block = $this->layout->createBlock(\Magento\Framework\View\Element\Template::class);
209 
210  $block->setAjaxUrl($this->getAjaxUrl());
211  $block->setTemplate($this->templateFileName);
212 
213  return $block->toHtml();
214  }
215 
221  protected function getAjaxUrl()
222  {
223  return $this->url->getUrl(
224  $this->translatorRoute,
225  ['_secure' => $this->scopeResolver->getScope()->isCurrentlySecure()]
226  );
227  }
228 
235  protected function stripInlineTranslations(&$body)
236  {
237  if (is_array($body)) {
238  foreach ($body as &$part) {
239  $this->stripInlineTranslations($part);
240  }
241  } else {
242  if (is_string($body)) {
243  $body = preg_replace(
244  '#' . \Magento\Framework\Translate\Inline\ParserInterface::REGEXP_TOKEN . '#',
245  '$1',
246  $body
247  );
248  }
249  }
250  return $this;
251  }
252 }
getAdditionalHtmlAttribute($tagName=null)
Definition: Inline.php:172
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$block
Definition: block.php:8
processResponseBody(&$body, $isJson=false)
Definition: Inline.php:141
__construct(\Magento\Framework\App\ScopeResolverInterface $scopeResolver, \Magento\Framework\UrlInterface $url, \Magento\Framework\View\LayoutInterface $layout, \Magento\Framework\Translate\Inline\ConfigInterface $config, \Magento\Framework\Translate\Inline\ParserInterface $parser, \Magento\Framework\Translate\Inline\StateInterface $state, $templateFileName='', $translatorRoute='', $scope=null)
Definition: Inline.php:85