Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] ProductMetadata/ComposerInformation rework #3494

Open
wants to merge 1 commit into
base: 2.10.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/module-elasticsuite-core/Model/ProductMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@

use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Composer\ComposerFactory;
use \Smile\ElasticsuiteCore\Model\ProductMetadata\ComposerInformation;
use Magento\Framework\Composer\ComposerJsonFinder;
use Smile\ElasticsuiteCore\Model\ProductMetadata\ComposerInformation;
use Smile\ElasticsuiteCore\Model\ProductMetadata\ComposerInformationProvider;

/**
* Composer Information model.
Expand All @@ -41,7 +39,7 @@ class ProductMetadata
const PRODUCT_NAME = 'Elasticsuite';

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

Expand All @@ -53,15 +51,20 @@ class ProductMetadata
protected $version;

/**
* @var \Magento\Framework\Composer\ComposerJsonFinder
* @var \Smile\ElasticsuiteCore\Model\ProductMetadata\ComposerInformationProvider
*/
protected $composerJsonFinder;
protected $composerInformationProvider;

/**
* @var \Smile\ElasticsuiteCore\Model\ProductMetadata\ComposerInformation
*/
private $composerInformation;

/**
* @var string
*/
private $packageName;

/**
* @var CacheInterface
*/
Expand All @@ -70,15 +73,18 @@ class ProductMetadata
/**
* ProductMetadata constructor.
*
* @param ComposerJsonFinder $composerJsonFinder Composer JSON finder
* @param \Magento\Framework\App\CacheInterface $cache Cache interface
* @param ComposerInformationProvider $composerInformationProvider Composer Information provider
* @param string $packageName Self package name
* @param CacheInterface $cache Cache interface
*/
public function __construct(
ComposerJsonFinder $composerJsonFinder,
ComposerInformationProvider $composerInformationProvider,
string $packageName = 'smile/elasticsuite',
CacheInterface $cache = null
) {
$this->composerJsonFinder = $composerJsonFinder;
$this->cache = $cache ?: ObjectManager::getInstance()->get(CacheInterface::class);
$this->composerInformationProvider = $composerInformationProvider;
$this->packageName = $packageName;
$this->cache = $cache ?: ObjectManager::getInstance()->get(CacheInterface::class);
}

/**
Expand Down Expand Up @@ -136,8 +142,8 @@ private function getSystemPackageVersion()
$packages = $this->getComposerInformation()->getSystemPackages();

foreach ($packages as $package) {
if (isset($package['name']) && isset($package['version'])) {
return $package['version'];
if (isset($package['name']) && ($package['name'] === $this->packageName)) {
return $package['version'] ?? '';
}
}

Expand All @@ -152,9 +158,7 @@ private function getSystemPackageVersion()
private function getComposerInformation()
{
if (!$this->composerInformation) {
$directoryList = new DirectoryList(BP);
$composerFactory = new ComposerFactory($directoryList, $this->composerJsonFinder);
$this->composerInformation = new ComposerInformation($composerFactory);
$this->composerInformation = $this->composerInformationProvider->getComposerInformation();
}

return $this->composerInformation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
namespace Smile\ElasticsuiteCore\Model\ProductMetadata;

use Composer\Package\CompletePackageInterface;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Composer\ComposerFactory;
use Smile\ElasticsuiteCore\Helper\Cache;

/**
* Composer Information model.
Expand All @@ -26,6 +29,11 @@
*/
class ComposerInformation extends \Magento\Framework\Composer\ComposerInformation
{
/**
* Elasticsuite packages list cache key
*/
const PACKAGES_CACHE_KEY = 'elasticsuite-packages';

/**
* @var \Composer\Composer
*/
Expand All @@ -42,13 +50,29 @@ class ComposerInformation extends \Magento\Framework\Composer\ComposerInformatio
private $composerFactory;

/**
* @param \Magento\Framework\Composer\ComposerFactory $composerFactory Composer Factory
* @var Cache
*/
public function __construct(ComposerFactory $composerFactory)
{
private $cache;

/**
* @var array
*/
private $packages;

/**
* Constructor.
*
* @param ComposerFactory $composerFactory Composer Factory
* @param Cache $cache Elasticsuite cache helper
*/
public function __construct(
ComposerFactory $composerFactory,
Cache $cache
) {
parent::__construct($composerFactory);

$this->composerFactory = $composerFactory;
$this->cache = $cache;
}

/**
Expand All @@ -58,20 +82,27 @@ public function __construct(ComposerFactory $composerFactory)
*/
public function getSystemPackages()
{
$packages = [];
/** @var CompletePackageInterface $package */

foreach ($this->getLocker()->getLockedRepository()->getPackages() as $package) {
if ($this->isSystemPackage($package->getName())) {
$packages[$package->getName()] = [
'name' => $package->getName(),
'type' => $package->getType(),
'version' => $package->getPrettyVersion(),
];
if (null === $this->packages) {
$packages = $this->cache->loadCache(self::PACKAGES_CACHE_KEY);
if (!is_array($packages)) {
$packages = [];
/** @var CompletePackageInterface $package */
foreach ($this->getLocker()->getLockedRepository()->getPackages() as $package) {
if ($this->isSystemPackage($package->getName())) {
$packages[$package->getName()] = [
'name' => $package->getName(),
'type' => $package->getType(),
'version' => $package->getPrettyVersion(),
];
}
}
$this->cache->saveCache(self::PACKAGES_CACHE_KEY, $packages, [Config::CACHE_TAG]);
}

$this->packages = $packages;
}

return $packages;
return $this->packages;
}

/**
Expand All @@ -83,7 +114,7 @@ public function getSystemPackages()
*/
public function isSystemPackage($packageName = '')
{
if (preg_match('/smile\/elasticsuite/', $packageName) == 1) {
if (preg_match('/smile\/(module-)?elasticsuite/', $packageName) == 1) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future.
*
* @category Smile
* @package Smile\Elasticsuite
* @author Richard BAYET <[email protected]>
* @copyright 2025 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Model\ProductMetadata;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Composer\ComposerFactory;
use Magento\Framework\Composer\ComposerJsonFinder;
use Smile\ElasticsuiteCore\Helper\Cache;

/**
* Composer information model provider.
* Helps sharing the same properly initialized composer information model (correct composer factory) between multiple models.
*
* @category Smile
* @package Smile\ElasticsuiteCore
*/
class ComposerInformationProvider
{
/** @var ComposerJsonFinder */
private $composerJsonFinder;

/** @var Cache */
private $cache;

/** @var ComposerInformation */
private $composerInformation;

/**
* Constructor
*
* @param ComposerJsonFinder $composerJsonFinder Composer JSON finder
* @param Cache $cache Elasticsuite cache helper
*/
public function __construct(
ComposerJsonFinder $composerJsonFinder,
Cache $cache
) {
$this->composerJsonFinder = $composerJsonFinder;
$this->cache = $cache;
}

/**
* Return a properly initialized Elasticsuite Composer Information model.
*
* @return ComposerInformation
*/
public function getComposerInformation(): ComposerInformation
{
if (null === $this->composerInformation) {
$this->composerInformation = new ComposerInformation(
new ComposerFactory(new DirectoryList(BP), $this->composerJsonFinder),
$this->cache
);
}

return $this->composerInformation;
}
}
Loading