Theme.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\View\Design\ThemeInterface;
  9. use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
  10. /**
  11. * Theme model class
  12. *
  13. * @method string getPackageCode()
  14. * @method string getParentThemePath()
  15. * @method string getParentId()
  16. * @method string getThemeTitle()
  17. * @method string getPreviewImage()
  18. * @method bool getIsFeatured()
  19. * @method int getThemeId()
  20. * @method int getType()
  21. * @method array getAssignedStores()
  22. * @method ThemeInterface setAssignedStores(array $stores)
  23. * @method ThemeInterface setParentId(int $id)
  24. * @method ThemeInterface setParentTheme($parentTheme)
  25. * @method ThemeInterface setPackageCode(string $packageCode)
  26. * @method ThemeInterface setThemeCode(string $themeCode)
  27. * @method ThemeInterface setThemePath(string $themePath)
  28. * @method ThemeInterface setThemeTitle(string $themeTitle)
  29. * @method ThemeInterface setType(int $type)
  30. * @method ThemeInterface setCode(string $code)
  31. *
  32. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  33. */
  34. class Theme extends \Magento\Framework\Model\AbstractModel implements ThemeInterface
  35. {
  36. /**
  37. * {@inheritdoc}
  38. *
  39. * @var string
  40. */
  41. protected $_eventPrefix = 'theme';
  42. /**
  43. * {@inheritdoc}
  44. *
  45. * @var string
  46. */
  47. protected $_eventObject = 'theme';
  48. /**
  49. * @var \Magento\Framework\View\Design\Theme\FlyweightFactory
  50. */
  51. protected $_themeFactory;
  52. /**
  53. * @var \Magento\Framework\View\Design\Theme\Domain\Factory
  54. */
  55. protected $_domainFactory;
  56. /**
  57. * @var \Magento\Framework\View\Design\Theme\ImageFactory
  58. */
  59. protected $_imageFactory;
  60. /**
  61. * @var \Magento\Framework\View\Design\Theme\Validator
  62. */
  63. protected $_validator;
  64. /**
  65. * @var \Magento\Framework\View\Design\Theme\Customization
  66. */
  67. protected $_customization;
  68. /**
  69. * @var \Magento\Framework\View\Design\Theme\CustomizationFactory
  70. */
  71. protected $_customFactory;
  72. /**
  73. * @var ThemeFactory
  74. */
  75. private $themeModelFactory;
  76. /**
  77. * @var ThemeInterface[]
  78. */
  79. protected $inheritanceSequence;
  80. /**
  81. * Initialize dependencies
  82. *
  83. * @param \Magento\Framework\Model\Context $context
  84. * @param \Magento\Framework\Registry $registry
  85. * @param \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory
  86. * @param \Magento\Framework\View\Design\Theme\Domain\Factory $domainFactory
  87. * @param \Magento\Framework\View\Design\Theme\ImageFactory $imageFactory
  88. * @param \Magento\Framework\View\Design\Theme\Validator $validator
  89. * @param \Magento\Framework\View\Design\Theme\CustomizationFactory $customizationFactory
  90. * @param \Magento\Theme\Model\ResourceModel\Theme $resource
  91. * @param \Magento\Theme\Model\ResourceModel\Theme\Collection $resourceCollection
  92. * @param array $data
  93. * @param ThemeFactory $themeModelFactory
  94. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  95. */
  96. public function __construct(
  97. \Magento\Framework\Model\Context $context,
  98. \Magento\Framework\Registry $registry,
  99. \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory,
  100. \Magento\Framework\View\Design\Theme\Domain\Factory $domainFactory,
  101. \Magento\Framework\View\Design\Theme\ImageFactory $imageFactory,
  102. \Magento\Framework\View\Design\Theme\Validator $validator,
  103. \Magento\Framework\View\Design\Theme\CustomizationFactory $customizationFactory,
  104. \Magento\Theme\Model\ResourceModel\Theme $resource = null,
  105. ThemeCollection $resourceCollection = null,
  106. array $data = [],
  107. ThemeFactory $themeModelFactory = null
  108. ) {
  109. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  110. $this->_themeFactory = $themeFactory;
  111. $this->_domainFactory = $domainFactory;
  112. $this->_imageFactory = $imageFactory;
  113. $this->_validator = $validator;
  114. $this->_customFactory = $customizationFactory;
  115. $this->themeModelFactory = $themeModelFactory ?: ObjectManager::getInstance()->get(ThemeFactory::class);
  116. $this->addData(['type' => self::TYPE_VIRTUAL]);
  117. }
  118. /**
  119. * Init resource model
  120. *
  121. * @return void
  122. */
  123. protected function _construct()
  124. {
  125. $this->_init(\Magento\Theme\Model\ResourceModel\Theme::class);
  126. }
  127. /**
  128. * Get theme image model
  129. *
  130. * @return \Magento\Framework\View\Design\Theme\Image
  131. */
  132. public function getThemeImage()
  133. {
  134. return $this->_imageFactory->create(['theme' => $this]);
  135. }
  136. /**
  137. * @return \Magento\Framework\View\Design\Theme\Customization
  138. */
  139. public function getCustomization()
  140. {
  141. if ($this->_customization === null) {
  142. $this->_customization = $this->_customFactory->create(['theme' => $this]);
  143. }
  144. return $this->_customization;
  145. }
  146. /**
  147. * Check if theme is deletable
  148. *
  149. * @return bool
  150. */
  151. public function isDeletable()
  152. {
  153. return $this->isEditable();
  154. }
  155. /**
  156. * Check if theme is editable
  157. *
  158. * @return bool
  159. */
  160. public function isEditable()
  161. {
  162. return self::TYPE_PHYSICAL != $this->getType();
  163. }
  164. /**
  165. * Check if theme is virtual
  166. *
  167. * @return bool
  168. */
  169. public function isVirtual()
  170. {
  171. return $this->getType() == self::TYPE_VIRTUAL;
  172. }
  173. /**
  174. * Check if theme is physical
  175. *
  176. * @return bool
  177. */
  178. public function isPhysical()
  179. {
  180. return $this->getType() == self::TYPE_PHYSICAL;
  181. }
  182. /**
  183. * Check theme is visible in backend
  184. *
  185. * @return bool
  186. */
  187. public function isVisible()
  188. {
  189. return in_array($this->getType(), [self::TYPE_PHYSICAL, self::TYPE_VIRTUAL]);
  190. }
  191. /**
  192. * Check is theme has child virtual themes
  193. *
  194. * @return bool
  195. */
  196. public function hasChildThemes()
  197. {
  198. return (bool)$this->getCollection()->addTypeFilter(
  199. self::TYPE_VIRTUAL
  200. )->addFieldToFilter(
  201. 'parent_id',
  202. ['eq' => $this->getId()]
  203. )->getSize();
  204. }
  205. /**
  206. * Retrieve theme instance representing the latest changes to a theme
  207. *
  208. * @return Theme|null
  209. */
  210. public function getStagingVersion()
  211. {
  212. if ($this->getId()) {
  213. $collection = $this->getCollection();
  214. $collection->addFieldToFilter('parent_id', $this->getId());
  215. $collection->addFieldToFilter('type', self::TYPE_STAGING);
  216. $stagingTheme = $collection->getFirstItem();
  217. if ($stagingTheme->getId()) {
  218. return $stagingTheme;
  219. }
  220. }
  221. return null;
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function getParentTheme()
  227. {
  228. if ($this->hasData('parent_theme')) {
  229. return $this->getData('parent_theme');
  230. }
  231. $theme = null;
  232. if ($this->getParentId()) {
  233. $theme = $this->_themeFactory->create($this->getParentId());
  234. }
  235. $this->setParentTheme($theme);
  236. return $theme;
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function getArea()
  242. {
  243. // In order to support environment emulation of area, if area is set, return it
  244. if ($this->getData('area') && !$this->_appState->isAreaCodeEmulated()) {
  245. return $this->getData('area');
  246. }
  247. return $this->_appState->getAreaCode();
  248. }
  249. /**
  250. * {@inheritdoc}
  251. */
  252. public function getThemePath()
  253. {
  254. return $this->getData('theme_path');
  255. }
  256. /**
  257. * Retrieve theme full path which is used to distinguish themes if they are not in DB yet
  258. *
  259. * Alternative id looks like "<area>/<theme_path>".
  260. * Used as id in file-system theme collection
  261. *
  262. * @return string|null
  263. */
  264. public function getFullPath()
  265. {
  266. return $this->getThemePath() ? $this->getArea() . self::PATH_SEPARATOR . $this->getThemePath() : null;
  267. }
  268. /**
  269. * {@inheritdoc}
  270. */
  271. public function getCode()
  272. {
  273. return (string)$this->getData('code');
  274. }
  275. /**
  276. * Get one of theme domain models
  277. *
  278. * @param int|null $type
  279. * @return \Magento\Theme\Model\Theme\Domain\Virtual|\Magento\Theme\Model\Theme\Domain\Staging
  280. * @throws \InvalidArgumentException
  281. */
  282. public function getDomainModel($type = null)
  283. {
  284. if ($type !== null && $type != $this->getType()) {
  285. throw new \InvalidArgumentException(
  286. sprintf(
  287. 'Invalid domain model "%s" requested for theme "%s" of type "%s"',
  288. $type,
  289. $this->getId(),
  290. $this->getType()
  291. )
  292. );
  293. }
  294. return $this->_domainFactory->create($this);
  295. }
  296. /**
  297. * Validate theme data
  298. *
  299. * @return $this
  300. * @throws \Magento\Framework\Exception\LocalizedException
  301. */
  302. protected function _validate()
  303. {
  304. if (!$this->_validator->validate($this)) {
  305. $messages = $this->_validator->getErrorMessages();
  306. throw new \Magento\Framework\Exception\LocalizedException(__(implode(PHP_EOL, reset($messages))));
  307. }
  308. return $this;
  309. }
  310. /**
  311. * Before theme save
  312. *
  313. * @return $this
  314. */
  315. public function beforeSave()
  316. {
  317. $this->_validate();
  318. return parent::beforeSave();
  319. }
  320. /**
  321. * Update all relations after deleting theme
  322. *
  323. * @return $this
  324. */
  325. public function afterDelete()
  326. {
  327. $stagingVersion = $this->getStagingVersion();
  328. if ($stagingVersion) {
  329. $stagingVersion->delete();
  330. }
  331. $this->getCollection()->updateChildRelations($this);
  332. return parent::afterDelete();
  333. }
  334. /**
  335. * Return the full theme inheritance sequence, from the root theme till a specified one
  336. *
  337. * @return ThemeInterface[]
  338. */
  339. public function getInheritedThemes()
  340. {
  341. if (null === $this->inheritanceSequence) {
  342. $theme = $this;
  343. $result = [];
  344. while ($theme) {
  345. $result[] = $theme;
  346. $theme = $theme->getParentTheme();
  347. }
  348. $this->inheritanceSequence = array_reverse($result);
  349. }
  350. return $this->inheritanceSequence;
  351. }
  352. /**
  353. * @inheritdoc
  354. */
  355. public function toArray(array $keys = [])
  356. {
  357. $data = parent::toArray($keys);
  358. if (isset($data['parent_theme'])) {
  359. $data['parent_theme'] = $this->getParentTheme()->toArray();
  360. }
  361. if (isset($data['inherited_themes'])) {
  362. foreach ($data['inherited_themes'] as $key => $inheritedTheme) {
  363. $data['inherited_themes'][$key] = $inheritedTheme->toArray();
  364. }
  365. }
  366. return $data;
  367. }
  368. /**
  369. * Populate Theme object from an array
  370. *
  371. * @param array $data
  372. * @return Theme
  373. */
  374. public function populateFromArray(array $data)
  375. {
  376. $this->_data = $data;
  377. if (isset($data['parent_theme'])) {
  378. $this->_data['parent_theme'] = $this->createThemeInstance()->populateFromArray($data['parent_theme']);
  379. }
  380. if (isset($data['inherited_themes'])) {
  381. foreach ($data['inherited_themes'] as $key => $inheritedTheme) {
  382. $themeInstance = $this->createThemeInstance()->populateFromArray($inheritedTheme);
  383. $this->_data['inherited_themes'][$key] = $themeInstance;
  384. }
  385. }
  386. return $this;
  387. }
  388. /**
  389. * Create Theme instance
  390. *
  391. * @return \Magento\Theme\Model\Theme
  392. */
  393. private function createThemeInstance()
  394. {
  395. return $this->themeModelFactory->create();
  396. }
  397. }