![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/Ecombricks/Inventory/Framework/Session/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See LICENSE.txt for license details. */ namespace Ecombricks\Inventory\Framework\Session; /** * Generic session manager */ class Generic extends \Magento\Framework\Session\Generic { /** * Source code * * @var string */ protected $sourceCode; /** * Source keys * * @var array */ protected $sourceKeys; /** * Underscore cache * * @var array */ protected static $inventoryUnderscoreCache = []; /** * Constructor * * @param \Magento\Framework\App\Request\Http $request * @param \Magento\Framework\Session\SidResolverInterface $sidResolver * @param \Magento\Framework\Session\Config\ConfigInterface $sessionConfig * @param \Magento\Framework\Session\SaveHandlerInterface $saveHandler * @param \Magento\Framework\Session\ValidatorInterface $validator * @param \Magento\Framework\Session\StorageInterface $storage * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager * @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory * @param \Magento\Framework\App\State $appState * @param array $sourceKeys * @throws \Magento\Framework\Exception\SessionException * @return void */ public function __construct( \Magento\Framework\App\Request\Http $request, \Magento\Framework\Session\SidResolverInterface $sidResolver, \Magento\Framework\Session\Config\ConfigInterface $sessionConfig, \Magento\Framework\Session\SaveHandlerInterface $saveHandler, \Magento\Framework\Session\ValidatorInterface $validator, \Magento\Framework\Session\StorageInterface $storage, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory, \Magento\Framework\App\State $appState, $sourceKeys = [] ) { $this->sourceKeys = $sourceKeys; parent::__construct( $request, $sidResolver, $sessionConfig, $saveHandler, $validator, $storage, $cookieManager, $cookieMetadataFactory, $appState ); } /** * Underscore * * @param string $name * @return string */ protected function inventoryUnderscore($name) { if (isset(self::$inventoryUnderscoreCache[$name])) { return self::$inventoryUnderscoreCache[$name]; } $result = strtolower(trim(preg_replace('/([A-Z]|[0-9]+)/', "_$1", $name), '_')); self::$inventoryUnderscoreCache[$name] = $result; return $result; } /** * Set source code * * @param string $sourceCode * @return $this */ public function setSourceCode($sourceCode) { $this->sourceCode = $sourceCode; return $this; } /** * Get source code * * @return string */ public function getSourceCode() { return $this->sourceCode; } /** * Unset source code * * @return $this */ public function unsSourceCode() { $this->sourceCode = null; return $this; } /** * Get key by method * * @param string $method * @return string */ protected function getKeyByMethod($method) { return (strlen($method) > 3) ? $this->inventoryUnderscore(substr($method, 3)) : null; } /** * Validate method * * @param string $method * @param array $args * @throws \InvalidArgumentException * @return $this */ protected function validateMethod($method, $args) { $methodPrefix = substr($method, 0, 3); $validMethodPrefixes = ['get', 'set', 'uns', 'has']; if (!in_array($methodPrefix, $validMethodPrefixes)) { throw new \InvalidArgumentException( sprintf('Invalid method %s::%s(%s)', get_class($this), $method, print_r($args, 1)) ); } return $this; } /** * Call * * @param string $method * @param array $args * @return mixed * @throws \InvalidArgumentException */ public function __call($method, $args) { $sourceCode = $this->getSourceCode(); if (!$sourceCode) { return parent::__call($method, $args); } $key = $this->getKeyByMethod($method); if (!$key || !in_array($key, $this->sourceKeys)) { return parent::__call($method, $args); } $this->validateMethod($method, $args); $return = call_user_func_array([ $this->storage, $method.'Source'.$sourceCode ], $args); return $return === $this->storage ? $this : $return; } /** * Get data * * @param string $key * @param boolean $clear * @return mixed */ public function getData($key = '', $clear = false) { $sourceCode = $this->getSourceCode(); if (!$sourceCode) { return parent::getData($key, $clear); } if (!$key || !in_array($key, $this->sourceKeys)) { return parent::getData($key, $clear); } $sourceKey = $key.'_source'.$sourceCode; $data = $this->storage->getData($sourceKey); if ($clear && isset($data)) { $this->storage->unsetData($sourceKey); } return $data; } }