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

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