Skip to content

Commit

Permalink
Merge branch '2.11.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayet committed Jan 23, 2025
2 parents 23af5fe + 17edcca commit 7e9a011
Show file tree
Hide file tree
Showing 11 changed files with 532 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?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\ElasticsuiteCore
* @author Richard BAYET <[email protected]>
* @copyright 2025 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCore\Model\Healthcheck;

use Smile\ElasticsuiteCore\Api\Healthcheck\CheckInterface;
use Smile\ElasticsuiteCore\Model\ProductMetadata\ComposerInformationProvider;

/**
* Presence of Elasticsuite Hyva compatibility module(s) check.
* Checks that, if Hyva theme package is installed,
* the specific Elasticsuite/Hyva compatibility module(s) are also installed.
*
* @category Smile
* @package Smile\ElasticsuiteCore
*/
class HyvaCompatibilityCheck implements CheckInterface
{
/** @var ComposerInformationProvider */
private $composerInformationProvider;

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

/** @var boolean */
private $isHyvaInstalled;

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

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

/**
* Constructor.
*
* @param ComposerInformationProvider $composerInformationProvider Composer information provider.
* @param string $triggerPackage Hÿva package name triggering the check.
* @param array $packagesToCheck Required Hÿva compatibility packages.
*/
public function __construct(
ComposerInformationProvider $composerInformationProvider,
string $triggerPackage = 'hyva-themes/magento2-default-theme',
array $packagesToCheck = []
) {
$this->composerInformationProvider = $composerInformationProvider;
$this->triggerPackage = $triggerPackage;
$this->packagesToCheck = $packagesToCheck;
}

/**
* {@inheritDoc}
*/
public function getIdentifier(): string
{
return 'hyva_compatibility_check';
}

/**
* {@inheritDoc}
*/
public function getStatus(): string
{
return ($this->hasPackagesErrors() ? 'warning' : 'success');
}

/**
* {@inheritDoc}
*/
public function getDescription(): string
{
$description = __(
'All the required Hÿva/Elasticsuite compatibility modules (%1) are correctly installed.',
implode(', ', $this->packagesToCheck)
);
if (!$this->isHyvaInstalled()) {
$description = __(
'Your site is not using the Hÿva theme, there are no specific requirements to fulfill.'
);
}

if ($this->hasPackagesErrors()) {
$errors = implode(', ', $this->getPackagesErrors());
// @codingStandardsIgnoreStart
$description = implode(
'<br />',
[
__(
'Your site uses the Hÿva theme through package <strong>%1</strong>. To work properly with Hÿva, Elasticsuite requires the installation of additional <strong>compatibility modules (%2)</strong>.',
$this->triggerPackage,
implode(', ', $this->packagesToCheck)
),
((count($this->getPackagesErrors()) > 1) ?
__(
'The compatibility modules <strong>%1 are missing</strong>. Please install them through composer.',
$errors,
) :
__(
'The compatibility module <strong>%1 is missing</strong>. Please install it through composer.',
$errors,
)
),
]
);
// @codingStandardsIgnoreEnd
}

return $description;
}

/**
* {@inheritDoc}
*/
public function getSortOrder(): int
{
return 40; // Adjust as necessary.
}

/**
* Returns true if the Hÿva theme package is installed.
*
* @return bool
*/
private function isHyvaInstalled(): bool
{
if (null === $this->isHyvaInstalled) {
$this->isHyvaInstalled = array_key_exists(
$this->triggerPackage,
$this->composerInformationProvider->getComposerInformation()->getSystemPackages()
);
}

return $this->isHyvaInstalled;
}

/**
* Return true if there is at least one system package having a mismatched composer version.
*
* @return bool
*/
private function hasPackagesErrors(): bool
{
return !empty($this->getPackagesErrors());
}

/**
* Return the list of packages having a mismatched composer version.
*
* @return array
*/
private function getPackagesErrors(): array
{
if (null === $this->packagesErrors) {
$this->packagesErrors = [];
if (!empty($this->packagesToCheck)) {
if ($this->isHyvaInstalled()) {
$systemPackages = $this->composerInformationProvider->getComposerInformation()->getSystemPackages();
foreach ($this->packagesToCheck as $packageName) {
if (false === array_key_exists($packageName, $systemPackages)) {
$this->packagesErrors[$packageName] = $packageName;
}
}
ksort($this->packagesErrors);
}
}
}

return $this->packagesErrors;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?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\Healthcheck;

use Smile\ElasticsuiteCore\Api\Healthcheck\CheckInterface;
use Smile\ElasticsuiteCore\Model\ProductMetadata;
use Smile\ElasticsuiteCore\Model\ProductMetadata\ComposerInformationProvider;

/**
* Elasticsuite packages versions mismatch check.
* Checks that known Elasticsuite packages that require to be in the same version as the core package
* are actually complying with that requirement.
*
* @category Smile
* @package Smile\ElasticsuiteCore
*/
class PackageVersionsMismatchCheck implements CheckInterface
{
/** @var ProductMetadata */
private $productMetadata;

/** @var ComposerInformationProvider */
private $composerInformationProvider;

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

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

/**
* Constructor.
*
* @param ProductMetadata $productMetadata Elasticsuite product metadata.
* @param ComposerInformationProvider $composerInformationProvider Composer information provider.
* @param array $packagesToCheck List of packages names to check.
*/
public function __construct(
ProductMetadata $productMetadata,
ComposerInformationProvider $composerInformationProvider,
array $packagesToCheck = []
) {
$this->productMetadata = $productMetadata;
$this->composerInformationProvider = $composerInformationProvider;
$this->packagesToCheck = $packagesToCheck;
}

/**
* {@inheritDoc}
*/
public function getIdentifier(): string
{
return 'packages_version_check';
}

/**
* {@inheritDoc}
*/
public function getStatus(): string
{
return ($this->hasPackagesErrors() ? 'warning' : 'success');
}

/**
* {@inheritDoc}
*/
public function getDescription(): string
{
$description = __(
'All additional Elasticsuite packages are in the same version as the Elasticsuite core package (smile/elasticsuite).'
);

if ($this->hasPackagesErrors()) {
$errors = [];
$errors[] = '<ul>';
foreach ($this->getPackagesErrors() as $packageName => $packageVersion) {
$errors[] = sprintf("<li>%s (<em>%s</em>)</li>", $packageName, $packageVersion);
}
$errors[] = '</ul>';

// @codingStandardsIgnoreStart
$description = implode(
'<br />',
[
__(
'Some additional Elasticsuite packages are <strong>not in the same version</strong> as the Elasticsuite core package <strong>smile/elasticsuite</strong> which is in version <strong>%1</strong>.',
$this->productMetadata->getVersion()
),
__(
'You should <strong>update</strong> either the core or those additional <strong>Elasticsuite packages through composer</strong> so they share the exact same version.'
),
__(
'Those composer packages are: %1',
implode(' ', $errors)
),
]
);
// @codingStandardsIgnoreEnd
}

return $description;
}

/**
* {@inheritDoc}
*/
public function isDisplayed(): bool
{
return true;
}

/**
* {@inheritDoc}
*/
public function getSortOrder(): int
{
return 40; // Adjust as necessary.
}

/**
* Return true if there is at least one system package having a mismatched composer version.
*
* @return bool
*/
private function hasPackagesErrors(): bool
{
return !empty($this->getPackagesErrors());
}

/**
* Return the list of packages having a mismatched composer version.
*
* @return array
*/
private function getPackagesErrors(): array
{
if (null === $this->packagesErrors) {
if (!empty($this->packagesToCheck)) {
$corePackageVersion = $this->productMetadata->getVersion();
$systemPackages = $this->composerInformationProvider->getComposerInformation()->getSystemPackages();
foreach ($this->packagesToCheck as $packageName) {
$packageVersion = $systemPackages[$packageName] ?? 'N/A';
if ($packageVersion !== $corePackageVersion) {
$this->packagesErrors[$packageName] = $packageVersion;
}
}
ksort($this->packagesErrors);
}
}

return $this->packagesErrors;
}
}
Loading

0 comments on commit 7e9a011

Please sign in to comment.