Source of file RequestVarProgressStore.php
Size: 3,346 Bytes - Last Modified: 2021-12-24T06:40:50+00:00
/var/www/docs.ssmods.com/process/src/src/Model/Store/RequestVarProgressStore.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 | <?php namespace CodeCraft\Pathfinder\Model\Store; use Psr\Log\LoggerInterface; use SilverStripe\Control\Controller; use SilverStripe\Core\Injector\Injector; /** * Can be used to pass progress on to the next step via the URL */ class RequestVarProgressStore extends ProgressStore { /** * @var string */ private static $progress_var_name = 'progress'; /** * @var string */ protected $encodedProgress; /** * {@inheritDoc} */ public function __construct() { $stored = $this->getDecodedStore(); $entries = []; foreach ($stored as $entry) { $entries[] = ProgressEntry::create($entry); } parent::set($entries); } /** * @param array $store * * r@eturn string */ public static function encode($store) { return base64_encode(json_encode($store)); } /** * @param string $encoded An encoded store * @return array */ public static function decode($encoded) { return json_decode(base64_decode($encoded), true); } /** * @return string */ public function getProgressVarName() { return $this->config()->get('progress_var_name'); } /** * @return string */ public function getEncodedProgress() { if ($this->encodedProgress) { return $this->encodedProgress; } // Fallback to the request $request = Controller::curr()->getRequest(); // Accept query variable from GET or POST $this->encodedProgress = urldecode($request->requestVar($this->getProgressVarName())); return $this->encodedProgress; } /** * Gzip, Base64 and URL encode the store * * @return string */ public function getEncodedStore() { return static::encode($this->toArray()); } /** * @return array */ public function getDecodedStore() { $encoded = $this->getEncodedProgress(); if (!$encoded) { return []; } $store = static::decode($encoded); if (!is_array($stored)) { Injector::inst()->get(LoggerInterface::class) ->warning(sprintf('Unexpected value found in request for "%s::decode()"', self::class)); return []; } return $store; } /** * @param array $vars * @param string $url * * @return string */ public function mergeRequestVarsWithURL($vars, $url) { $queryString = parse_url($url, PHP_URL_QUERY); parse_str($queryString, $current); $output = array_merge($current, $vars); // Match the query segment of the url preg_match('/\?(.*?)(?:#|$)/s', $url, $matches); $existing = array_key_exists(1, $matches) ? $matches[1] : ''; if ($existing) { // Insert it into the url return str_replace($existing, http_build_query($output), $url); } return sprintf('%s?%s', $url, http_build_query($output)); } /** * {@inheritDoc} */ public function augmentURL($url, $form = null) { $url = parent::augmentURL($url); return $this->mergeRequestVarsWithURL(['progress' => $this->getEncodedStore()], $url); } } |