19
19
20
20
use Magento \Config \Model \ResourceModel \Config as ResourceConfig ;
21
21
use Magento \Framework \App \Cache \TypeListInterface ;
22
+ use Magento \Framework \App \CacheInterface ;
23
+ use Magento \Framework \App \Config as AppConfig ;
22
24
use Magento \Framework \App \Config \ScopeConfigInterface ;
25
+ use Magento \Framework \Composer \ComposerInformation ;
23
26
use Magento \Framework \Exception \NoSuchEntityException ;
24
- use Magento \Framework \Module \ModuleListInterface ;
25
27
use Magento \Store \Model \ScopeInterface ;
26
28
use Magento \Store \Model \StoreManagerInterface ;
27
29
30
32
*/
31
33
class Config
32
34
{
33
- private const MODULE_NAME = 'Meta_BusinessExtension ' ;
35
+ private const VERSION_CACHE_KEY = 'meta-business-extension-version ' ;
36
+ private const EXTENSION_PACKAGE_NAME = 'meta/meta-for-magento2 ' ;
34
37
35
38
private const XML_PATH_FACEBOOK_BUSINESS_EXTENSION_ACTIVE = 'facebook/business_extension/active ' ;
36
39
public const XML_PATH_FACEBOOK_BUSINESS_EXTENSION_INSTALLED = 'facebook/business_extension/installed ' ;
@@ -111,36 +114,51 @@ class Config
111
114
private $ resourceConfig ;
112
115
113
116
/**
114
- * @var ModuleListInterface
117
+ * @var TypeListInterface
115
118
*/
116
- private $ moduleList ;
119
+ private $ cacheTypeList ;
117
120
118
121
/**
119
- * @var TypeListInterface
122
+ * @var CacheInterface
120
123
*/
121
- private $ cacheTypeList ;
124
+ private $ cache ;
125
+
126
+ /**
127
+ * @var ComposerInformation
128
+ */
129
+ private $ composerInformation ;
130
+
131
+ /**
132
+ * Extension version
133
+ *
134
+ * @var string
135
+ */
136
+ private $ version ;
122
137
123
138
/**
124
139
* @method __construct
125
140
* @param StoreManagerInterface $storeManager
126
141
* @param ScopeConfigInterface $scopeConfig
127
142
* @param ResourceConfig $resourceConfig
128
- * @param ModuleListInterface $moduleList
129
143
* @param TypeListInterface $cacheTypeList
144
+ * @param CacheInterface $cache
145
+ * @param ComposerInformation $composerInformation
130
146
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
131
147
*/
132
148
public function __construct (
133
149
StoreManagerInterface $ storeManager ,
134
150
ScopeConfigInterface $ scopeConfig ,
135
151
ResourceConfig $ resourceConfig ,
136
- ModuleListInterface $ moduleList ,
137
- TypeListInterface $ cacheTypeList
152
+ TypeListInterface $ cacheTypeList ,
153
+ CacheInterface $ cache ,
154
+ ComposerInformation $ composerInformation
138
155
) {
139
156
$ this ->storeManager = $ storeManager ;
140
157
$ this ->scopeConfig = $ scopeConfig ;
141
158
$ this ->resourceConfig = $ resourceConfig ;
142
- $ this ->moduleList = $ moduleList ;
143
159
$ this ->cacheTypeList = $ cacheTypeList ;
160
+ $ this ->cache = $ cache ;
161
+ $ this ->composerInformation = $ composerInformation ;
144
162
}
145
163
146
164
/**
@@ -154,13 +172,24 @@ public function getAppId()
154
172
}
155
173
156
174
/**
157
- * Get module version
175
+ * Get extension version
158
176
*
159
- * @return mixed
177
+ * @return string
160
178
*/
161
- public function getModuleVersion ()
179
+ public function getModuleVersion (): string
162
180
{
163
- return $ this ->moduleList ->getOne (self ::MODULE_NAME )['setup_version ' ];
181
+ $ this ->version = $ this ->version ?: $ this ->cache ->load (self ::VERSION_CACHE_KEY );
182
+ if (!$ this ->version ) {
183
+ $ installedPackages = $ this ->composerInformation ->getInstalledMagentoPackages ();
184
+ $ extensionVersion = $ installedPackages [self ::EXTENSION_PACKAGE_NAME ]['version ' ] ?? null ;
185
+ if (!empty ($ extensionVersion )) {
186
+ $ this ->version = $ extensionVersion ;
187
+ } else {
188
+ $ this ->version = 'dev ' ;
189
+ }
190
+ $ this ->cache ->save ($ this ->version , self ::VERSION_CACHE_KEY , [AppConfig::CACHE_TAG ]);
191
+ }
192
+ return $ this ->version ;
164
193
}
165
194
166
195
/**
0 commit comments