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.     public function setKey($key)
  112.     {
  113.         $this->key $key;
  114.     }
  115.     /**
  116.      * Returns the key of the webspace.
  117.      *
  118.      * @return string
  119.      */
  120.     public function getKey()
  121.     {
  122.         return $this->key;
  123.     }
  124.     /**
  125.      * Adds a localization to the webspace.
  126.      */
  127.     public function addLocalization(Localization $localization)
  128.     {
  129.         $this->localizations[] = $localization;
  130.         if ($localization->isDefault()) {
  131.             $this->setDefaultLocalization($localization);
  132.         }
  133.         if ($localization->isXDefault(false)) {
  134.             $this->xDefaultLocalization $localization;
  135.         }
  136.     }
  137.     /**
  138.      * Returns the localizations of this webspace.
  139.      *
  140.      * @param Localization[] $localizations
  141.      */
  142.     public function setLocalizations($localizations)
  143.     {
  144.         $this->localizations $localizations;
  145.     }
  146.     /**
  147.      * Returns the localizations of this webspace.
  148.      *
  149.      * @return Localization[]
  150.      */
  151.     public function getLocalizations()
  152.     {
  153.         return $this->localizations;
  154.     }
  155.     /**
  156.      * Returns a list of all localizations and sublocalizations.
  157.      *
  158.      * @return Localization[]
  159.      */
  160.     public function getAllLocalizations()
  161.     {
  162.         $localizations = [];
  163.         foreach ($this->getLocalizations() as $child) {
  164.             $localizations[] = $child;
  165.             $localizations \array_merge($localizations$child->getAllLocalizations());
  166.         }
  167.         return $localizations;
  168.     }
  169.     /**
  170.      * Returns the localization object for a given localization string.
  171.      *
  172.      * @param string $localization
  173.      *
  174.      * @return Localization|null
  175.      */
  176.     public function getLocalization($localization)
  177.     {
  178.         $localizations $this->getLocalizations();
  179.         if (!empty($localizations)) {
  180.             foreach ($localizations as $webspaceLocalization) {
  181.                 $result $webspaceLocalization->findLocalization($localization);
  182.                 if ($result) {
  183.                     return $result;
  184.                 }
  185.             }
  186.         }
  187.         return;
  188.     }
  189.     /**
  190.      * Sets the default localization for this webspace.
  191.      *
  192.      * @param Localization $defaultLocalization
  193.      */
  194.     public function setDefaultLocalization($defaultLocalization)
  195.     {
  196.         $this->defaultLocalization $defaultLocalization;
  197.         if (!$this->getXDefaultLocalization()) {
  198.             $this->xDefaultLocalization $defaultLocalization;
  199.         }
  200.     }
  201.     /**
  202.      * Returns the default localization for this webspace.
  203.      *
  204.      * @return Localization
  205.      */
  206.     public function getDefaultLocalization()
  207.     {
  208.         if (!$this->defaultLocalization) {
  209.             return $this->localizations[0];
  210.         }
  211.         return $this->defaultLocalization;
  212.     }
  213.     /**
  214.      * Returns the x-default localization for this webspace.
  215.      *
  216.      * @return Localization
  217.      */
  218.     public function getXDefaultLocalization()
  219.     {
  220.         if (!$this->xDefaultLocalization) {
  221.             return $this->localizations[0];
  222.         }
  223.         return $this->xDefaultLocalization;
  224.     }
  225.     /**
  226.      * Sets the name of the webspace.
  227.      *
  228.      * @param string $name
  229.      */
  230.     public function setName($name)
  231.     {
  232.         $this->name $name;
  233.     }
  234.     /**
  235.      * Returns the name of the webspace.
  236.      *
  237.      * @return string
  238.      */
  239.     public function getName()
  240.     {
  241.         return $this->name;
  242.     }
  243.     /**
  244.      * Adds a portal to the webspace.
  245.      */
  246.     public function addPortal(Portal $portal)
  247.     {
  248.         $this->portals[] = $portal;
  249.     }
  250.     /**
  251.      * Sets the portals of this webspace.
  252.      *
  253.      * @param Portal[] $portals
  254.      */
  255.     public function setPortals($portals)
  256.     {
  257.         $this->portals $portals;
  258.     }
  259.     /**
  260.      * Returns the portals of this webspace.
  261.      *
  262.      * @return Portal[]
  263.      */
  264.     public function getPortals()
  265.     {
  266.         return $this->portals;
  267.     }
  268.     /**
  269.      * Adds a segment to the webspace.
  270.      */
  271.     public function addSegment(Segment $segment)
  272.     {
  273.         $this->segments[] = $segment;
  274.         if ($segment->isDefault()) {
  275.             $this->setDefaultSegment($segment);
  276.         }
  277.     }
  278.     /**
  279.      * Sets the segments of this webspace.
  280.      *
  281.      * @param Segment[] $segments
  282.      */
  283.     public function setSegments($segments)
  284.     {
  285.         $this->segments $segments;
  286.     }
  287.     /**
  288.      * Returns the segments of this webspace.
  289.      *
  290.      * @return Segment[]
  291.      */
  292.     public function getSegments()
  293.     {
  294.         return $this->segments;
  295.     }
  296.     public function getSegment(string $segmentKey): ?Segment
  297.     {
  298.         foreach ($this->segments as $segment) {
  299.             if ($segment->getKey() === $segmentKey) {
  300.                 return $segment;
  301.             }
  302.         }
  303.         return null;
  304.     }
  305.     /**
  306.      * Sets the default segment of this webspace.
  307.      *
  308.      * @param Segment $defaultSegment
  309.      */
  310.     public function setDefaultSegment($defaultSegment)
  311.     {
  312.         $this->defaultSegment $defaultSegment;
  313.     }
  314.     /**
  315.      * Returns the default segment for this webspace.
  316.      *
  317.      * @return Segment
  318.      */
  319.     public function getDefaultSegment()
  320.     {
  321.         return $this->defaultSegment;
  322.     }
  323.     /**
  324.      * Sets the theme for this portal.
  325.      *
  326.      * @param string|null $theme this parameter is options
  327.      */
  328.     public function setTheme($theme null)
  329.     {
  330.         $this->theme $theme;
  331.     }
  332.     /**
  333.      * Returns the theme for this portal.
  334.      *
  335.      * @return string
  336.      */
  337.     public function getTheme()
  338.     {
  339.         return $this->theme;
  340.     }
  341.     /**
  342.      * Sets the security system.
  343.      *
  344.      * @param Security|null $security
  345.      */
  346.     public function setSecurity($security)
  347.     {
  348.         $this->security $security;
  349.     }
  350.     /**
  351.      * Returns the security system.
  352.      *
  353.      * @return Security|null
  354.      */
  355.     public function getSecurity()
  356.     {
  357.         return $this->security;
  358.     }
  359.     public function hasWebsiteSecurity()
  360.     {
  361.         $security $this->getSecurity();
  362.         if (!$security) {
  363.             return false;
  364.         }
  365.         return null !== $security->getSystem() && $security->getPermissionCheck();
  366.     }
  367.     /**
  368.      * @return Navigation
  369.      */
  370.     public function getNavigation()
  371.     {
  372.         return $this->navigation;
  373.     }
  374.     /**
  375.      * @param Navigation $navigation
  376.      */
  377.     public function setNavigation($navigation)
  378.     {
  379.         $this->navigation $navigation;
  380.     }
  381.     /**
  382.      * Returns false if domain not exists in webspace.
  383.      *
  384.      * @param string $domain
  385.      * @param string $environment
  386.      * @param string $locale
  387.      *
  388.      * @return bool
  389.      *
  390.      * @throws Exception\EnvironmentNotFoundException
  391.      */
  392.     public function hasDomain($domain$environment$locale null)
  393.     {
  394.         $localizationParts \explode('_'$locale);
  395.         $language $localizationParts[0];
  396.         $country = isset($localizationParts[1]) ? $localizationParts[1] : '';
  397.         foreach ($this->getPortals() as $portal) {
  398.             foreach ($portal->getEnvironment($environment)->getUrls() as $url) {
  399.                 $host \parse_url('//' $url->getUrl())['host'];
  400.                 if ((null === $locale || $url->isValidLocale($language$country))
  401.                     && ($host === $domain || '{host}' === $host)
  402.                 ) {
  403.                     return true;
  404.                 }
  405.             }
  406.         }
  407.         return false;
  408.     }
  409.     /**
  410.      * Add a new template for given type.
  411.      *
  412.      * @param string $type
  413.      * @param string $template
  414.      */
  415.     public function addTemplate($type$template)
  416.     {
  417.         $this->templates[$type] = $template;
  418.     }
  419.     /**
  420.      * Returns a template for the given type.
  421.      *
  422.      * @param string $type
  423.      * @param string $format
  424.      *
  425.      * @return string|null
  426.      */
  427.     public function getTemplate($type$format 'html')
  428.     {
  429.         if (\array_key_exists($type$this->templates)) {
  430.             return $this->templates[$type] . '.' $format '.twig';
  431.         }
  432.         return;
  433.     }
  434.     /**
  435.      * Returns an array of templates.
  436.      *
  437.      * @return string[]
  438.      */
  439.     public function getTemplates()
  440.     {
  441.         return $this->templates;
  442.     }
  443.     /**
  444.      * Add a new default template for given type.
  445.      *
  446.      * @param string $type
  447.      * @param string $template
  448.      */
  449.     public function addDefaultTemplate($type$template)
  450.     {
  451.         $this->defaultTemplates[$type] = $template;
  452.     }
  453.     /**
  454.      * Returns a error template for given code.
  455.      *
  456.      * @param string $type
  457.      *
  458.      * @return string|null
  459.      */
  460.     public function getDefaultTemplate($type)
  461.     {
  462.         if (\array_key_exists($type$this->defaultTemplates)) {
  463.             return $this->defaultTemplates[$type];
  464.         }
  465.         return;
  466.     }
  467.     /**
  468.      * Returns a array of default template.
  469.      *
  470.      * @return string[]
  471.      */
  472.     public function getDefaultTemplates()
  473.     {
  474.         return $this->defaultTemplates;
  475.     }
  476.     /**
  477.      * Add a new template for given type.
  478.      *
  479.      * @param string $excludedTemplate
  480.      */
  481.     public function addExcludedTemplate($excludedTemplate)
  482.     {
  483.         $this->excludedTemplates[] = $excludedTemplate;
  484.     }
  485.     /**
  486.      * @return string[]
  487.      */
  488.     public function getExcludedTemplates()
  489.     {
  490.         return $this->excludedTemplates;
  491.     }
  492.     /**
  493.      * Set resource-locator strategy.
  494.      *
  495.      * @param string $resourceLocatorStrategy
  496.      */
  497.     public function setResourceLocatorStrategy($resourceLocatorStrategy)
  498.     {
  499.         $this->resourceLocatorStrategy $resourceLocatorStrategy;
  500.     }
  501.     /**
  502.      * Returns resource-locator strategy.
  503.      *
  504.      * @return string
  505.      */
  506.     public function getResourceLocatorStrategy()
  507.     {
  508.         return $this->resourceLocatorStrategy;
  509.     }
  510.     public function toArray($depth null)
  511.     {
  512.         $res = [];
  513.         $res['key'] = $this->getKey();
  514.         $res['name'] = $this->getName();
  515.         $res['localizations'] = [];
  516.         $res['templates'] = $this->getTemplates();
  517.         $res['defaultTemplates'] = $this->getDefaultTemplates();
  518.         $res['excludedTemplates'] = $this->getExcludedTemplates();
  519.         $res['resourceLocator']['strategy'] = $this->getResourceLocatorStrategy();
  520.         foreach ($this->getLocalizations() as $localization) {
  521.             $res['localizations'][] = $localization->toArray();
  522.         }
  523.         $thisSecurity $this->getSecurity();
  524.         if (null != $thisSecurity) {
  525.             $res['security']['system'] = $thisSecurity->getSystem();
  526.             $res['security']['permissionCheck'] = $thisSecurity->getPermissionCheck();
  527.         }
  528.         $res['segments'] = [];
  529.         $segments $this->getSegments();
  530.         if (!empty($segments)) {
  531.             foreach ($segments as $segment) {
  532.                 $res['segments'][] = $segment->toArray();
  533.             }
  534.         }
  535.         $res['theme'] = !$this->theme null $this->theme;
  536.         $res['portals'] = [];
  537.         foreach ($this->getPortals() as $portal) {
  538.             $res['portals'][] = $portal->toArray();
  539.         }
  540.         $res['navigation'] = [];
  541.         $res['navigation']['contexts'] = [];
  542.         if ($navigation $this->getNavigation()) {
  543.             foreach ($navigation->getContexts() as $context) {
  544.                 $res['navigation']['contexts'][] = [
  545.                     'key' => $context->getKey(),
  546.                     'metadata' => $context->getMetadata(),
  547.                 ];
  548.             }
  549.         }
  550.         return $res;
  551.     }
  552. }