vendor/sulu/sulu/src/Sulu/Component/Webspace/Manager/WebspaceCollection.php line 150

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\Manager;
  11. use Sulu\Component\Webspace\Portal;
  12. use Sulu\Component\Webspace\PortalInformation;
  13. use Sulu\Component\Webspace\Webspace;
  14. use Symfony\Component\Config\Resource\FileResource;
  15. /**
  16.  * A collection of all webspaces and portals in a specific sulu installation.
  17.  *
  18.  * @implements \IteratorAggregate<Webspace>
  19.  */
  20. class WebspaceCollection implements \IteratorAggregate
  21. {
  22.     /**
  23.      * All the webspaces in a specific sulu installation.
  24.      *
  25.      * @var Webspace[]
  26.      */
  27.     private $webspaces;
  28.     /**
  29.      * All the portals in a specific sulu installation.
  30.      *
  31.      * @var Portal[]
  32.      */
  33.     private $portals;
  34.     /**
  35.      * The portals of this specific sulu installation, prefiltered by the environment and url.
  36.      *
  37.      * @var array
  38.      */
  39.     private $portalInformations;
  40.     /**
  41.      * Contains all the resources, which where used to build this collection.
  42.      * Is required by the Symfony CacheConfig-Component.
  43.      *
  44.      * @var FileResource[]
  45.      */
  46.     private $resources;
  47.     public function __construct(array $webspaces = [])
  48.     {
  49.         $this->webspaces $webspaces;
  50.     }
  51.     /**
  52.      * Adds a new FileResource, which is required to determine if the cache is fresh.
  53.      */
  54.     public function addResource(FileResource $resource)
  55.     {
  56.         $this->resources[] = $resource;
  57.     }
  58.     /**
  59.      * Returns the resources used to build this collection.
  60.      *
  61.      * @return array The resources build to use this collection
  62.      */
  63.     public function getResources()
  64.     {
  65.         return $this->resources;
  66.     }
  67.     /**
  68.      * Returns the portal with the given index.
  69.      *
  70.      * @param string $key The index of the portal
  71.      *
  72.      * @return Portal|null
  73.      */
  74.     public function getPortal($key)
  75.     {
  76.         return \array_key_exists($key$this->portals) ? $this->portals[$key] : null;
  77.     }
  78.     /**
  79.      * Returns the portal informations for the given environment.
  80.      *
  81.      * @param string $environment The environment to deliver
  82.      * @param array|null $types Defines which type of portals are requested (null for all)
  83.      *
  84.      * @return PortalInformation[]
  85.      */
  86.     public function getPortalInformations($environment$types null)
  87.     {
  88.         if (!isset($this->portalInformations[$environment])) {
  89.             throw new \InvalidArgumentException(\sprintf(
  90.                 'Unknown portal environment "%s"'$environment
  91.             ));
  92.         }
  93.         if (null === $types) {
  94.             return $this->portalInformations[$environment];
  95.         }
  96.         return \array_filter(
  97.             $this->portalInformations[$environment],
  98.             function(PortalInformation $portalInformation) use ($types) {
  99.                 return \in_array($portalInformation->getType(), $types);
  100.             }
  101.         );
  102.     }
  103.     /**
  104.      * Returns the webspace with the given key.
  105.      *
  106.      * @param string $key The key of the webspace
  107.      *
  108.      * @return Webspace|null
  109.      */
  110.     public function getWebspace($key)
  111.     {
  112.         return \array_key_exists($key$this->webspaces) ? $this->webspaces[$key] : null;
  113.     }
  114.     /**
  115.      * Returns the length of the collection.
  116.      *
  117.      * @return int
  118.      */
  119.     public function length()
  120.     {
  121.         return \count($this->webspaces);
  122.     }
  123.     #[\ReturnTypeWillChange]
  124.     public function getIterator()
  125.     {
  126.         return new \ArrayIterator($this->webspaces);
  127.     }
  128.     /**
  129.      * Returns the content of these portals as array.
  130.      *
  131.      * @return array
  132.      */
  133.     public function toArray()
  134.     {
  135.         $collection = [];
  136.         $webspaces = [];
  137.         foreach ($this->webspaces as $webspace) {
  138.             $webspaces[] = $webspace->toArray();
  139.         }
  140.         $portalInformations = [];
  141.         foreach ($this->portalInformations as $environment => $environmentPortalInformations) {
  142.             $portalInformations[$environment] = [];
  143.             foreach ($environmentPortalInformations as $environmentPortalInformation) {
  144.                 $portalInformations[$environment][$environmentPortalInformation->getUrl()] = $environmentPortalInformation->toArray();
  145.             }
  146.         }
  147.         $collection['webspaces'] = $webspaces;
  148.         $collection['portalInformations'] = $portalInformations;
  149.         return $collection;
  150.     }
  151.     /**
  152.      * @param Webspace[] $webspaces
  153.      */
  154.     public function setWebspaces($webspaces)
  155.     {
  156.         $this->webspaces $webspaces;
  157.     }
  158.     /**
  159.      * @return Webspace[]
  160.      */
  161.     public function getWebspaces()
  162.     {
  163.         return $this->webspaces;
  164.     }
  165.     /**
  166.      * Returns all the portals of this collection.
  167.      *
  168.      * @return Portal[]
  169.      */
  170.     public function getPortals()
  171.     {
  172.         return $this->portals;
  173.     }
  174.     /**
  175.      * Sets the portals for this collection.
  176.      *
  177.      * @param Portal[] $portals
  178.      */
  179.     public function setPortals($portals)
  180.     {
  181.         $this->portals $portals;
  182.     }
  183.     /**
  184.      * Sets the portal Information for this collection.
  185.      *
  186.      * @param array $portalInformations
  187.      */
  188.     public function setPortalInformations($portalInformations)
  189.     {
  190.         $this->portalInformations $portalInformations;
  191.     }
  192. }