Inline.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Inline Translations Library
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Translate;
  9. class Inline implements \Magento\Framework\Translate\InlineInterface
  10. {
  11. /**
  12. * Indicator to hold state of whether inline translation is allowed
  13. *
  14. * @var bool
  15. */
  16. protected $isAllowed;
  17. /**
  18. * @var \Magento\Framework\Translate\Inline\ParserInterface
  19. */
  20. protected $parser;
  21. /**
  22. * Flag about inserted styles and scripts for inline translates
  23. *
  24. * @var bool
  25. */
  26. protected $isScriptInserted = false;
  27. /**
  28. * @var \Magento\Framework\UrlInterface
  29. */
  30. protected $url;
  31. /**
  32. * @var \Magento\Framework\View\LayoutInterface
  33. */
  34. protected $layout;
  35. /**
  36. * @var \Magento\Framework\Translate\Inline\ConfigInterface
  37. */
  38. protected $config;
  39. /**
  40. * @var \Magento\Framework\App\ScopeResolverInterface
  41. */
  42. protected $scopeResolver;
  43. /**
  44. * @var string
  45. */
  46. protected $templateFileName;
  47. /**
  48. * @var string
  49. */
  50. protected $translatorRoute;
  51. /**
  52. * @var null|string
  53. */
  54. protected $scope;
  55. /**
  56. * @var Inline\StateInterface
  57. */
  58. protected $state;
  59. /**
  60. * Initialize inline translation model
  61. *
  62. * @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver
  63. * @param \Magento\Framework\UrlInterface $url
  64. * @param \Magento\Framework\View\LayoutInterface $layout
  65. * @param Inline\ConfigInterface $config
  66. * @param Inline\ParserInterface $parser
  67. * @param Inline\StateInterface $state
  68. * @param string $templateFileName
  69. * @param string $translatorRoute
  70. * @param null $scope
  71. */
  72. public function __construct(
  73. \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
  74. \Magento\Framework\UrlInterface $url,
  75. \Magento\Framework\View\LayoutInterface $layout,
  76. \Magento\Framework\Translate\Inline\ConfigInterface $config,
  77. \Magento\Framework\Translate\Inline\ParserInterface $parser,
  78. \Magento\Framework\Translate\Inline\StateInterface $state,
  79. $templateFileName = '',
  80. $translatorRoute = '',
  81. $scope = null
  82. ) {
  83. $this->scopeResolver = $scopeResolver;
  84. $this->url = $url;
  85. $this->layout = $layout;
  86. $this->config = $config;
  87. $this->parser = $parser;
  88. $this->state = $state;
  89. $this->templateFileName = $templateFileName;
  90. $this->translatorRoute = $translatorRoute;
  91. $this->scope = $scope;
  92. }
  93. /**
  94. * Check if Inline Translates is allowed
  95. *
  96. * @return bool
  97. */
  98. public function isAllowed()
  99. {
  100. if ($this->isAllowed === null) {
  101. if (!$this->scope instanceof \Magento\Framework\App\ScopeInterface) {
  102. $scope = $this->scopeResolver->getScope($this->scope);
  103. }
  104. $this->isAllowed = $this->config->isActive($scope)
  105. && $this->config->isDevAllowed($scope);
  106. }
  107. return $this->state->isEnabled() && $this->isAllowed;
  108. }
  109. /**
  110. * Retrieve Inline Parser instance
  111. *
  112. * @return Inline\ParserInterface
  113. */
  114. public function getParser()
  115. {
  116. return $this->parser;
  117. }
  118. /**
  119. * Replace translation templates with HTML fragments
  120. *
  121. * @param array|string &$body
  122. * @param bool $isJson
  123. * @return $this
  124. */
  125. public function processResponseBody(&$body, $isJson = false)
  126. {
  127. if (!$this->isAllowed()) {
  128. $this->stripInlineTranslations($body);
  129. return $this;
  130. }
  131. $this->getParser()->setIsJson($isJson);
  132. if (is_array($body)) {
  133. foreach ($body as &$part) {
  134. $this->processResponseBody($part, $isJson);
  135. }
  136. } elseif (is_string($body)) {
  137. $this->getParser()->processResponseBodyString($body);
  138. $this->addInlineScript();
  139. $body = $this->getParser()->getContent();
  140. }
  141. $this->getParser()->setIsJson(false);
  142. return $this;
  143. }
  144. /**
  145. * Additional translation mode html attribute is not needed for base inline translation.
  146. *
  147. * @param mixed|string|null $tagName
  148. * @return null
  149. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  150. */
  151. public function getAdditionalHtmlAttribute($tagName = null)
  152. {
  153. return null;
  154. }
  155. /**
  156. * Add inline script code
  157. *
  158. * Insert script and html with
  159. * added inline translation content.
  160. *
  161. * @return void
  162. */
  163. protected function addInlineScript()
  164. {
  165. $content = $this->getParser()->getContent();
  166. if (stripos($content, '</body>') === false) {
  167. return;
  168. }
  169. if (!$this->isScriptInserted) {
  170. $this->getParser()->setContent(str_ireplace('</body>', $this->getInlineScript() . '</body>', $content));
  171. $this->isScriptInserted = true;
  172. }
  173. }
  174. /**
  175. * Retrieve inline script code
  176. *
  177. * Create block to render script and html with
  178. * added inline translation content.
  179. *
  180. * @return string
  181. */
  182. protected function getInlineScript()
  183. {
  184. /** @var $block \Magento\Framework\View\Element\Template */
  185. $block = $this->layout->createBlock(\Magento\Framework\View\Element\Template::class);
  186. $block->setAjaxUrl($this->getAjaxUrl());
  187. $block->setTemplate($this->templateFileName);
  188. return $block->toHtml();
  189. }
  190. /**
  191. * Return URL for ajax requests
  192. *
  193. * @return string
  194. */
  195. protected function getAjaxUrl()
  196. {
  197. return $this->url->getUrl(
  198. $this->translatorRoute,
  199. ['_secure' => $this->scopeResolver->getScope()->isCurrentlySecure()]
  200. );
  201. }
  202. /**
  203. * Strip inline translations from text
  204. *
  205. * @param array|string &$body
  206. * @return $this
  207. */
  208. protected function stripInlineTranslations(&$body)
  209. {
  210. if (is_array($body)) {
  211. foreach ($body as &$part) {
  212. $this->stripInlineTranslations($part);
  213. }
  214. } else {
  215. if (is_string($body)) {
  216. $body = preg_replace(
  217. '#' . \Magento\Framework\Translate\Inline\ParserInterface::REGEXP_TOKEN . '#',
  218. '$1',
  219. $body
  220. );
  221. }
  222. }
  223. return $this;
  224. }
  225. }