vendor/shopware/core/Framework/Context.php line 14

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework;
  3. use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  6. use Shopware\Core\Framework\Api\Context\ContextSource;
  7. use Shopware\Core\Framework\Api\Context\SystemSource;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
  9. use Shopware\Core\Framework\Log\Package;
  10. use Shopware\Core\Framework\Struct\StateAwareTrait;
  11. use Shopware\Core\Framework\Struct\Struct;
  12. use Shopware\Core\System\SalesChannel\Exception\ContextRulesLockedException;
  13. use Symfony\Component\Serializer\Annotation\Ignore;
  14. #[Package('core')]
  15. class Context extends Struct
  16. {
  17.     use StateAwareTrait;
  18.     final public const SYSTEM_SCOPE 'system';
  19.     final public const USER_SCOPE 'user';
  20.     final public const CRUD_API_SCOPE 'crud';
  21.     final public const SKIP_TRIGGER_FLOW 'skipTriggerFlow';
  22.     /**
  23.      * @var non-empty-array<string>
  24.      */
  25.     protected array $languageIdChain;
  26.     protected string $scope self::USER_SCOPE;
  27.     protected bool $rulesLocked false;
  28.     #[Ignore]
  29.     protected $extensions = [];
  30.     /**
  31.      * @param array<string> $languageIdChain
  32.      * @param array<string> $ruleIds
  33.      */
  34.     public function __construct(
  35.         protected ContextSource $source,
  36.         protected array $ruleIds = [],
  37.         protected string $currencyId Defaults::CURRENCY,
  38.         array $languageIdChain = [Defaults::LANGUAGE_SYSTEM],
  39.         protected string $versionId Defaults::LIVE_VERSION,
  40.         protected float $currencyFactor 1.0,
  41.         protected bool $considerInheritance false,
  42.         /**
  43.          * @see CartPrice::TAX_STATE_GROSS, CartPrice::TAX_STATE_NET, CartPrice::TAX_STATE_FREE
  44.          */
  45.         protected string $taxState CartPrice::TAX_STATE_GROSS,
  46.         protected CashRoundingConfig $rounding = new CashRoundingConfig(20.01true)
  47.     ) {
  48.         if ($source instanceof SystemSource) {
  49.             $this->scope self::SYSTEM_SCOPE;
  50.         }
  51.         if (empty($languageIdChain)) {
  52.             throw new \InvalidArgumentException('Argument languageIdChain must not be empty');
  53.         }
  54.         /** @var non-empty-array<string> $chain */
  55.         $chain array_keys(array_flip(array_filter($languageIdChain)));
  56.         $this->languageIdChain $chain;
  57.     }
  58.     /**
  59.      * @internal
  60.      */
  61.     public static function createDefaultContext(?ContextSource $source null): self
  62.     {
  63.         $source ??= new SystemSource();
  64.         return new self($source);
  65.     }
  66.     public function getSource(): ContextSource
  67.     {
  68.         return $this->source;
  69.     }
  70.     public function getVersionId(): string
  71.     {
  72.         return $this->versionId;
  73.     }
  74.     public function getLanguageId(): string
  75.     {
  76.         return $this->languageIdChain[0];
  77.     }
  78.     public function getCurrencyId(): string
  79.     {
  80.         return $this->currencyId;
  81.     }
  82.     public function getCurrencyFactor(): float
  83.     {
  84.         return $this->currencyFactor;
  85.     }
  86.     /**
  87.      * @return array<string>
  88.      */
  89.     public function getRuleIds(): array
  90.     {
  91.         return $this->ruleIds;
  92.     }
  93.     /**
  94.      * @return non-empty-array<string>
  95.      */
  96.     public function getLanguageIdChain(): array
  97.     {
  98.         return $this->languageIdChain;
  99.     }
  100.     public function createWithVersionId(string $versionId): self
  101.     {
  102.         $context = new self(
  103.             $this->source,
  104.             $this->ruleIds,
  105.             $this->currencyId,
  106.             $this->languageIdChain,
  107.             $versionId,
  108.             $this->currencyFactor,
  109.             $this->considerInheritance,
  110.             $this->taxState,
  111.             $this->rounding
  112.         );
  113.         $context->scope $this->scope;
  114.         foreach ($this->getExtensions() as $key => $extension) {
  115.             $context->addExtension($key$extension);
  116.         }
  117.         return $context;
  118.     }
  119.     /**
  120.      * @template TReturn of mixed
  121.      *
  122.      * @param callable(Context): TReturn $callback
  123.      *
  124.      * @return TReturn the return value of the provided callback function
  125.      */
  126.     public function scope(string $scope, callable $callback)
  127.     {
  128.         $currentScope $this->getScope();
  129.         $this->scope $scope;
  130.         try {
  131.             $result $callback($this);
  132.         } finally {
  133.             $this->scope $currentScope;
  134.         }
  135.         return $result;
  136.     }
  137.     public function getScope(): string
  138.     {
  139.         return $this->scope;
  140.     }
  141.     public function considerInheritance(): bool
  142.     {
  143.         return $this->considerInheritance;
  144.     }
  145.     public function setConsiderInheritance(bool $considerInheritance): void
  146.     {
  147.         $this->considerInheritance $considerInheritance;
  148.     }
  149.     public function getTaxState(): string
  150.     {
  151.         return $this->taxState;
  152.     }
  153.     public function setTaxState(string $taxState): void
  154.     {
  155.         $this->taxState $taxState;
  156.     }
  157.     public function isAllowed(string $privilege): bool
  158.     {
  159.         if ($this->source instanceof AdminApiSource) {
  160.             return $this->source->isAllowed($privilege);
  161.         }
  162.         return true;
  163.     }
  164.     /**
  165.      * @param array<string> $ruleIds
  166.      */
  167.     public function setRuleIds(array $ruleIds): void
  168.     {
  169.         if ($this->rulesLocked) {
  170.             throw new ContextRulesLockedException();
  171.         }
  172.         $this->ruleIds array_filter(array_values($ruleIds));
  173.     }
  174.     /**
  175.      * @template TReturn of mixed
  176.      *
  177.      * @param callable(Context): TReturn $function
  178.      *
  179.      * @return TReturn
  180.      */
  181.     public function enableInheritance(callable $function)
  182.     {
  183.         $previous $this->considerInheritance;
  184.         $this->considerInheritance true;
  185.         $result $function($this);
  186.         $this->considerInheritance $previous;
  187.         return $result;
  188.     }
  189.     /**
  190.      * @template TReturn of mixed
  191.      *
  192.      * @param callable(Context): TReturn $function
  193.      *
  194.      * @return TReturn
  195.      */
  196.     public function disableInheritance(callable $function)
  197.     {
  198.         $previous $this->considerInheritance;
  199.         $this->considerInheritance false;
  200.         $result $function($this);
  201.         $this->considerInheritance $previous;
  202.         return $result;
  203.     }
  204.     public function getApiAlias(): string
  205.     {
  206.         return 'context';
  207.     }
  208.     public function getRounding(): CashRoundingConfig
  209.     {
  210.         return $this->rounding;
  211.     }
  212.     public function setRounding(CashRoundingConfig $rounding): void
  213.     {
  214.         $this->rounding $rounding;
  215.     }
  216.     public function lockRules(): void
  217.     {
  218.         $this->rulesLocked true;
  219.     }
  220. }