11
11
use Magento \Customer \Api \Data \AddressInterfaceFactory ;
12
12
use Magento \Customer \Api \Data \RegionInterface ;
13
13
use Magento \Customer \Api \Data \RegionInterfaceFactory ;
14
+ use Magento \Customer \Model \Address \AbstractAddress \CountryModelsCache ;
15
+ use Magento \Customer \Model \Address \AbstractAddress \RegionModelsCache ;
14
16
use Magento \Customer \Model \Data \Address as AddressData ;
15
17
use Magento \Framework \App \ObjectManager ;
16
18
use Magento \Framework \Model \AbstractExtensibleModel ;
@@ -61,18 +63,28 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
61
63
protected $ _eventObject = 'customer_address ' ;
62
64
63
65
/**
64
- * cache of Directory country models
66
+ * Directory country models
65
67
*
66
68
* @var \Magento\Directory\Model\Country[]
69
+ * @deprecated
70
+ * @see $countryModelsCache
67
71
*/
68
- protected $ _countryModels = [];
72
+ protected static $ _countryModels = [];
73
+
74
+ /** @var CountryModelsCache */
75
+ private readonly CountryModelsCache $ countryModelsCache ;
69
76
70
77
/**
71
- * cache of Directory region models
78
+ * Directory region models
72
79
*
73
80
* @var \Magento\Directory\Model\Region[]
81
+ * @deprecated
82
+ * @see $regionModelsCache
74
83
*/
75
- protected $ _regionModels = [];
84
+ protected static $ _regionModels = [];
85
+
86
+ /** @var RegionModelsCache */
87
+ private readonly RegionModelsCache $ regionModelsCache ;
76
88
77
89
/**
78
90
* @var \Magento\Directory\Helper\Data
@@ -144,7 +156,9 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
144
156
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
145
157
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
146
158
* @param array $data
147
- * @param CompositeValidator $compositeValidator
159
+ * @param CompositeValidator|null $compositeValidator
160
+ * @param CountryModelsCache|null $countryModelsCache
161
+ * @param RegionModelsCache|null $regionModelsCache
148
162
*
149
163
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
150
164
*/
@@ -165,7 +179,9 @@ public function __construct(
165
179
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
166
180
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
167
181
array $ data = [],
168
- CompositeValidator $ compositeValidator = null
182
+ ?CompositeValidator $ compositeValidator = null ,
183
+ ?CountryModelsCache $ countryModelsCache = null ,
184
+ ?RegionModelsCache $ regionModelsCache = null ,
169
185
) {
170
186
$ this ->_directoryData = $ directoryData ;
171
187
$ data = $ this ->_implodeArrayField ($ data );
@@ -179,6 +195,10 @@ public function __construct(
179
195
$ this ->dataObjectHelper = $ dataObjectHelper ;
180
196
$ this ->compositeValidator = $ compositeValidator ?: ObjectManager::getInstance ()
181
197
->get (CompositeValidator::class);
198
+ $ this ->countryModelsCache = $ countryModelsCache ?: ObjectManager::getInstance ()
199
+ ->get (CountryModelsCache::class);
200
+ $ this ->regionModelsCache = $ regionModelsCache ?: ObjectManager::getInstance ()
201
+ ->get (RegionModelsCache::class);
182
202
parent ::__construct (
183
203
$ context ,
184
204
$ registry ,
@@ -502,12 +522,12 @@ public function getCountry()
502
522
*/
503
523
public function getCountryModel ()
504
524
{
505
- if (! isset ( $ this ->_countryModels [ $ this ->getCountryId ()] )) {
525
+ if ($ country = $ this ->countryModelsCache -> get ( $ this ->getCountryId ())) {
506
526
$ country = $ this ->_createCountryInstance ();
507
527
$ country ->load ($ this ->getCountryId ());
508
- $ this ->_countryModels [ $ this ->getCountryId ()] = $ country ;
528
+ $ this ->countryModelsCache -> add ( $ this ->getCountryId (), $ country) ;
509
529
}
510
- return $ this -> _countryModels [ $ this -> getCountryId ()] ;
530
+ return $ country ;
511
531
}
512
532
513
533
/**
@@ -521,14 +541,12 @@ public function getRegionModel($regionId = null)
521
541
if ($ regionId === null ) {
522
542
$ regionId = $ this ->getRegionId ();
523
543
}
524
-
525
- if (!isset ($ this ->_regionModels [$ regionId ])) {
544
+ if ($ region = $ this ->regionModelsCache ->get ($ regionId )) {
526
545
$ region = $ this ->_createRegionInstance ();
527
546
$ region ->load ($ regionId );
528
- $ this ->_regionModels [ $ regionId] = $ region ;
547
+ $ this ->regionModelsCache -> add ( $ regionId, $ region) ;
529
548
}
530
-
531
- return $ this ->_regionModels [$ regionId ];
549
+ return $ region ;
532
550
}
533
551
534
552
/**
@@ -746,7 +764,7 @@ private function processCustomAttribute(array $attribute): array
746
764
*/
747
765
public function _resetState (): void
748
766
{
749
- $ this -> _countryModels = [];
750
- $ this -> _regionModels = [];
767
+ self :: $ _countryModels = [];
768
+ self :: $ _regionModels = [];
751
769
}
752
770
}
0 commit comments