From 3a9ce68694f150b27f47f04d97fee7cdee460d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Chans=C3=A9aume?= Date: Wed, 2 Jul 2014 09:55:14 +0200 Subject: [PATCH] added cache to get default country and currency --- core/lib/Thelia/Model/Country.php | 15 ++++++++++----- core/lib/Thelia/Model/Currency.php | 13 +++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/core/lib/Thelia/Model/Country.php b/core/lib/Thelia/Model/Country.php index 038bd5a61..9d94c6824 100644 --- a/core/lib/Thelia/Model/Country.php +++ b/core/lib/Thelia/Model/Country.php @@ -16,6 +16,8 @@ class Country extends BaseCountry { use \Thelia\Model\Tools\ModelEventDispatcherTrait; + protected static $defaultCountry = null; + /** * * Put the current country as the default one. @@ -93,16 +95,19 @@ class Country extends BaseCountry /** * Return the default country * - * @throws LogicException if no default country is defined + * @throws \LogicException if no default country is defined */ public static function getDefaultCountry() { - $dc = CountryQuery::create()->findOneByByDefault(true); + if (null === self::$defaultCountry) { + self::$defaultCountry = CountryQuery::create()->findOneByByDefault(true); - if ($dc == null) - throw new \LogicException(Translator::getInstance()->trans("Cannot find a default country. Please define one.")); + if (self::$defaultCountry == null) { + throw new \LogicException(Translator::getInstance()->trans("Cannot find a default country. Please define one.")); + } + } - return $dc; + return self::$defaultCountry; } /** diff --git a/core/lib/Thelia/Model/Currency.php b/core/lib/Thelia/Model/Currency.php index 706553430..6a19088f7 100644 --- a/core/lib/Thelia/Model/Currency.php +++ b/core/lib/Thelia/Model/Currency.php @@ -14,15 +14,20 @@ class Currency extends BaseCurrency use \Thelia\Model\Tools\PositionManagementTrait; + protected static $defaultCurrency = null; + public static function getDefaultCurrency() { - $currency = CurrencyQuery::create()->findOneByByDefault(1); + if (null === self::$defaultCurrency) { - if (null === $currency) { - throw new \RuntimeException("No default currency is defined. Please define one."); + self::$defaultCurrency = CurrencyQuery::create()->findOneByByDefault(1); + + if (self::$defaultCurrency == null) { + throw new \RuntimeException("No default currency is defined. Please define one."); + } } - return $currency; + return self::$defaultCurrency; } /**