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

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