Skip to content

Commit

Permalink
Split Magento and Mage-OS versions in ProductMetadata for compatibili…
Browse files Browse the repository at this point in the history
…ty (#115)
  • Loading branch information
rhoerr authored Jan 28, 2025
1 parent b30bda9 commit 4eb2b41
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 20 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->getDistributionName();
}

/**
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>
7 changes: 4 additions & 3 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,11 +53,11 @@ 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(
$this->productMetadata->getName() . '/' .
$this->productMetadata->getDistributionName() . '/' .
$this->getMajorMinorVersion($versionParts) .
' (' . $this->productMetadata->getEdition() . ')'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 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,15 +183,15 @@ 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
}
$majorMinorVersion = $versionParts[0] . '.' . $versionParts[1];

return [
'version' => $majorMinorVersion,
'title' => $this->productMetadata->getName() . ' ' . $this->productMetadata->getEdition(),
'title' => $this->productMetadata->getDistributionName() . ' ' . $this->productMetadata->getEdition(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function testGenerate($serviceMetadata, $typeData, $schema)
);

$this->productMetadata->expects($this->once())
->method('getVersion')
->method('getDistributionVersion')
->willReturn('UNKNOWN');

$this->assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* (c) Mage-OS
*
* For the full copyright and license information, please view the LICENSE
* files distributed with this source code.
*/
namespace Magento\Framework\App;

/**
* Mage-OS Distribution metadata
*
* @api
* @since 1.0.6
*/
interface DistributionMetadataInterface
{
/**
* Get Distribution version
*
* @return string
*/
public function getDistributionVersion();

/**
* Get Distribution name
*
* @return string
*/
public function getDistributionName();
}
68 changes: 66 additions & 2 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';
public const PRODUCT_NAME = 'Magento';

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

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

/**
* Distribution version cache key
*/
protected 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 @@ -109,13 +147,39 @@ public function getName()
return self::PRODUCT_NAME;
}

/**
* Get Distribution name
*
* @return string
*/
public function getDistributionName()
{
return self::DISTRIBUTION_NAME;
}

/**
* Get version from system package
*
* @return string
* @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->getDistributionName() . ' 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->getName(),
$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 4eb2b41

Please sign in to comment.