Skip to content

Commit

Permalink
feat: separate Mage-OS vs Magento version in ProductMetadata for compat
Browse files Browse the repository at this point in the history
  • Loading branch information
rhoerr committed Jan 13, 2025
1 parent b30bda9 commit 7cc14d0
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 15 deletions.
16 changes: 14 additions & 2 deletions app/code/Magento/Backend/Block/Page/Footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Backend\Block\Page;

use Magento\Framework\App\DistributionMetadataInterface;

/**
* Adminhtml footer block
*
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ use Magento\Framework\Escaper;
/** @var Footer $block */
?>
<p class="magento-version">
<strong><?= $escaper->escapeHtml(__('Mage-OS')); ?></strong>
<strong><?= $escaper->escapeHtml(__($block->getName())); ?></strong>
<?= $escaper->escapeHtml(__('ver. %1', $block->getMagentoVersion())); ?>
</p>
5 changes: 3 additions & 2 deletions app/code/Magento/Version/Controller/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

/**
* @var ProductMetadataInterface
* @var ProductMetadataInterface|DistributionMetadataInterface
*/
private $productMetadata;

Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,7 +53,7 @@ class Generator extends AbstractSchemaGenerator
/**
* Magento product metadata
*
* @var ProductMetadataInterface
* @var ProductMetadataInterface|DistributionMetadataInterface
*/
protected ProductMetadataInterface $productMetadata;

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App;

/**
* Magento application product metadata
*
* @api
* @since 100.0.2
*/
interface DistributionMetadataInterface
{
/**
* Get Distribution version
*
* @return string
*/
public function getDistributionVersion();

/**
* Get Product name
*
* @return string
*/
public function getProductName();
}
70 changes: 67 additions & 3 deletions lib/internal/Magento/Framework/App/ProductMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Magento application product metadata
*/
class ProductMetadata implements ProductMetadataInterface
class ProductMetadata implements ProductMetadataInterface, DistributionMetadataInterface
{
/**
* Magento product edition
Expand All @@ -25,20 +25,37 @@ class ProductMetadata implements ProductMetadataInterface
/**
* Magento product name
*/
const PRODUCT_NAME = 'Mage-OS';
const PRODUCT_NAME = 'Magento';

/**
* Distribution product name
*/
const DISTRIBUTION_NAME = 'Mage-OS';

/**
* Magento version cache key
*/
const VERSION_CACHE_KEY = 'mage-version';

/**
* Distribution version cache key
*/
const DISTRO_VERSION_CACHE_KEY = 'distro-version';

/**
* Product version
*
* @var string
*/
protected $version;

/**
* Distribution version
*
* @var string
*/
protected $distroVersion;

/**
* @var \Magento\Framework\Composer\ComposerJsonFinder
* @deprecated 100.1.0
Expand Down Expand Up @@ -89,6 +106,27 @@ public function getVersion()
return $this->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
*
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
}
Expand Down
32 changes: 30 additions & 2 deletions lib/internal/Magento/Framework/Console/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion setup/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7cc14d0

Please sign in to comment.