Skip to content

Commit

Permalink
Fallback Magento version detection when magento/magento2-base is not …
Browse files Browse the repository at this point in the history
…available
  • Loading branch information
patrick-bigbridge committed Jun 21, 2021
1 parent 9420451 commit b3f2f1f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
16 changes: 12 additions & 4 deletions Model/Resource/MetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.");
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b3f2f1f

Please sign in to comment.