Chain.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Asset\PreProcessor;
  7. use Magento\Framework\View\Asset\LocalInterface;
  8. /**
  9. * An object that's passed to preprocessors to carry current and original information for processing
  10. * Encapsulates complexity of all necessary context and parameters
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Chain
  16. {
  17. /**
  18. * @var array
  19. */
  20. private $compatibleTypes;
  21. /**
  22. * @var LocalInterface
  23. */
  24. private $asset;
  25. /**
  26. * @var string
  27. */
  28. private $origContent;
  29. /**
  30. * @var string
  31. */
  32. protected $origContentType;
  33. /**
  34. * @var string
  35. */
  36. private $content;
  37. /**
  38. * @var string
  39. */
  40. private $contentType;
  41. /**
  42. * @var string
  43. */
  44. protected $targetContentType;
  45. /**
  46. * @var null|string
  47. */
  48. protected $targetAssetPath;
  49. /**
  50. * @var string
  51. */
  52. protected $origAssetPath;
  53. /**
  54. * @param LocalInterface $asset
  55. * @param string $origContent
  56. * @param string $origContentType
  57. * @param string $origAssetPath
  58. * @param array $compatibleTypes
  59. */
  60. public function __construct(
  61. LocalInterface $asset,
  62. $origContent,
  63. $origContentType,
  64. $origAssetPath,
  65. array $compatibleTypes = []
  66. ) {
  67. $this->asset = $asset;
  68. $this->origContent = $origContent;
  69. $this->content = $origContent;
  70. $this->origContentType = $origContentType;
  71. $this->contentType = $origContentType;
  72. $this->targetContentType = $asset->getContentType();
  73. $this->targetAssetPath = $asset->getPath();
  74. $this->origAssetPath = $origAssetPath;
  75. $this->compatibleTypes = $compatibleTypes;
  76. }
  77. /**
  78. * Get asset object
  79. *
  80. * @return LocalInterface
  81. */
  82. public function getAsset()
  83. {
  84. return $this->asset;
  85. }
  86. /**
  87. * Get original content
  88. *
  89. * @return string
  90. */
  91. public function getOrigContent()
  92. {
  93. return $this->origContent;
  94. }
  95. /**
  96. * Get current content
  97. *
  98. * @return string
  99. */
  100. public function getContent()
  101. {
  102. return $this->content;
  103. }
  104. /**
  105. * Set current content
  106. *
  107. * @param string $content
  108. * @return void
  109. */
  110. public function setContent($content)
  111. {
  112. $this->content = $content;
  113. }
  114. /**
  115. * Get original content type
  116. *
  117. * @return string
  118. */
  119. public function getOrigContentType()
  120. {
  121. return $this->origContentType;
  122. }
  123. /**
  124. * Get current content type
  125. *
  126. * @return string
  127. */
  128. public function getContentType()
  129. {
  130. return $this->contentType;
  131. }
  132. /**
  133. * Set current content type
  134. *
  135. * @param string $contentType
  136. * @return void
  137. */
  138. public function setContentType($contentType)
  139. {
  140. $this->contentType = $contentType;
  141. }
  142. /**
  143. * Get the intended content type
  144. *
  145. * @return string
  146. */
  147. public function getTargetContentType()
  148. {
  149. return $this->targetContentType;
  150. }
  151. /**
  152. * Get the target asset path
  153. *
  154. * @return string
  155. */
  156. public function getTargetAssetPath()
  157. {
  158. return $this->targetAssetPath;
  159. }
  160. /**
  161. * Assert invariants
  162. *
  163. * Impose an integrity check to avoid generating mismatching content type and not leaving transient data behind
  164. *
  165. * @return void
  166. * @throws \LogicException
  167. */
  168. public function assertValid()
  169. {
  170. if ($this->contentType !== $this->targetContentType
  171. && empty($this->compatibleTypes[$this->targetContentType][$this->contentType])) {
  172. throw new \LogicException(
  173. "The requested asset type was '{$this->targetContentType}', but ended up with '{$this->contentType}'"
  174. );
  175. }
  176. }
  177. /**
  178. * Whether the contents or type have changed during the lifetime of the object
  179. *
  180. * @return bool
  181. */
  182. public function isChanged()
  183. {
  184. return $this->origContentType != $this->contentType || $this->origContent != $this->content;
  185. }
  186. /**
  187. * @return string
  188. * @codeCoverageIgnore
  189. */
  190. public function getOrigAssetPath()
  191. {
  192. return $this->origAssetPath;
  193. }
  194. }