Proxy.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Translate\Inline;
  7. /**
  8. * Proxy class for \Magento\Framework\Translate\Inline
  9. */
  10. class Proxy extends \Magento\Framework\Translate\Inline implements
  11. \Magento\Framework\ObjectManager\NoninterceptableInterface
  12. {
  13. /**
  14. * Object Manager instance
  15. *
  16. * @var \Magento\Framework\ObjectManagerInterface
  17. */
  18. protected $objectManager;
  19. /**
  20. * Proxied instance name
  21. *
  22. * @var string
  23. */
  24. protected $instanceName;
  25. /**
  26. * Proxied instance
  27. *
  28. * @var \Magento\Framework\Translate\Inline
  29. */
  30. protected $subject;
  31. /**
  32. * Instance shareability flag
  33. *
  34. * @var bool
  35. */
  36. protected $isShared;
  37. /**
  38. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  39. * @param string $instanceName
  40. * @param bool $shared
  41. */
  42. public function __construct(
  43. \Magento\Framework\ObjectManagerInterface $objectManager,
  44. $instanceName = \Magento\Framework\Translate\Inline::class,
  45. $shared = true
  46. ) {
  47. $this->objectManager = $objectManager;
  48. $this->instanceName = $instanceName;
  49. $this->isShared = $shared;
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function __sleep()
  55. {
  56. return ['subject', 'isShared'];
  57. }
  58. /**
  59. * Retrieve ObjectManager from global scope
  60. *
  61. * @return void
  62. */
  63. public function __wakeup()
  64. {
  65. $this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  66. }
  67. /**
  68. * Clone proxied instance
  69. *
  70. * @return void
  71. */
  72. public function __clone()
  73. {
  74. $this->subject = clone $this->_getSubject();
  75. }
  76. /**
  77. * Get proxied instance
  78. *
  79. * @return \Magento\Framework\Translate\Inline
  80. */
  81. protected function _getSubject()
  82. {
  83. if (!$this->subject) {
  84. $this->subject = true === $this->isShared
  85. ? $this->objectManager->get($this->instanceName)
  86. : $this->objectManager->create($this->instanceName);
  87. }
  88. return $this->subject;
  89. }
  90. /**
  91. * Check if Inline Translates is allowed
  92. *
  93. * @return bool
  94. */
  95. public function isAllowed()
  96. {
  97. return $this->_getSubject()->isAllowed();
  98. }
  99. /**
  100. * Retrieve Inline Parser instance
  101. *
  102. * @return \Magento\Framework\Translate\Inline\ParserInterface
  103. */
  104. public function getParser()
  105. {
  106. return $this->_getSubject()->getParser();
  107. }
  108. /**
  109. * Replace translation templates with HTML fragments
  110. *
  111. * @param array|string &$body
  112. * @param bool $isJson
  113. * @return $this
  114. */
  115. public function processResponseBody(&$body, $isJson = false)
  116. {
  117. return $this->_getSubject()->processResponseBody($body, $isJson);
  118. }
  119. /**
  120. * Additional translation mode html attribute is not needed for base inline translation.
  121. *
  122. * @param mixed|string|null $tagName
  123. * @return mixed
  124. */
  125. public function getAdditionalHtmlAttribute($tagName = null)
  126. {
  127. return $this->_getSubject()->getAdditionalHtmlAttribute($tagName);
  128. }
  129. }