vendor/shopware/core/Framework/Struct/ExtendableTrait.php line 27

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Struct;
  3. use Shopware\Core\Framework\Feature;
  4. trait ExtendableTrait
  5. {
  6.     /**
  7.      * Contains an array of extension structs.
  8.      *
  9.      * @var Struct[]
  10.      */
  11.     protected $extensions = [];
  12.     /**
  13.      * Adds a new extension struct into the class storage.
  14.      * The passed name is used as unique identifier and has to be stored too.
  15.      *
  16.      * @deprecated tag:v6.5.0 - second param $extension will not allow null anymore
  17.      */
  18.     public function addExtension(string $name, ?Struct $extension): void
  19.     {
  20.         if ($extension === null) {
  21.             Feature::triggerDeprecationOrThrow(
  22.                 'v6.5.0.0',
  23.                 'Second parameter `$extension` of method `addExtension()` in `ExtendableTrait` will no accept null anymore in v6.5.0.0.'
  24.             );
  25.             return;
  26.         }
  27.         $this->extensions[$name] = $extension;
  28.     }
  29.     /**
  30.      * Adds a new array struct as extension into the class storage.
  31.      * The passed name is used as unique identifier and has to be stored too.
  32.      */
  33.     public function addArrayExtension(string $name, array $extension): void
  34.     {
  35.         $this->extensions[$name] = new ArrayStruct($extension);
  36.     }
  37.     /**
  38.      * @param Struct[] $extensions
  39.      */
  40.     public function addExtensions(array $extensions): void
  41.     {
  42.         foreach ($extensions as $key => $extension) {
  43.             $this->addExtension($key$extension);
  44.         }
  45.     }
  46.     /**
  47.      * Returns a single extension struct element of this class.
  48.      * The passed name is used as unique identifier.
  49.      */
  50.     public function getExtension(string $name): ?Struct
  51.     {
  52.         return $this->extensions[$name] ?? null;
  53.     }
  54.     public function getExtensionOfType(string $namestring $type): ?Struct
  55.     {
  56.         if ($this->hasExtensionOfType($name$type)) {
  57.             return $this->getExtension($name);
  58.         }
  59.         return null;
  60.     }
  61.     /**
  62.      * Helper function which checks if an associated
  63.      * extension exists.
  64.      */
  65.     public function hasExtension(string $name): bool
  66.     {
  67.         return isset($this->extensions[$name]);
  68.     }
  69.     public function hasExtensionOfType(string $namestring $type): bool
  70.     {
  71.         return $this->hasExtension($name) && \get_class($this->getExtension($name)) === $type;
  72.     }
  73.     /**
  74.      * Returns all stored extension structures of this class.
  75.      * The array has to be an associated array with name and extension instance.
  76.      *
  77.      * @return Struct[]
  78.      */
  79.     public function getExtensions(): array
  80.     {
  81.         return $this->extensions;
  82.     }
  83.     public function setExtensions(array $extensions): void
  84.     {
  85.         $this->extensions $extensions;
  86.     }
  87.     public function removeExtension(string $name): void
  88.     {
  89.         if (isset($this->extensions[$name])) {
  90.             unset($this->extensions[$name]);
  91.         }
  92.     }
  93. }