From 7cc14d033c972a473bfc258e3c99ba230be61e65 Mon Sep 17 00:00:00 2001 From: Ryan Hoerr Date: Sun, 12 Jan 2025 23:16:17 -0500 Subject: [PATCH 1/5] feat: separate Mage-OS vs Magento version in ProductMetadata for compat --- .../Magento/Backend/Block/Page/Footer.php | 16 ++++- .../adminhtml/templates/page/footer.phtml | 2 +- .../Version/Controller/Index/Index.php | 5 +- .../Webapi/Model/Rest/Swagger/Generator.php | 5 +- .../Version/Controller/Index/IndexTest.php | 2 +- .../App/DistributionMetadataInterface.php | 29 ++++++++ .../Magento/Framework/App/ProductMetadata.php | 70 ++++++++++++++++++- .../Composer/ComposerInformation.php | 3 +- .../Magento/Framework/Console/Cli.php | 32 ++++++++- setup/index.php | 2 +- 10 files changed, 151 insertions(+), 15 deletions(-) create mode 100644 lib/internal/Magento/Framework/App/DistributionMetadataInterface.php diff --git a/app/code/Magento/Backend/Block/Page/Footer.php b/app/code/Magento/Backend/Block/Page/Footer.php index 610d28b0f53e..5ad3111b57bd 100644 --- a/app/code/Magento/Backend/Block/Page/Footer.php +++ b/app/code/Magento/Backend/Block/Page/Footer.php @@ -5,6 +5,8 @@ */ namespace Magento\Backend\Block\Page; +use Magento\Framework\App\DistributionMetadataInterface; + /** * Adminhtml footer block * @@ -20,7 +22,7 @@ class Footer extends \Magento\Backend\Block\Template protected $_template = 'Magento_Backend::page/footer.phtml'; /** - * @var \Magento\Framework\App\ProductMetadataInterface + * @var \Magento\Framework\App\ProductMetadataInterface|DistributionMetadataInterface * @since 100.1.0 */ protected $productMetadata; @@ -55,7 +57,17 @@ protected function _construct() */ public function getMagentoVersion() { - return $this->productMetadata->getVersion(); + return $this->productMetadata->getDistributionVersion(); + } + + /** + * Get product name + * + * @return string + */ + public function getName() + { + return $this->productMetadata->getName(); } /** diff --git a/app/code/Magento/Backend/view/adminhtml/templates/page/footer.phtml b/app/code/Magento/Backend/view/adminhtml/templates/page/footer.phtml index 7b1f14d28a50..ab20cec1d9bb 100644 --- a/app/code/Magento/Backend/view/adminhtml/templates/page/footer.phtml +++ b/app/code/Magento/Backend/view/adminhtml/templates/page/footer.phtml @@ -11,6 +11,6 @@ use Magento\Framework\Escaper; /** @var Footer $block */ ?>

- escapeHtml(__('Mage-OS')); ?> + escapeHtml(__($block->getName())); ?> escapeHtml(__('ver. %1', $block->getMagentoVersion())); ?>

