diff --git a/CHANGELOG.md b/CHANGELOG.md index 01d5c84..3eb65bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +# 1.7.2 : Use official Magento version detection when magento/magento2-base is not available + +As mentioned by Pieter Hoste, magento/magento2-base may not be available; in this case the official version detection will now be used. + # 1.7.1 : More robust when handling non-existing categories. Stronger getCategoryInfo check when fetching parent categories. Pull request by Duckā†„hip. diff --git a/Model/Resource/MetaData.php b/Model/Resource/MetaData.php index b394b71..db35b4b 100644 --- a/Model/Resource/MetaData.php +++ b/Model/Resource/MetaData.php @@ -12,6 +12,8 @@ use BigBridge\ProductImport\Model\Resource\Serialize\SerializeValueSerializer; use BigBridge\ProductImport\Model\Resource\Serialize\ValueSerializer; use Exception; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\App\ProductMetadata; /** * Pre-loads all meta data needed for the core processes once. @@ -415,14 +417,20 @@ protected function detectMagentoVersion() // $productMetadata = new \Magento\Framework\App\ProductMetadata(); // $version = $productMetadata->getVersion(); // - // But is takes 0.2 seconds to execute, this is too long + // But it takes up to 0.2 seconds to execute, this is too long // See also https://magento.stackexchange.com/questions/96858/how-to-get-magento-version-in-magento2-equivalent-of-magegetversion + // + // However, if magento/magento2-base is not deployed, it falls back to the official method + // See https://github.com/bigbridge-nl/product-import/issues/45 - if (preg_match('/"version": "([^\"]+)"/', - file_get_contents(BP . '/vendor/magento/magento2-base/composer.json'), $matches)) { + $composerFile = BP . '/vendor/magento/magento2-base/composer.json'; + if (!file_exists($composerFile)) { + $productMetadata = ObjectManager::getInstance()->get(ProductMetadata::class); + $magentoVersion = $productMetadata->getVersion(); + } else if (preg_match('/"version": "([^\"]+)"/', + file_get_contents($composerFile), $matches)) { $magentoVersion = $matches[1]; - } else { throw new Exception("Magento version could not be detected."); } diff --git a/README.md b/README.md index 1de648b..e264be9 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This library imports product data into Magento 2 via direct database queries. It * A programming library to import products * An CLI command for product import via XML file -* An Web API service for product import via Post Request with XML +* A web API service for product import via Post Request with XML * A tool to update url_rewrites ## Note