vendor/sulu/sulu/src/Sulu/Component/Webspace/Webspace.php line 20

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Component\Webspace;
  11. use Sulu\Component\Localization\Localization;
  12. use Sulu\Component\Util\ArrayableInterface;
  13. /**
  14. * Container for a webspace definition.
  15. */
  16. class Webspace implements ArrayableInterface
  17. {
  18. /**
  19. * The name of the webspace.
  20. *
  21. * @var string
  22. */
  23. private $name;
  24. /**
  25. * The key of the webspace.
  26. *
  27. * @var string
  28. */
  29. private $key;
  30. /**
  31. * The localizations defined for this webspace.
  32. *
  33. * @var Localization[]
  34. */
  35. private $localizations = [];
  36. /**
  37. * The default localization defined for this webspace.
  38. *
  39. * @var Localization
  40. */
  41. private $defaultLocalization;
  42. /**
  43. * The x-default localization defined for this webspace.
  44. *
  45. * @var Localization
  46. */
  47. private $xDefaultLocalization;
  48. /**
  49. * The segments defined for this webspace.
  50. *
  51. * @var Segment[]
  52. */
  53. private $segments = [];
  54. /**
  55. * The default segment defined for this webspace.
  56. *
  57. * @var Segment
  58. */
  59. private $defaultSegment;
  60. /**
  61. * The theme of the webspace.
  62. *
  63. * @var string
  64. */
  65. private $theme;
  66. /**
  67. * The portals defined for this webspace.
  68. *
  69. * @var Portal[]
  70. */
  71. private $portals = [];
  72. /**
  73. * The security system for this webspace.
  74. *
  75. * @var Security|null
  76. */
  77. private $security;
  78. /**
  79. * Navigation for this webspace.
  80. *
  81. * @var Navigation
  82. */
  83. private $navigation;
  84. /**
  85. * A list of twig templates.
  86. *
  87. * @var array
  88. */
  89. private $templates = [];
  90. /**
  91. * Template which is selected by default if no other template is chosen.
  92. *
  93. * @var string[]
  94. */
  95. private $defaultTemplates = [];
  96. /**
  97. * @var string[]
  98. */
  99. private $excludedTemplates = [];
  100. /**
  101. * The url generation strategy for this portal.
  102. *
  103. * @var string
  104. */
  105. private $resourceLocatorStrategy;
  106. /**
  107. * Sets the key of the webspace.
  108. *
  109. * @param string $key
  110. *
  111. * @return void
  112. */
  113. public function setKey($key)
  114. {
  115. $this->key = $key;
  116. }
  117. /**
  118. * Returns the key of the webspace.
  119. *
  120. * @return string
  121. */
  122. public function getKey()
  123. {
  124. return $this->key;
  125. }
  126. /**
  127. * Adds a localization to the webspace.
  128. *
  129. * @return void
  130. */
  131. public function addLocalization(Localization $localization)
  132. {
  133. $this->localizations[] = $localization;
  134. if ($localization->isDefault()) {
  135. $this->setDefaultLocalization($localization);
  136. }
  137. if ($localization->isXDefault(false)) {
  138. $this->xDefaultLocalization = $localization;
  139. }
  140. }
  141. /**
  142. * Returns the localizations of this webspace.
  143. *
  144. * @param Localization[] $localizations
  145. *
  146. * @return void
  147. */
  148. public function setLocalizations($localizations)
  149. {
  150. $this->localizations = $localizations;
  151. }
  152. /**
  153. * Returns the localizations of this webspace.
  154. *
  155. * @return Localization[]
  156. */
  157. public function getLocalizations()
  158. {
  159. return $this->localizations;
  160. }
  161. /**
  162. * Returns a list of all localizations and sublocalizations.
  163. *
  164. * @return Localization[]
  165. */
  166. public function getAllLocalizations()
  167. {
  168. $localizations = [];
  169. foreach ($this->getLocalizations() as $child) {
  170. $localizations[] = $child;
  171. $localizations = \array_merge($localizations, $child->getAllLocalizations());
  172. }
  173. return $localizations;
  174. }
  175. /**
  176. * Returns the localization object for a given localization string.
  177. *
  178. * @param string $localization
  179. *
  180. * @return Localization|null
  181. */
  182. public function getLocalization($localization)
  183. {
  184. $localizations = $this->getLocalizations();
  185. if (!empty($localizations)) {
  186. foreach ($localizations as $webspaceLocalization) {
  187. $result = $webspaceLocalization->findLocalization($localization);
  188. if ($result) {
  189. return $result;
  190. }
  191. }
  192. }
  193. return null;
  194. }
  195. /**
  196. * Sets the default localization for this webspace.
  197. *
  198. * @param Localization $defaultLocalization
  199. *
  200. * @return void
  201. */
  202. public function setDefaultLocalization($defaultLocalization)
  203. {
  204. $this->defaultLocalization = $defaultLocalization;
  205. if (!$this->getXDefaultLocalization()) {
  206. $this->xDefaultLocalization = $defaultLocalization;
  207. }
  208. }
  209. /**
  210. * Returns the default localization for this webspace.
  211. *
  212. * @return Localization
  213. */
  214. public function getDefaultLocalization()
  215. {
  216. if (!$this->defaultLocalization) {
  217. return $this->localizations[0];
  218. }
  219. return $this->defaultLocalization;
  220. }
  221. /**
  222. * Returns the x-default localization for this webspace.
  223. *
  224. * @return Localization
  225. */
  226. public function getXDefaultLocalization()
  227. {
  228. if (!$this->xDefaultLocalization) {
  229. return $this->localizations[0];
  230. }
  231. return $this->xDefaultLocalization;
  232. }
  233. /**
  234. * Sets the name of the webspace.
  235. *
  236. * @param string $name
  237. *
  238. * @return void
  239. */
  240. public function setName($name)
  241. {
  242. $this->name = $name;
  243. }
  244. /**
  245. * Returns the name of the webspace.
  246. *
  247. * @return string
  248. */
  249. public function getName()
  250. {
  251. return $this->name;
  252. }
  253. /**
  254. * Adds a portal to the webspace.
  255. *
  256. * @return void
  257. */
  258. public function addPortal(Portal $portal)
  259. {
  260. $this->portals[] = $portal;
  261. }
  262. /**
  263. * Sets the portals of this webspace.
  264. *
  265. * @param Portal[] $portals
  266. *
  267. * @return void
  268. */
  269. public function setPortals($portals)
  270. {
  271. $this->portals = $portals;
  272. }
  273. /**
  274. * Returns the portals of this webspace.
  275. *
  276. * @return Portal[]
  277. */
  278. public function getPortals()
  279. {
  280. return $this->portals;
  281. }
  282. /**
  283. * Adds a segment to the webspace.
  284. *
  285. * @return void
  286. */
  287. public function addSegment(Segment $segment)
  288. {
  289. $this->segments[] = $segment;
  290. if ($segment->isDefault()) {
  291. $this->setDefaultSegment($segment);
  292. }
  293. }
  294. /**
  295. * Sets the segments of this webspace.
  296. *
  297. * @param Segment[] $segments
  298. *
  299. * @return void
  300. */
  301. public function setSegments($segments)
  302. {
  303. $this->segments = $segments;
  304. }
  305. /**
  306. * Returns the segments of this webspace.
  307. *
  308. * @return Segment[]
  309. */
  310. public function getSegments()
  311. {
  312. return $this->segments;
  313. }
  314. public function getSegment(string $segmentKey): ?Segment
  315. {
  316. foreach ($this->segments as $segment) {
  317. if ($segment->getKey() === $segmentKey) {
  318. return $segment;
  319. }
  320. }
  321. return null;
  322. }
  323. /**
  324. * Sets the default segment of this webspace.
  325. *
  326. * @param Segment $defaultSegment
  327. *
  328. * @return void
  329. */
  330. public function setDefaultSegment($defaultSegment)
  331. {
  332. $this->defaultSegment = $defaultSegment;
  333. }
  334. /**
  335. * Returns the default segment for this webspace.
  336. *
  337. * @return Segment
  338. */
  339. public function getDefaultSegment()
  340. {
  341. return $this->defaultSegment;
  342. }
  343. /**
  344. * Sets the theme for this portal.
  345. *
  346. * @param string|null $theme this parameter is options
  347. *
  348. * @return void
  349. */
  350. public function setTheme($theme = null)
  351. {
  352. $this->theme = $theme;
  353. }
  354. /**
  355. * Returns the theme for this portal.
  356. *
  357. * @return string
  358. */
  359. public function getTheme()
  360. {
  361. return $this->theme;
  362. }
  363. /**
  364. * Sets the security system.
  365. *
  366. * @param Security|null $security
  367. *
  368. * @return void
  369. */
  370. public function setSecurity($security)
  371. {
  372. $this->security = $security;
  373. }
  374. /**
  375. * Returns the security system.
  376. *
  377. * @return Security|null
  378. */
  379. public function getSecurity()
  380. {
  381. return $this->security;
  382. }
  383. /**
  384. * @return bool
  385. */
  386. public function hasWebsiteSecurity()
  387. {
  388. $security = $this->getSecurity();
  389. if (!$security) {
  390. return false;
  391. }
  392. return null !== $security->getSystem() && $security->getPermissionCheck();
  393. }
  394. /**
  395. * @return Navigation
  396. */
  397. public function getNavigation()
  398. {
  399. return $this->navigation;
  400. }
  401. /**
  402. * @param Navigation $navigation
  403. *
  404. * @return void
  405. */
  406. public function setNavigation($navigation)
  407. {
  408. $this->navigation = $navigation;
  409. }
  410. /**
  411. * Returns false if domain not exists in webspace.
  412. *
  413. * @param string $domain
  414. * @param string $environment
  415. * @param string $locale
  416. *
  417. * @return bool
  418. *
  419. * @throws Exception\EnvironmentNotFoundException
  420. */
  421. public function hasDomain($domain, $environment, $locale = null)
  422. {
  423. $localizationParts = \explode('_', $locale);
  424. $language = $localizationParts[0];
  425. $country = isset($localizationParts[1]) ? $localizationParts[1] : '';
  426. foreach ($this->getPortals() as $portal) {
  427. foreach ($portal->getEnvironment($environment)->getUrls() as $url) {
  428. $host = \parse_url('//' . $url->getUrl())['host'];
  429. if ((null === $locale || $url->isValidLocale($language, $country))
  430. && ($host === $domain || '{host}' === $host)
  431. ) {
  432. return true;
  433. }
  434. }
  435. }
  436. return false;
  437. }
  438. /**
  439. * Add a new template for given type.
  440. *
  441. * @param string $type
  442. * @param string $template
  443. *
  444. * @return void
  445. */
  446. public function addTemplate($type, $template)
  447. {
  448. $this->templates[$type] = $template;
  449. }
  450. /**
  451. * Returns a template for the given type.
  452. *
  453. * @param string $type
  454. * @param string $format
  455. *
  456. * @return string|null
  457. */
  458. public function getTemplate($type, $format = 'html')
  459. {
  460. if (\array_key_exists($type, $this->templates)) {
  461. return $this->templates[$type] . '.' . $format . '.twig';
  462. }
  463. return null;
  464. }
  465. /**
  466. * Returns an array of templates.
  467. *
  468. * @return string[]
  469. */
  470. public function getTemplates()
  471. {
  472. return $this->templates;
  473. }
  474. /**
  475. * Add a new default template for given type.
  476. *
  477. * @param string $type
  478. * @param string $template
  479. *
  480. * @return void
  481. */
  482. public function addDefaultTemplate($type, $template)
  483. {
  484. $this->defaultTemplates[$type] = $template;
  485. }
  486. /**
  487. * Returns a error template for given code.
  488. *
  489. * @param string $type
  490. *
  491. * @return string|null
  492. */
  493. public function getDefaultTemplate($type)
  494. {
  495. if (\array_key_exists($type, $this->defaultTemplates)) {
  496. return $this->defaultTemplates[$type];
  497. }
  498. return null;
  499. }
  500. /**
  501. * Returns a array of default template.
  502. *
  503. * @return string[]
  504. */
  505. public function getDefaultTemplates()
  506. {
  507. return $this->defaultTemplates;
  508. }
  509. /**
  510. * Add a new template for given type.
  511. *
  512. * @param string $excludedTemplate
  513. *
  514. * @return void
  515. */
  516. public function addExcludedTemplate($excludedTemplate)
  517. {
  518. $this->excludedTemplates[] = $excludedTemplate;
  519. }
  520. /**
  521. * @return string[]
  522. */
  523. public function getExcludedTemplates()
  524. {
  525. return $this->excludedTemplates;
  526. }
  527. /**
  528. * Set resource-locator strategy.
  529. *
  530. * @param string $resourceLocatorStrategy
  531. *
  532. * @return void
  533. */
  534. public function setResourceLocatorStrategy($resourceLocatorStrategy)
  535. {
  536. $this->resourceLocatorStrategy = $resourceLocatorStrategy;
  537. }
  538. /**
  539. * Returns resource-locator strategy.
  540. *
  541. * @return string
  542. */
  543. public function getResourceLocatorStrategy()
  544. {
  545. return $this->resourceLocatorStrategy;
  546. }
  547. /**
  548. * @return array
  549. */
  550. public function toArray($depth = null)
  551. {
  552. $res = [];
  553. $res['key'] = $this->getKey();
  554. $res['name'] = $this->getName();
  555. $res['localizations'] = [];
  556. $res['templates'] = $this->getTemplates();
  557. $res['defaultTemplates'] = $this->getDefaultTemplates();
  558. $res['excludedTemplates'] = $this->getExcludedTemplates();
  559. $res['resourceLocator']['strategy'] = $this->getResourceLocatorStrategy();
  560. foreach ($this->getLocalizations() as $localization) {
  561. $res['localizations'][] = $localization->toArray();
  562. }
  563. $thisSecurity = $this->getSecurity();
  564. if (null != $thisSecurity) {
  565. $res['security']['system'] = $thisSecurity->getSystem();
  566. $res['security']['permissionCheck'] = $thisSecurity->getPermissionCheck();
  567. }
  568. $res['segments'] = [];
  569. $segments = $this->getSegments();
  570. if (!empty($segments)) {
  571. foreach ($segments as $segment) {
  572. $res['segments'][] = $segment->toArray();
  573. }
  574. }
  575. $res['theme'] = !$this->theme ? null : $this->theme;
  576. $res['portals'] = [];
  577. foreach ($this->getPortals() as $portal) {
  578. $res['portals'][] = $portal->toArray();
  579. }
  580. $res['navigation'] = [];
  581. $res['navigation']['contexts'] = [];
  582. if ($navigation = $this->getNavigation()) {
  583. foreach ($navigation->getContexts() as $context) {
  584. $res['navigation']['contexts'][] = [
  585. 'key' => $context->getKey(),
  586. 'metadata' => $context->getMetadata(),
  587. ];
  588. }
  589. }
  590. return $res;
  591. }
  592. }