Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Add a basic singleton to save on queries.
James Cole73512b036586
diff --git a/app/Support/Amount.php b/app/Support/Amount.phpindex 57377bc672..e7a7d41ecf 100644--- a/app/Support/Amount.php+++ b/app/Support/Amount.php@@ -29,9 +29,9 @@ use FireflyIII\Models\TransactionCurrency;use FireflyIII\Models\TransactionJournal;use FireflyIII\Models\UserGroup;use FireflyIII\Support\Facades\Preferences;+use FireflyIII\Support\Singleton\PreferencesSingleton;use FireflyIII\User;use Illuminate\Support\Collection;-use Illuminate\Support\Facades\Log;use NumberFormatter;/**@@ -58,15 +58,15 @@ class Amount*/public function formatFlat(string $symbol, int $decimalPlaces, string $amount, ?bool $coloured = null): string{- $locale = app('steam')->getLocale();- $rounded = app('steam')->bcround($amount, $decimalPlaces);+ $locale = app('steam')->getLocale();+ $rounded = app('steam')->bcround($amount, $decimalPlaces);$coloured ??= true;- $fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);+ $fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);$fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, $symbol);$fmt->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces);$fmt->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces);- $result = (string)$fmt->format((float)$rounded); // intentional float+ $result = (string)$fmt->format((float)$rounded); // intentional floatif (true === $coloured) {if (1 === bccomp((string)$rounded, '0')) {@@ -116,11 +116,20 @@ class Amountpublic function convertToPrimary(?User $user = null): bool{- if (!$user instanceof User) {- return true === Preferences::get('convert_to_primary', false)->data && true === config('cer.enabled');- }+ $instance = PreferencesSingleton::getInstance();+ $pref = $instance->getPreference('convert_to_primary');+ if (null === $pref) {+ if (!$user instanceof User) {+ $res = true === Preferences::get('convert_to_primary', false)->data && true === config('cer.enabled');+ $instance->setPreference('convert_to_primary', $res);+ return $res;+ }- return true === Preferences::getForUser($user, 'convert_to_primary', false)->data && true === config('cer.enabled');+ $res = true === Preferences::getForUser($user, 'convert_to_primary', false)->data && true === config('cer.enabled');+ $instance->setPreference('convert_to_primary', $res);+ return $res;+ }+ return $pref;}public function getPrimaryCurrency(): TransactionCurrency@@ -138,7 +147,7 @@ class Amountpublic function getPrimaryCurrencyByUserGroup(UserGroup $userGroup): TransactionCurrency{- $cache = new CacheProperties();+ $cache = new CacheProperties();$cache->addProperty('getPrimaryCurrencyByGroup');$cache->addProperty($userGroup->id);if ($cache->has()) {@@ -168,16 +177,16 @@ class Amount*/public function getAmountFromJournalObject(TransactionJournal $journal): string{- $convertToPrimary = $this->convertToPrimary();- $currency = $this->getPrimaryCurrency();- $field = $convertToPrimary && $currency->id !== $journal->transaction_currency_id ? 'pc_amount' : 'amount';+ $convertToPrimary = $this->convertToPrimary();+ $currency = $this->getPrimaryCurrency();+ $field = $convertToPrimary && $currency->id !== $journal->transaction_currency_id ? 'pc_amount' : 'amount';/** @var null|Transaction $sourceTransaction */$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();if (null === $sourceTransaction) {return '0';}- $amount = $sourceTransaction->{$field} ?? '0';+ $amount = $sourceTransaction->{$field} ?? '0';if ((int)$sourceTransaction->foreign_currency_id === $currency->id) {// use foreign amount instead!$amount = (string)$sourceTransaction->foreign_amount; // hard coded to be foreign amount.@@ -225,20 +234,20 @@ class Amountprivate function getLocaleInfo(): array{// get config from preference, not from translation:- $locale = app('steam')->getLocale();- $array = app('steam')->getLocaleArray($locale);+ $locale = app('steam')->getLocale();+ $array = app('steam')->getLocaleArray($locale);setlocale(LC_MONETARY, $array);- $info = localeconv();+ $info = localeconv();// correct variables- $info['n_cs_precedes'] = $this->getLocaleField($info, 'n_cs_precedes');- $info['p_cs_precedes'] = $this->getLocaleField($info, 'p_cs_precedes');+ $info['n_cs_precedes'] = $this->getLocaleField($info, 'n_cs_precedes');+ $info['p_cs_precedes'] = $this->getLocaleField($info, 'p_cs_precedes');- $info['n_sep_by_space'] = $this->getLocaleField($info, 'n_sep_by_space');- $info['p_sep_by_space'] = $this->getLocaleField($info, 'p_sep_by_space');+ $info['n_sep_by_space'] = $this->getLocaleField($info, 'n_sep_by_space');+ $info['p_sep_by_space'] = $this->getLocaleField($info, 'p_sep_by_space');- $fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);+ $fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);$info['mon_decimal_point'] = $fmt->getSymbol(NumberFormatter::MONETARY_SEPARATOR_SYMBOL);$info['mon_thousands_sep'] = $fmt->getSymbol(NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL);@@ -270,11 +279,11 @@ class Amount// there are five possible positions for the "+" or "-" sign (if it is even used)// pos_a and pos_e could be the ( and ) symbol.- $posA = ''; // before everything- $posB = ''; // before currency symbol- $posC = ''; // after currency symbol- $posD = ''; // before amount- $posE = ''; // after everything+ $posA = ''; // before everything+ $posB = ''; // before currency symbol+ $posC = ''; // after currency symbol+ $posD = ''; // before amount+ $posE = ''; // after everything// format would be (currency before amount)// AB%sC_D%vE@@ -316,9 +325,9 @@ class Amount}if ($csPrecedes) {- return $posA.$posB.'%s'.$posC.$space.$posD.'%v'.$posE;+ return $posA . $posB . '%s' . $posC . $space . $posD . '%v' . $posE;}- return $posA.$posD.'%v'.$space.$posB.'%s'.$posC.$posE;+ return $posA . $posD . '%v' . $space . $posB . '%s' . $posC . $posE;}}diff --git a/app/Support/Preferences.php b/app/Support/Preferences.phpindex c042fb4e04..7332c69d20 100644--- a/app/Support/Preferences.php+++ b/app/Support/Preferences.php@@ -25,6 +25,7 @@ namespace FireflyIII\Support;use FireflyIII\Exceptions\FireflyException;use FireflyIII\Models\Preference;+use FireflyIII\Support\Singleton\PreferencesSingleton;use FireflyIII\User;use Illuminate\Contracts\Encryption\DecryptException;use Illuminate\Contracts\Encryption\EncryptException;@@ -283,6 +284,12 @@ class Preferences*/public function lastActivity(): string{+ $instance = PreferencesSingleton::getInstance();+ $pref = $instance->getPreference('last_activity');+ if(null !== $pref) {+ // Log::debug(sprintf('Found last activity in singleton: %s', $pref));+ return $pref;+ }$lastActivity = microtime();$preference = $this->get('lastActivity', microtime());@@ -292,13 +299,16 @@ class Preferencesif (is_array($lastActivity)) {$lastActivity = implode(',', $lastActivity);}-- return hash('sha256', (string) $lastActivity);+ $setting = hash('sha256', (string) $lastActivity);+ $instance->setPreference('last_activity', $setting);+ return $setting;}public function mark(): void{$this->set('lastActivity', microtime());+ $instance = PreferencesSingleton::getInstance();+ $instance->setPreference('last_activity', microtime());Session::forget('first');}diff --git a/app/Support/Singleton/PreferencesSingleton.php b/app/Support/Singleton/PreferencesSingleton.phpnew file mode 100644index 0000000000..6d670aec84--- /dev/null+++ b/app/Support/Singleton/PreferencesSingleton.php@@ -0,0 +1,35 @@+<?php++namespace FireflyIII\Support\Singleton;++class PreferencesSingleton+{+ private static ?PreferencesSingleton $instance = null;++ private array $preferences = [];++ private function __construct()+ {+ // Private constructor to prevent direct instantiation.+ }++ public static function getInstance(): PreferencesSingleton+ {+ if (self::$instance === null) {+ self::$instance = new PreferencesSingleton();+ }++ return self::$instance;+ }++ public function setPreference(string $key, mixed $value): void+ {+ $this->preferences[$key] = $value;+ }++ public function getPreference(string $key): mixed+ {+ return $this->preferences[$key] ?? null;+ }++}