11<?php
2- declare (strict_types= 1 );
2+
33/**
44 * @copyright Copyright (c) Vendic B.V https://vendic.nl/
55 */
66
7+ declare (strict_types=1 );
8+
79namespace Vendic \OptimizeCacheSize \Plugin ;
810
9- use Magento \Framework \App \ScopeInterface ;
1011use Magento \Framework \View \DesignInterface ;
1112use Magento \Store \Model \StoreManagerInterface ;
1213use Magento \Framework \Exception \LocalizedException ;
1314use Magento \Framework \View \Layout \ProcessorInterface ;
15+ use Magento \Widget \Model \ResourceModel \Layout \Update \CollectionFactory ;
1416use Vendic \OptimizeCacheSize \Model \Config ;
15- use Magento \Widget \Model \ResourceModel \Layout \Update ;
1617use Magento \Framework \Exception \NoSuchEntityException ;
17- use Vendic \OptimizeCacheSize \Model \LayoutUpdateFetcher ;
1818
19- class RemoveHandlersPlugin
19+ class RemoveHandlers
2020{
21-
2221 private const string PRODUCT_ID_HANDLER_STRING = 'catalog_product_view_id_ ' ;
2322 private const string PRODUCT_SKU_HANDLER_STRING = 'catalog_product_view_sku_ ' ;
2423 private const string CATEGORY_ID_HANDLER_STRING = 'catalog_category_view_id_ ' ;
2524
25+ private array $ dbLayoutHandlers = [];
26+
2627 public function __construct (
2728 private readonly Config $ config ,
28- private readonly LayoutUpdateFetcher $ layoutUpdateFetcher ,
2929 private readonly StoreManagerInterface $ storeManager ,
30- private readonly DesignInterface $ design
30+ private readonly DesignInterface $ design ,
31+ private readonly CollectionFactory $ layoutUpdateCollectionFactory
3132 ) {
3233 }
3334
@@ -44,17 +45,11 @@ public function afterAddHandle(
4445 return $ result ;
4546 }
4647
47- $ store = (string )$ this ->storeManager ->getStore ()->getId ();
48- $ theme = (string )$ this ->design ->getDesignTheme ()->getId ();
49- $ handlers = $ result ->getHandles ();
50-
51- $ dbLayoutHandlers = $ this ->layoutUpdateFetcher ->fetchDbLayoutHandlers ($ handlers , $ theme , $ store );
52-
53- foreach ($ handlers as $ handler ) {
48+ foreach ($ result ->getHandles () as $ handler ) {
5449 if (
5550 $ this ->config ->isRemoveCategoryIdHandlers ()
5651 && str_contains ($ handler , self ::CATEGORY_ID_HANDLER_STRING )
57- && !in_array ($ handler, $ dbLayoutHandlers )
52+ && !$ this -> hasDbLayoutUpdate ($ handler )
5853 ) {
5954 $ result ->removeHandle ($ handler );
6055 continue ;
@@ -63,7 +58,7 @@ public function afterAddHandle(
6358 if (
6459 $ this ->config ->isRemoveProductIdHandlers ()
6560 && str_contains ($ handler , self ::PRODUCT_ID_HANDLER_STRING )
66- && !in_array ($ handler, $ dbLayoutHandlers )
61+ && !$ this -> hasDbLayoutUpdate ($ handler )
6762 ) {
6863 $ result ->removeHandle ($ handler );
6964 continue ;
@@ -72,11 +67,27 @@ public function afterAddHandle(
7267 if (
7368 $ this ->config ->isRemoveProductSkuHandlers ()
7469 && str_contains ($ handler , self ::PRODUCT_SKU_HANDLER_STRING )
75- && !in_array ($ handler, $ dbLayoutHandlers )
70+ && !$ this -> hasDbLayoutUpdate ($ handler )
7671 ) {
7772 $ result ->removeHandle ($ handler );
7873 }
7974 }
8075 return $ result ;
8176 }
77+
78+ private function hasDbLayoutUpdate (string $ handler ): bool
79+ {
80+ $ storeId = (int )$ this ->storeManager ->getStore ()->getId ();
81+ $ themeId = (int )$ this ->design ->getDesignTheme ()->getId ();
82+
83+ if (!isset ($ this ->dbLayoutHandlers [$ storeId ][$ themeId ][$ handler ])) {
84+ $ updateCollection = $ this ->layoutUpdateCollectionFactory ->create ();
85+ $ updateCollection ->addStoreFilter ($ storeId );
86+ $ updateCollection ->addThemeFilter ($ themeId );
87+ $ updateCollection ->addFieldToFilter ('handle ' , $ handler );
88+ $ this ->dbLayoutHandlers [$ storeId ][$ themeId ][$ handler ] = $ updateCollection ->getSize () > 0 ;
89+ }
90+
91+ return $ this ->dbLayoutHandlers [$ storeId ][$ themeId ][$ handler ];
92+ }
8293}
0 commit comments