From da67c9a936a6bafd9df6a16b7a27145e59dfeb20 Mon Sep 17 00:00:00 2001 From: indykoning <15870933+indykoning@users.noreply.github.com> Date: Wed, 30 Oct 2024 09:09:35 +0100 Subject: [PATCH] Performance optimizations (#77) --- composer.json | 1 + src/Extend/SitesLinkedToMagentoStores.php | 10 +++++++--- src/Models/Category.php | 5 ++++- src/Models/Product.php | 5 ++++- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index a833e00..c625727 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "php": "^8.1|^8.2", "rapidez/blade-directives": "^0.6", "justbetter/statamic-glide-directive": "^2.1", + "spatie/once": "*", "statamic-rad-pack/runway": "^7.6", "statamic/cms": "^5.0", "statamic/eloquent-driver": "^4.9", diff --git a/src/Extend/SitesLinkedToMagentoStores.php b/src/Extend/SitesLinkedToMagentoStores.php index cef143c..dffe7e0 100644 --- a/src/Extend/SitesLinkedToMagentoStores.php +++ b/src/Extend/SitesLinkedToMagentoStores.php @@ -2,22 +2,26 @@ namespace Rapidez\Statamic\Extend; +use Illuminate\Support\Collection; use Statamic\Sites\Sites; class SitesLinkedToMagentoStores extends Sites { public function findByUrl($url) { - if ($site = $this->findByMageRunCode(request()->server('MAGE_RUN_CODE'))) { + if ($site = once(fn() => $this->findByMageRunCode(request()->server('MAGE_RUN_CODE')))) { return $site; } - return parent::findByUrl($url); + return once(fn() => parent::findByUrl($url)); } public function findByMageRunCode($code) { - return collect($this->sites)->get($code); + if (!$code || !($this->sites instanceof Collection)) { + return null; + } + return $this->sites->get($code)); } } diff --git a/src/Models/Category.php b/src/Models/Category.php index 96a5c2f..91f6447 100644 --- a/src/Models/Category.php +++ b/src/Models/Category.php @@ -15,6 +15,9 @@ class Category extends Model public function getTable() { - return 'catalog_category_flat_store_'.(Statamic::isCpRoute() ? (Site::selected()->attributes['magento_store_id'] ?? '1') : (Site::current()->attributes['magento_store_id'] ?? '1')); + return 'catalog_category_flat_store_'.once(fn() => (Statamic::isCpRoute() + ? (Site::selected()->attributes['magento_store_id'] ?? '1') + : (Site::current()->attributes['magento_store_id'] ?? '1') + )); } } diff --git a/src/Models/Product.php b/src/Models/Product.php index 12636c7..1ceae55 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -24,6 +24,9 @@ protected static function booting() public function getTable() { - return 'catalog_product_flat_' . (Statamic::isCpRoute() ? (Site::selected()->attributes['magento_store_id'] ?? '1') : Site::current()->attributes['magento_store_id'] ?? '1'); + return 'catalog_product_flat_' . once(fn() => (Statamic::isCpRoute() + ? (Site::selected()->attributes['magento_store_id'] ?? '1') + : (Site::current()->attributes['magento_store_id'] ?? '1') + )); } }