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

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