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

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