PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-theme/Model/Theme.php

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