diff --git a/app/code/Magento/Version/Controller/Index/Index.php b/app/code/Magento/Version/Controller/Index/Index.php index 512dbe13ab3c..5d4a054564ee 100644 --- a/app/code/Magento/Version/Controller/Index/Index.php +++ b/app/code/Magento/Version/Controller/Index/Index.php @@ -10,6 +10,7 @@ use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Action\HttpGetActionInterface; +use Magento\Framework\App\DistributionMetadataInterface; use Magento\Framework\App\ProductMetadataInterface; use Magento\Framework\Controller\Result\RawFactory as RawResponseFactory; @@ -21,7 +22,7 @@ class Index extends Action implements HttpGetActionInterface public const DEV_PREFIX = 'dev-'; /** - * @var ProductMetadataInterface + * @var ProductMetadataInterface|DistributionMetadataInterface */ private $productMetadata; @@ -52,7 +53,7 @@ public function execute() { $rawResponse = $this->rawFactory->create(); - $version = $this->productMetadata->getVersion() ?? ''; + $version = $this->productMetadata->getDistributionVersion() ?? ''; $versionParts = explode('.', $version); if (!$this->isGitBasedInstallation($version) && $this->isCorrectVersion($versionParts)) { $rawResponse->setContents( diff --git a/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php b/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php index c2af90db35d2..2d42545735fb 100644 --- a/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php +++ b/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php @@ -6,6 +6,7 @@ namespace Magento\Webapi\Model\Rest\Swagger; use Magento\Framework\Api\SimpleDataObjectConverter; +use Magento\Framework\App\DistributionMetadataInterface; use Magento\Framework\App\ProductMetadataInterface; use Magento\Framework\Reflection\TypeProcessor; use Magento\Framework\Webapi\Authorization; @@ -52,7 +53,7 @@ class Generator extends AbstractSchemaGenerator /** * Magento product metadata * - * @var ProductMetadataInterface + * @var ProductMetadataInterface|DistributionMetadataInterface */ protected ProductMetadataInterface $productMetadata; @@ -182,7 +183,7 @@ protected function generateSchema($requestedServiceMetadata, $requestScheme, $re */ protected function getGeneralInfo() { - $versionParts = explode('.', $this->productMetadata->getVersion()); + $versionParts = explode('.', $this->productMetadata->getDistributionVersion()); if (!isset($versionParts[0]) || !isset($versionParts[1])) { return []; // Major and minor version are not set - return empty response } diff --git a/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php b/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php index 18c1b5cc9884..4524b962a98b 100644 --- a/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php +++ b/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php @@ -19,7 +19,7 @@ public function testIndexAction() $name = $productMetadata->getName(); $edition = $productMetadata->getEdition(); - $fullVersion = $productMetadata->getVersion(); + $fullVersion = $productMetadata->getDistributionVersion(); if ($this->isComposerBasedInstallation($fullVersion)) { $versionParts = explode('.', $fullVersion); $majorMinor = $versionParts[0] . '.' . $versionParts[1]; diff --git a/lib/internal/Magento/Framework/App/DistributionMetadataInterface.php b/lib/internal/Magento/Framework/App/DistributionMetadataInterface.php new file mode 100644 index 000000000000..fb5d735fa38a --- /dev/null +++ b/lib/internal/Magento/Framework/App/DistributionMetadataInterface.php @@ -0,0 +1,29 @@ +version; } + /** + * Get Distribution version + * + * @return string + */ + public function getDistributionVersion() + { + $this->distroVersion = $this->distroVersion ?: $this->cache->load(self::DISTRO_VERSION_CACHE_KEY); + if (!$this->distroVersion) { + if (!($this->distroVersion = $this->getSystemDistroVersion())) { + if ($this->getComposerInformation()->isMagentoRoot()) { + $this->distroVersion = $this->getComposerInformation()->getRootPackage()->getPrettyVersion(); + } else { + $this->distroVersion = 'UNKNOWN'; + } + } + $this->cache->save($this->distroVersion, self::DISTRO_VERSION_CACHE_KEY, [Config::CACHE_TAG]); + } + return $this->distroVersion; + } + /** * Get Product edition * @@ -100,11 +138,21 @@ public function getEdition() } /** - * Get Product name + * Get Distribution name * * @return string */ public function getName() + { + return self::DISTRIBUTION_NAME; + } + + /** + * Get Product name + * + * @return string + */ + public function getProductName() { return self::PRODUCT_NAME; } @@ -116,6 +164,22 @@ public function getName() * @deprecated 100.1.0 */ private function getSystemPackageVersion() + { + $packages = $this->getComposerInformation()->getSystemPackages(); + foreach ($packages as $package) { + if (isset($package['name']) && isset($package['magento_version'])) { + return $package['magento_version']; + } + } + return ''; + } + + /** + * Get distribution version from system package + * + * @return string + */ + private function getSystemDistroVersion() { $packages = $this->getComposerInformation()->getSystemPackages(); foreach ($packages as $package) { diff --git a/lib/internal/Magento/Framework/Composer/ComposerInformation.php b/lib/internal/Magento/Framework/Composer/ComposerInformation.php index a190f5597ca3..a27462241718 100644 --- a/lib/internal/Magento/Framework/Composer/ComposerInformation.php +++ b/lib/internal/Magento/Framework/Composer/ComposerInformation.php @@ -246,7 +246,8 @@ public function getSystemPackages() $packages[$package->getName()] = [ 'name' => $package->getName(), 'type' => $package->getType(), - 'version' => $package->getPrettyVersion() + 'version' => $package->getPrettyVersion(), + 'magento_version' => $package->getExtra()['magento_version'] ?? null, ]; } } diff --git a/lib/internal/Magento/Framework/Console/Cli.php b/lib/internal/Magento/Framework/Console/Cli.php index ca82e495e5a5..7cea5409e108 100644 --- a/lib/internal/Magento/Framework/Console/Cli.php +++ b/lib/internal/Magento/Framework/Console/Cli.php @@ -9,6 +9,7 @@ use Magento\Framework\App\Bootstrap; use Magento\Framework\App\DeploymentConfig; +use Magento\Framework\App\DistributionMetadataInterface; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\ProductMetadata; use Magento\Framework\Composer\ComposerJsonFinder; @@ -68,6 +69,11 @@ class Cli extends Console\Application */ private $logger; + /** + * @var ProductMetadata + */ + private $productMetadata; + /** * @param string $name the application name * @param string $version the application version @@ -97,8 +103,9 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') if ($version == 'UNKNOWN') { $directoryList = new DirectoryList(BP); $composerJsonFinder = new ComposerJsonFinder($directoryList); - $productMetadata = new ProductMetadata($composerJsonFinder); - $version = $productMetadata->getVersion(); + $this->productMetadata = new ProductMetadata($composerJsonFinder); + $name = $this->productMetadata->getName() . ' CLI'; + $version = $this->productMetadata->getDistributionVersion(); } parent::__construct($name, $version); @@ -232,4 +239,25 @@ protected function getVendorCommands($objectManager) return array_merge([], ...$commands); } + + /** + * Get system version info. + * + * @return string + */ + public function getLongVersion() + { + if (isset($this->productMetadata) + && $this->productMetadata instanceof DistributionMetadataInterface + && $this->productMetadata->getDistributionVersion() !== $this->productMetadata->getVersion()) { + return sprintf( + '%s (based on %s %s)', + parent::getLongVersion(), + $this->productMetadata->getProductName(), + $this->productMetadata->getVersion() + ); + } + + return parent::getLongVersion(); + } } diff --git a/setup/index.php b/setup/index.php index fd338f8f0c6e..93b9cf84d0b1 100644 --- a/setup/index.php +++ b/setup/index.php @@ -41,7 +41,7 @@ /** @var License $license */ $license = $licenseClass->getContents(); /** @var ProductMetadata $version */ -$version = $metaClass->getVersion(); +$version = $metaClass->getDistributionVersion(); $request = new Request(); $basePath = $request->getBasePath(); From 37713e687fcddb1b5496f8b717861dc4ee83f94e Mon Sep 17 00:00:00 2001 From: Ryan Hoerr Date: Mon, 13 Jan 2025 19:55:16 -0500 Subject: [PATCH 2/5] Split distribution name; PR fixes --- app/code/Magento/Backend/Block/Page/Footer.php | 2 +- .../Magento/Version/Controller/Index/Index.php | 2 +- .../Webapi/Model/Rest/Swagger/Generator.php | 2 +- .../Version/Controller/Index/IndexTest.php | 2 +- .../App/DistributionMetadataInterface.php | 14 ++++++++------ .../Magento/Framework/App/ProductMetadata.php | 16 ++++++++-------- lib/internal/Magento/Framework/Console/Cli.php | 2 +- 7 files changed, 21 insertions(+), 19 deletions(-) diff --git a/app/code/Magento/Backend/Block/Page/Footer.php b/app/code/Magento/Backend/Block/Page/Footer.php index 5ad3111b57bd..7818ce7c3edc 100644 --- a/app/code/Magento/Backend/Block/Page/Footer.php +++ b/app/code/Magento/Backend/Block/Page/Footer.php @@ -67,7 +67,7 @@ public function getMagentoVersion() */ public function getName() { - return $this->productMetadata->getName(); + return $this->productMetadata->getDistributionName(); } /** diff --git a/app/code/Magento/Version/Controller/Index/Index.php b/app/code/Magento/Version/Controller/Index/Index.php index 5d4a054564ee..b1a62eff7f86 100644 --- a/app/code/Magento/Version/Controller/Index/Index.php +++ b/app/code/Magento/Version/Controller/Index/Index.php @@ -57,7 +57,7 @@ public function execute() $versionParts = explode('.', $version); if (!$this->isGitBasedInstallation($version) && $this->isCorrectVersion($versionParts)) { $rawResponse->setContents( - $this->productMetadata->getName() . '/' . + $this->productMetadata->getDistributionName() . '/' . $this->getMajorMinorVersion($versionParts) . ' (' . $this->productMetadata->getEdition() . ')' ); diff --git a/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php b/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php index 2d42545735fb..4b4f10700f4c 100644 --- a/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php +++ b/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php @@ -191,7 +191,7 @@ protected function getGeneralInfo() return [ 'version' => $majorMinorVersion, - 'title' => $this->productMetadata->getName() . ' ' . $this->productMetadata->getEdition(), + 'title' => $this->productMetadata->getDistributionName() . ' ' . $this->productMetadata->getEdition(), ]; } diff --git a/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php b/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php index 4524b962a98b..18c1b5cc9884 100644 --- a/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php +++ b/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php @@ -19,7 +19,7 @@ public function testIndexAction() $name = $productMetadata->getName(); $edition = $productMetadata->getEdition(); - $fullVersion = $productMetadata->getDistributionVersion(); + $fullVersion = $productMetadata->getVersion(); if ($this->isComposerBasedInstallation($fullVersion)) { $versionParts = explode('.', $fullVersion); $majorMinor = $versionParts[0] . '.' . $versionParts[1]; diff --git a/lib/internal/Magento/Framework/App/DistributionMetadataInterface.php b/lib/internal/Magento/Framework/App/DistributionMetadataInterface.php index fb5d735fa38a..0654f5cd59f8 100644 --- a/lib/internal/Magento/Framework/App/DistributionMetadataInterface.php +++ b/lib/internal/Magento/Framework/App/DistributionMetadataInterface.php @@ -1,15 +1,17 @@ productMetadata = new ProductMetadata($composerJsonFinder); - $name = $this->productMetadata->getName() . ' CLI'; + $name = $this->productMetadata->getDistributionName() . ' CLI'; $version = $this->productMetadata->getDistributionVersion(); } From a1bced72b8dc8bb086291efcea66cc1d28c6738e Mon Sep 17 00:00:00 2001 From: Ryan Hoerr Date: Tue, 14 Jan 2025 21:01:44 -0500 Subject: [PATCH 3/5] Fixed method call --- lib/internal/Magento/Framework/Console/Cli.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Console/Cli.php b/lib/internal/Magento/Framework/Console/Cli.php index c848cebc3584..b1fb90dd3482 100644 --- a/lib/internal/Magento/Framework/Console/Cli.php +++ b/lib/internal/Magento/Framework/Console/Cli.php @@ -253,7 +253,7 @@ public function getLongVersion() return sprintf( '%s (based on %s %s)', parent::getLongVersion(), - $this->productMetadata->getProductName(), + $this->productMetadata->getName(), $this->productMetadata->getVersion() ); } From de902a9d34f1b9e5b1271b73293fe40b45e51530 Mon Sep 17 00:00:00 2001 From: Ryan Hoerr Date: Sun, 19 Jan 2025 15:46:30 -0500 Subject: [PATCH 4/5] Fixed test errors --- .../Version/Test/Unit/Controller/Index/IndexTest.php | 8 ++++++-- .../Magento/Version/Controller/Index/IndexTest.php | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Version/Test/Unit/Controller/Index/IndexTest.php b/app/code/Magento/Version/Test/Unit/Controller/Index/IndexTest.php index 2667a5b993ef..79d4c2765ab5 100644 --- a/app/code/Magento/Version/Test/Unit/Controller/Index/IndexTest.php +++ b/app/code/Magento/Version/Test/Unit/Controller/Index/IndexTest.php @@ -41,7 +41,7 @@ protected function setUp(): void $this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class) ->disableOriginalConstructor() - ->setMethods(['getName', 'getEdition', 'getVersion']) + ->setMethods(['getName', 'getEdition', 'getVersion', 'getDistributionName', 'getDistributionVersion']) ->getMockForAbstractClass(); $this->rawResponseFactoryMock = $this->createPartialMock(RawFactory::class, ['create']); @@ -78,9 +78,13 @@ public function testCommunityVersionDisplaysMajorMinorVersionAndEditionName(): v $this->productMetadataMock->expects($this->any())->method('getVersion')->willReturn('2.3.3'); $this->productMetadataMock->expects($this->any())->method('getEdition')->willReturn('Community'); $this->productMetadataMock->expects($this->any())->method('getName')->willReturn('Magento'); + $this->productMetadataMock->expects($this->any())->method('getDistributionVersion') + ->willReturn('1.1.0'); + $this->productMetadataMock->expects($this->any())->method('getDistributionName') + ->willReturn('Mage-OS'); $this->rawResponseMock->expects($this->once())->method('setContents') - ->with('Magento/2.3 (Community)') + ->with('Mage-OS/1.1 (Community)') ->willReturnSelf(); $this->versionController->execute(); diff --git a/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php b/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php index 18c1b5cc9884..a311ab6f621c 100644 --- a/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php +++ b/dev/tests/integration/testsuite/Magento/Version/Controller/Index/IndexTest.php @@ -16,10 +16,10 @@ public function testIndexAction() $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); /** @var \Magento\Framework\App\ProductMetadataInterface $productMetadata */ $productMetadata = $objectManager->get(\Magento\Framework\App\ProductMetadataInterface::class); - $name = $productMetadata->getName(); + $name = $productMetadata->getDistributionName(); $edition = $productMetadata->getEdition(); - $fullVersion = $productMetadata->getVersion(); + $fullVersion = $productMetadata->getDistributionVersion(); if ($this->isComposerBasedInstallation($fullVersion)) { $versionParts = explode('.', $fullVersion); $majorMinor = $versionParts[0] . '.' . $versionParts[1]; From a0490c9550b9a9e1d73a7515919ff192e9ecf30c Mon Sep 17 00:00:00 2001 From: Ryan Hoerr Date: Sun, 19 Jan 2025 16:02:57 -0500 Subject: [PATCH 5/5] Fixed Webapi unit test failure --- .../Webapi/Test/Unit/Model/Rest/Swagger/GeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Webapi/Test/Unit/Model/Rest/Swagger/GeneratorTest.php b/app/code/Magento/Webapi/Test/Unit/Model/Rest/Swagger/GeneratorTest.php index a40d66180666..163d17f7d629 100644 --- a/app/code/Magento/Webapi/Test/Unit/Model/Rest/Swagger/GeneratorTest.php +++ b/app/code/Magento/Webapi/Test/Unit/Model/Rest/Swagger/GeneratorTest.php @@ -181,7 +181,7 @@ public function testGenerate($serviceMetadata, $typeData, $schema) ); $this->productMetadata->expects($this->once()) - ->method('getVersion') + ->method('getDistributionVersion') ->willReturn('UNKNOWN'); $this->assertEquals(