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

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