vendor/pimcore/pimcore/lib/HttpKernel/BundleCollection/BundleCollection.php line 153

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\HttpKernel\BundleCollection;
  16. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  17. class BundleCollection
  18. {
  19.     /**
  20.      * @var ItemInterface[]
  21.      */
  22.     private $items = [];
  23.     /**
  24.      * Adds a collection item
  25.      *
  26.      * @param ItemInterface $item
  27.      *
  28.      * @return self
  29.      */
  30.     public function add(ItemInterface $item): self
  31.     {
  32.         $identifier $item->getBundleIdentifier();
  33.         // a bundle can only be registered once
  34.         if ($this->hasItem($identifier)) {
  35.             return $this;
  36.         }
  37.         $this->items[$identifier] = $item;
  38.         // handle DependentBundleInterface by adding a bundle's dependencies to the collection - dependencies
  39.         // are added AFTER the item was added to the collection to avoid circular reference loops, but the
  40.         // sort order can be influenced by specifying a priority on the item
  41.         $item->registerDependencies($this);
  42.         return $this;
  43.     }
  44.     /**
  45.      * Returns a collection item by identifier
  46.      *
  47.      * @param string $identifier
  48.      *
  49.      * @return ItemInterface
  50.      */
  51.     public function getItem(string $identifier): ItemInterface
  52.     {
  53.         if (!$this->hasItem($identifier)) {
  54.             throw new \InvalidArgumentException(sprintf('Bundle "%s" is not registered'$identifier));
  55.         }
  56.         return $this->items[$identifier];
  57.     }
  58.     /**
  59.      * Checks if a specific item is registered
  60.      *
  61.      * @param string $identifier
  62.      *
  63.      * @return bool
  64.      */
  65.     public function hasItem(string $identifier)
  66.     {
  67.         return isset($this->items[$identifier]);
  68.     }
  69.     /**
  70.      * Returns all collection items ordered by priority and optionally filtered by matching environment
  71.      *
  72.      * @param string|null $environment
  73.      *
  74.      * @return ItemInterface[]
  75.      */
  76.     public function getItems(string $environment null): array
  77.     {
  78.         $items array_values($this->items);
  79.         if (null !== $environment) {
  80.             $items array_filter($items, function (ItemInterface $item) use ($environment) {
  81.                 return $item->matchesEnvironment($environment);
  82.             });
  83.         }
  84.         usort($items, function (ItemInterface $aItemInterface $b) {
  85.             if ($a->getPriority() === $b->getPriority()) {
  86.                 return 0;
  87.             }
  88.             return ($a->getPriority() > $b->getPriority()) ? -1;
  89.         });
  90.         return $items;
  91.     }
  92.     /**
  93.      * Returns all bundle identifiers
  94.      *
  95.      * @return array
  96.      */
  97.     public function getIdentifiers(string $environment null): array
  98.     {
  99.         return array_map(function (ItemInterface $item) {
  100.             return $item->getBundleIdentifier();
  101.         }, $this->getItems($environment));
  102.     }
  103.     /**
  104.      * Get bundles matching environment ordered by priority
  105.      *
  106.      * @param string $environment
  107.      *
  108.      * @return BundleInterface[]
  109.      */
  110.     public function getBundles(string $environment): array
  111.     {
  112.         return array_map(function (ItemInterface $item) {
  113.             return $item->getBundle();
  114.         }, $this->getItems($environment));
  115.     }
  116.     /**
  117.      * Adds a bundle
  118.      *
  119.      * @param BundleInterface|string $bundle
  120.      * @param int $priority
  121.      * @param array $environments
  122.      *
  123.      * @throws \InvalidArgumentException
  124.      *
  125.      * @return self
  126.      */
  127.     public function addBundle($bundleint $priority 0, array $environments = []): self
  128.     {
  129.         if ($bundle instanceof BundleInterface) {
  130.             $item = new Item($bundle$priority$environments);
  131.         } elseif (is_string($bundle)) {
  132.             $item = new LazyLoadedItem($bundle$priority$environments);
  133.         } else {
  134.             throw new \InvalidArgumentException('Bundle must be either an instance of BundleInterface or a string containing the bundle class name');
  135.         }
  136.         return $this->add($item);
  137.     }
  138.     /**
  139.      * Adds a collection of bundles with the same priority and environments
  140.      *
  141.      * @param BundleInterface[]|string[] $bundles
  142.      * @param int $priority
  143.      * @param array $environments
  144.      *
  145.      * @return BundleCollection
  146.      */
  147.     public function addBundles(array $bundlesint $priority 0, array $environments = []): self
  148.     {
  149.         foreach ($bundles as $bundle) {
  150.             $this->addBundle($bundle$priority$environments);
  151.         }
  152.         return $this;
  153.     }
  154. }