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.         try {
  150.             $result $callback($this);
  151.         } finally {
  152.             $this->scope $currentScope;
  153.         }
  154.         return $result;
  155.     }
  156.     public function getScope(): string
  157.     {
  158.         return $this->scope;
  159.     }
  160.     public function getCurrencyPrecision(): int
  161.     {
  162.         return $this->currencyPrecision;
  163.     }
  164.     public function considerInheritance(): bool
  165.     {
  166.         return $this->considerInheritance;
  167.     }
  168.     public function setConsiderInheritance(bool $considerInheritance): void
  169.     {
  170.         $this->considerInheritance $considerInheritance;
  171.     }
  172.     public function getTaxState(): string
  173.     {
  174.         return $this->taxState;
  175.     }
  176.     public function setTaxState(string $taxState): void
  177.     {
  178.         $this->taxState $taxState;
  179.     }
  180.     public function disableCache(callable $function)
  181.     {
  182.         $previous $this->useCache;
  183.         $this->useCache false;
  184.         $result $function($this);
  185.         $this->useCache $previous;
  186.         return $result;
  187.     }
  188.     public function getUseCache(): bool
  189.     {
  190.         return $this->useCache;
  191.     }
  192.     /**
  193.      * @param string $resource - @deprecated tag:v6.4.0 - Resources and privileges are merged in 6.3.0, new pattern: `product:create`
  194.      */
  195.     public function isAllowed(string $privilege, ?string $resource null): bool
  196.     {
  197.         // @deprecated tag:v6.4.0 - Fallback will be removed
  198.         if ($resource !== null) {
  199.             // old pattern provided ->isAllowed('product', 'write');
  200.             $privilege implode(':', [$privilege$resource]);
  201.         }
  202.         if ($this->source instanceof AdminApiSource) {
  203.             return $this->source->isAllowed($privilege);
  204.         }
  205.         return true;
  206.     }
  207.     public function setRuleIds(array $ruleIds): void
  208.     {
  209.         $this->ruleIds array_filter(array_values($ruleIds));
  210.     }
  211.     public function enableCache(callable $function)
  212.     {
  213.         $previous $this->useCache;
  214.         $this->useCache true;
  215.         $result $function($this);
  216.         $this->useCache $previous;
  217.         return $result;
  218.     }
  219.     public function enableInheritance(callable $function)
  220.     {
  221.         $previous $this->considerInheritance;
  222.         $this->considerInheritance true;
  223.         $result $function($this);
  224.         $this->considerInheritance $previous;
  225.         return $result;
  226.     }
  227.     public function disableInheritance(callable $function)
  228.     {
  229.         $previous $this->considerInheritance;
  230.         $this->considerInheritance false;
  231.         $result $function($this);
  232.         $this->considerInheritance $previous;
  233.         return $result;
  234.     }
  235.     public function getApiAlias(): string
  236.     {
  237.         return 'context';
  238.     }
  239. }