Skip to content

Commit 4eb2b41

Browse files
authored
Split Magento and Mage-OS versions in ProductMetadata for compatibility (#115)
1 parent b30bda9 commit 4eb2b41

File tree

12 files changed

+162
-20
lines changed

12 files changed

+162
-20
lines changed

app/code/Magento/Backend/Block/Page/Footer.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Backend\Block\Page;
77

8+
use Magento\Framework\App\DistributionMetadataInterface;
9+
810
/**
911
* Adminhtml footer block
1012
*
@@ -20,7 +22,7 @@ class Footer extends \Magento\Backend\Block\Template
2022
protected $_template = 'Magento_Backend::page/footer.phtml';
2123

2224
/**
23-
* @var \Magento\Framework\App\ProductMetadataInterface
25+
* @var \Magento\Framework\App\ProductMetadataInterface|DistributionMetadataInterface
2426
* @since 100.1.0
2527
*/
2628
protected $productMetadata;
@@ -55,7 +57,17 @@ protected function _construct()
5557
*/
5658
public function getMagentoVersion()
5759
{
58-
return $this->productMetadata->getVersion();
60+
return $this->productMetadata->getDistributionVersion();
61+
}
62+
63+
/**
64+
* Get product name
65+
*
66+
* @return string
67+
*/
68+
public function getName()
69+
{
70+
return $this->productMetadata->getDistributionName();
5971
}
6072

6173
/**

app/code/Magento/Backend/view/adminhtml/templates/page/footer.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ use Magento\Framework\Escaper;
1111
/** @var Footer $block */
1212
?>
1313
<p class="magento-version">
14-
<strong><?= $escaper->escapeHtml(__('Mage-OS')); ?></strong>
14+
<strong><?= $escaper->escapeHtml(__($block->getName())); ?></strong>
1515
<?= $escaper->escapeHtml(__('ver. %1', $block->getMagentoVersion())); ?>
1616
</p>

app/code/Magento/Version/Controller/Index/Index.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\App\Action\Action;
1111
use Magento\Framework\App\Action\Context;
1212
use Magento\Framework\App\Action\HttpGetActionInterface;
13+
use Magento\Framework\App\DistributionMetadataInterface;
1314
use Magento\Framework\App\ProductMetadataInterface;
1415
use Magento\Framework\Controller\Result\RawFactory as RawResponseFactory;
1516

@@ -21,7 +22,7 @@ class Index extends Action implements HttpGetActionInterface
2122
public const DEV_PREFIX = 'dev-';
2223

2324
/**
24-
* @var ProductMetadataInterface
25+
* @var ProductMetadataInterface|DistributionMetadataInterface
2526
*/
2627
private $productMetadata;
2728

@@ -52,11 +53,11 @@ public function execute()
5253
{
5354
$rawResponse = $this->rawFactory->create();
5455

55-
$version = $this->productMetadata->getVersion() ?? '';
56+
$version = $this->productMetadata->getDistributionVersion() ?? '';
5657
$versionParts = explode('.', $version);
5758
if (!$this->isGitBasedInstallation($version) && $this->isCorrectVersion($versionParts)) {
5859
$rawResponse->setContents(
59-
$this->productMetadata->getName() . '/' .
60+
$this->productMetadata->getDistributionName() . '/' .
6061
$this->getMajorMinorVersion($versionParts) .
6162
' (' . $this->productMetadata->getEdition() . ')'
6263
);

app/code/Magento/Version/Test/Unit/Controller/Index/IndexTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function setUp(): void
4141

4242
$this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class)
4343
->disableOriginalConstructor()
44-
->setMethods(['getName', 'getEdition', 'getVersion'])
44+
->setMethods(['getName', 'getEdition', 'getVersion', 'getDistributionName', 'getDistributionVersion'])
4545
->getMockForAbstractClass();
4646

4747
$this->rawResponseFactoryMock = $this->createPartialMock(RawFactory::class, ['create']);
@@ -78,9 +78,13 @@ public function testCommunityVersionDisplaysMajorMinorVersionAndEditionName(): v
7878
$this->productMetadataMock->expects($this->any())->method('getVersion')->willReturn('2.3.3');
7979
$this->productMetadataMock->expects($this->any())->method('getEdition')->willReturn('Community');
8080
$this->productMetadataMock->expects($this->any())->method('getName')->willReturn('Magento');
81+
$this->productMetadataMock->expects($this->any())->method('getDistributionVersion')
82+
->willReturn('1.1.0');
83+
$this->productMetadataMock->expects($this->any())->method('getDistributionName')
84+
->willReturn('Mage-OS');
8185

8286
$this->rawResponseMock->expects($this->once())->method('setContents')
83-
->with('Magento/2.3 (Community)')
87+
->with('Mage-OS/1.1 (Community)')
8488
->willReturnSelf();
8589

8690
$this->versionController->execute();

app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Webapi\Model\Rest\Swagger;
77

88
use Magento\Framework\Api\SimpleDataObjectConverter;
9+
use Magento\Framework\App\DistributionMetadataInterface;
910
use Magento\Framework\App\ProductMetadataInterface;
1011
use Magento\Framework\Reflection\TypeProcessor;
1112
use Magento\Framework\Webapi\Authorization;
@@ -52,7 +53,7 @@ class Generator extends AbstractSchemaGenerator
5253
/**
5354
* Magento product metadata
5455
*
55-
* @var ProductMetadataInterface
56+
* @var ProductMetadataInterface|DistributionMetadataInterface
5657
*/
5758
protected ProductMetadataInterface $productMetadata;
5859

@@ -182,15 +183,15 @@ protected function generateSchema($requestedServiceMetadata, $requestScheme, $re
182183
*/
183184
protected function getGeneralInfo()
184185
{
185-
$versionParts = explode('.', $this->productMetadata->getVersion());
186+
$versionParts = explode('.', $this->productMetadata->getDistributionVersion());
186187
if (!isset($versionParts[0]) || !isset($versionParts[1])) {
187188
return []; // Major and minor version are not set - return empty response
188189
}
189190
$majorMinorVersion = $versionParts[0] . '.' . $versionParts[1];
190191

191192
return [
192193
'version' => $majorMinorVersion,
193-
'title' => $this->productMetadata->getName() . ' ' . $this->productMetadata->getEdition(),
194+
'title' => $this->productMetadata->getDistributionName() . ' ' . $this->productMetadata->getEdition(),
194195
];
195196
}
196197

app/code/Magento/Webapi/Test/Unit/Model/Rest/Swagger/GeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function testGenerate($serviceMetadata, $typeData, $schema)
181181
);
182182

183183
$this->productMetadata->expects($this->once())
184-
->method('getVersion')
184+
->method('getDistributionVersion')
185185
->willReturn('UNKNOWN');
186186

187187
$this->assertEquals(

dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public function testIndexAction()
1616
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
1717
/** @var \Magento\Framework\App\ProductMetadataInterface $productMetadata */
1818
$productMetadata = $objectManager->get(\Magento\Framework\App\ProductMetadataInterface::class);
19-
$name = $productMetadata->getName();
19+
$name = $productMetadata->getDistributionName();
2020
$edition = $productMetadata->getEdition();
2121

22-
$fullVersion = $productMetadata->getVersion();
22+
$fullVersion = $productMetadata->getDistributionVersion();
2323
if ($this->isComposerBasedInstallation($fullVersion)) {
2424
$versionParts = explode('.', $fullVersion);
2525
$majorMinor = $versionParts[0] . '.' . $versionParts[1];
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* (c) Mage-OS
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* files distributed with this source code.
7+
*/
8+
namespace Magento\Framework\App;
9+
10+
/**
11+
* Mage-OS Distribution metadata
12+
*
13+
* @api
14+
* @since 1.0.6
15+
*/
16+
interface DistributionMetadataInterface
17+
{
18+
/**
19+
* Get Distribution version
20+
*
21+
* @return string
22+
*/
23+
public function getDistributionVersion();
24+
25+
/**
26+
* Get Distribution name
27+
*
28+
* @return string
29+
*/
30+
public function getDistributionName();
31+
}

lib/internal/Magento/Framework/App/ProductMetadata.php

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Magento application product metadata
1717
*/
18-
class ProductMetadata implements ProductMetadataInterface
18+
class ProductMetadata implements ProductMetadataInterface, DistributionMetadataInterface
1919
{
2020
/**
2121
* Magento product edition
@@ -25,20 +25,37 @@ class ProductMetadata implements ProductMetadataInterface
2525
/**
2626
* Magento product name
2727
*/
28-
const PRODUCT_NAME = 'Mage-OS';
28+
public const PRODUCT_NAME = 'Magento';
29+
30+
/**
31+
* Distribution product name
32+
*/
33+
public const DISTRIBUTION_NAME = 'Mage-OS';
2934

3035
/**
3136
* Magento version cache key
3237
*/
3338
const VERSION_CACHE_KEY = 'mage-version';
3439

40+
/**
41+
* Distribution version cache key
42+
*/
43+
protected const DISTRO_VERSION_CACHE_KEY = 'distro-version';
44+
3545
/**
3646
* Product version
3747
*
3848
* @var string
3949
*/
4050
protected $version;
4151

52+
/**
53+
* Distribution version
54+
*
55+
* @var string
56+
*/
57+
protected $distroVersion;
58+
4259
/**
4360
* @var \Magento\Framework\Composer\ComposerJsonFinder
4461
* @deprecated 100.1.0
@@ -89,6 +106,27 @@ public function getVersion()
89106
return $this->version;
90107
}
91108

109+
/**
110+
* Get Distribution version
111+
*
112+
* @return string
113+
*/
114+
public function getDistributionVersion()
115+
{
116+
$this->distroVersion = $this->distroVersion ?: $this->cache->load(self::DISTRO_VERSION_CACHE_KEY);
117+
if (!$this->distroVersion) {
118+
if (!($this->distroVersion = $this->getSystemDistroVersion())) {
119+
if ($this->getComposerInformation()->isMagentoRoot()) {
120+
$this->distroVersion = $this->getComposerInformation()->getRootPackage()->getPrettyVersion();
121+
} else {
122+
$this->distroVersion = 'UNKNOWN';
123+
}
124+
}
125+
$this->cache->save($this->distroVersion, self::DISTRO_VERSION_CACHE_KEY, [Config::CACHE_TAG]);
126+
}
127+
return $this->distroVersion;
128+
}
129+
92130
/**
93131
* Get Product edition
94132
*
@@ -109,13 +147,39 @@ public function getName()
109147
return self::PRODUCT_NAME;
110148
}
111149

150+
/**
151+
* Get Distribution name
152+
*
153+
* @return string
154+
*/
155+
public function getDistributionName()
156+
{
157+
return self::DISTRIBUTION_NAME;
158+
}
159+
112160
/**
113161
* Get version from system package
114162
*
115163
* @return string
116164
* @deprecated 100.1.0
117165
*/
118166
private function getSystemPackageVersion()
167+
{
168+
$packages = $this->getComposerInformation()->getSystemPackages();
169+
foreach ($packages as $package) {
170+
if (isset($package['name']) && isset($package['magento_version'])) {
171+
return $package['magento_version'];
172+
}
173+
}
174+
return '';
175+
}
176+
177+
/**
178+
* Get distribution version from system package
179+
*
180+
* @return string
181+
*/
182+
private function getSystemDistroVersion()
119183
{
120184
$packages = $this->getComposerInformation()->getSystemPackages();
121185
foreach ($packages as $package) {

lib/internal/Magento/Framework/Composer/ComposerInformation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ public function getSystemPackages()
246246
$packages[$package->getName()] = [
247247
'name' => $package->getName(),
248248
'type' => $package->getType(),
249-
'version' => $package->getPrettyVersion()
249+
'version' => $package->getPrettyVersion(),
250+
'magento_version' => $package->getExtra()['magento_version'] ?? null,
250251
];
251252
}
252253
}

0 commit comments

Comments
 (0)