Ferreteria/v0.5/stash/session: Difference between revisions
< Ferreteria | v0.5 | stash
Jump to navigation
Jump to search
(Created page with "{{fmt/title|Ferreteria 0.5: the Session stash}} ==About== This is one possible way of stashing and the one Ferreteria uses, in order to minimize use of cookies. ==Clas...") |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{fmt/title|Ferreteria 0.5: the Session stash}} | {{fmt/title|Ferreteria 0.5: the Session stash}} | ||
==About== | ==About== | ||
This is one possible way of [[../|stashing]] | This is one possible way of [[../|stashing]]. It's the one Ferreteria uses, in order to minimize use of cookies. | ||
The stash array is encoded in JSON and stored in the <code>{{l/ver/sql|user_session}}.Stash</code> field. | |||
==Classes/Methods== | ==Classes/Methods== | ||
* The | * The {{l/ver/class|ns=login/session/card|cCalc|the Session CalcCard class}} can read/write Session Stash data. | ||
==Archive== | |||
'''2022-12-20''' These methods from {{l/ver/class|ns=login/session/card|cCalc|the Session CalcCard class}} ''were'' putting the Stash in the cookie, which... shouldn't be this class's job. I'm saving the code for reference before rewriting it. | |||
<syntaxhighlight lang=php> | |||
/*---- | |||
ACTION: retrieves stash data from cookie and returns it | |||
*/ | |||
static protected function FetchStash() : array { | |||
$osStash = F\csAppCookie::GetValue( | |||
F\caApp::Me()->Config()::WPaths()::StashCookieAffix() | |||
); | |||
if ($osStash->HasIt()) { | |||
$sStash = $osStash->GetIt(); | |||
$arStash = unserialize($sStash); | |||
} else { | |||
$arStash = array(); | |||
} | |||
return $arStash; | |||
} | |||
/*---- | |||
ACTION: stores the given stash data into browser cookie | |||
*/ | |||
static protected function StoreStash(array $ar) : void { | |||
$sStashKey = F\caApp::Me()->Config()::WPaths()::StashCookieAffix(); | |||
#echo 'STASH AR:<pre>'.print_r($ar,TRUE); die(); | |||
if (count($ar) > 0) { | |||
$sStashVal = serialize($ar); | |||
F\csAppCookie::SetValue($sStashKey,$sStashVal); | |||
} else { | |||
F\csAppCookie::ClearValue($sStashKey); | |||
} | |||
} | |||
/*---- | |||
ACTION: sets a value in the local (memory) stash data | |||
*/ | |||
public function SetStashValue(string $sName,string $sValue) : void { | |||
$arStash = $this->FetchStash(); | |||
$sApp = F\caApp::Me()->Config()::WPaths()::AppCookiePrefix(); | |||
$arStash[$sApp][$sName] = $sValue; | |||
#echo 'STASH ARRAY:<pre>'.print_r($arStash,TRUE); | |||
$this->StoreStash($arStash); | |||
} | |||
// NOTE 2020-11-26 NULL values are sometimes getting stashed. Is there a reason for this? | |||
public function GetStashValue(string $sName) : string { | |||
$arStash = $this->FetchStash(); | |||
$sApp = F\caApp::Me()->Config()::WPaths()::AppCookiePrefix(); | |||
$arAppStash = F\csArray::Nz($arStash,$sApp,NULL); | |||
if (!is_array($arAppStash)) { | |||
if (is_null($arAppStash)) { | |||
// not sure why this happens, but treat it like there's no stash | |||
$sValue = NULL; | |||
} else { | |||
$sType = gettype($arAppStash); | |||
$s = "The app stash is supposed to be an array, but is a $sType instead."; | |||
$e = new F\except\cInternal($s); | |||
$e->AddDiagnostic("app key is [$sApp]"); | |||
if (is_scalar($arAppStash)) { | |||
$e->AddDiagnostic("app stash value is [$arAppStash]"); | |||
} | |||
$e->AddDiagnostic('entire stash: '.F\csArray::Render($arStash)); | |||
throw $e; | |||
} | |||
} | |||
$sValue = F\csArray::Nz($arAppStash,$sName); | |||
return is_null($sValue)?'':$sValue; | |||
} | |||
// ACTION: retrieve the value from the stash and remove it | |||
public function PullStashValue(string $sName) : string { | |||
$sValue = $this->GetStashValue($sName); | |||
#$this->ClearStashValue($sName); | |||
return $sValue; | |||
} | |||
// ACTION: delete the given value from the stash | |||
protected function ClearStashValue(string $sName) : void { | |||
$sApp = F\caApp::Me()->Config()::WPaths()::AppCookiePrefix(); | |||
$arStash = $this->FetchStash(); | |||
unset($arStash[$sApp][$sName]); | |||
$this->StoreStash($arStash); | |||
} | |||
</syntaxhighlight> |
Latest revision as of 22:37, 20 December 2022
Ferreteria 0.5: the Session stash
|
About
This is one possible way of stashing. It's the one Ferreteria uses, in order to minimize use of cookies.
The stash array is encoded in JSON and stored in the user_session.Stash
field.
Classes/Methods
- The the Session CalcCard class can read/write Session Stash data.
Archive
2022-12-20 These methods from the Session CalcCard class were putting the Stash in the cookie, which... shouldn't be this class's job. I'm saving the code for reference before rewriting it.
/*----
ACTION: retrieves stash data from cookie and returns it
*/
static protected function FetchStash() : array {
$osStash = F\csAppCookie::GetValue(
F\caApp::Me()->Config()::WPaths()::StashCookieAffix()
);
if ($osStash->HasIt()) {
$sStash = $osStash->GetIt();
$arStash = unserialize($sStash);
} else {
$arStash = array();
}
return $arStash;
}
/*----
ACTION: stores the given stash data into browser cookie
*/
static protected function StoreStash(array $ar) : void {
$sStashKey = F\caApp::Me()->Config()::WPaths()::StashCookieAffix();
#echo 'STASH AR:<pre>'.print_r($ar,TRUE); die();
if (count($ar) > 0) {
$sStashVal = serialize($ar);
F\csAppCookie::SetValue($sStashKey,$sStashVal);
} else {
F\csAppCookie::ClearValue($sStashKey);
}
}
/*----
ACTION: sets a value in the local (memory) stash data
*/
public function SetStashValue(string $sName,string $sValue) : void {
$arStash = $this->FetchStash();
$sApp = F\caApp::Me()->Config()::WPaths()::AppCookiePrefix();
$arStash[$sApp][$sName] = $sValue;
#echo 'STASH ARRAY:<pre>'.print_r($arStash,TRUE);
$this->StoreStash($arStash);
}
// NOTE 2020-11-26 NULL values are sometimes getting stashed. Is there a reason for this?
public function GetStashValue(string $sName) : string {
$arStash = $this->FetchStash();
$sApp = F\caApp::Me()->Config()::WPaths()::AppCookiePrefix();
$arAppStash = F\csArray::Nz($arStash,$sApp,NULL);
if (!is_array($arAppStash)) {
if (is_null($arAppStash)) {
// not sure why this happens, but treat it like there's no stash
$sValue = NULL;
} else {
$sType = gettype($arAppStash);
$s = "The app stash is supposed to be an array, but is a $sType instead.";
$e = new F\except\cInternal($s);
$e->AddDiagnostic("app key is [$sApp]");
if (is_scalar($arAppStash)) {
$e->AddDiagnostic("app stash value is [$arAppStash]");
}
$e->AddDiagnostic('entire stash: '.F\csArray::Render($arStash));
throw $e;
}
}
$sValue = F\csArray::Nz($arAppStash,$sName);
return is_null($sValue)?'':$sValue;
}
// ACTION: retrieve the value from the stash and remove it
public function PullStashValue(string $sName) : string {
$sValue = $this->GetStashValue($sName);
#$this->ClearStashValue($sName);
return $sValue;
}
// ACTION: delete the given value from the stash
protected function ClearStashValue(string $sName) : void {
$sApp = F\caApp::Me()->Config()::WPaths()::AppCookiePrefix();
$arStash = $this->FetchStash();
unset($arStash[$sApp][$sName]);
$this->StoreStash($arStash);
}