Skip to content
Snippets Groups Projects
Commit 58c9abd3 authored by Kunal Sachdev's avatar Kunal Sachdev Committed by Adam G-H
Browse files

Issue #3354594 by kunal.sachdev: Merge ComposerSettingsValidator into ComposerValidator

parent 9b381244
No related branches found
No related tags found
No related merge requests found
......@@ -92,10 +92,6 @@ services:
tags:
- { name: event_subscriber }
Drupal\package_manager\Validator\WritableFileSystemValidator: '@package_manager.validator.file_system'
package_manager.validator.composer_settings:
class: Drupal\package_manager\Validator\ComposerSettingsValidator
tags:
- { name: event_subscriber }
package_manger.validator.composer_minimum_stability:
class: Drupal\package_manager\Validator\ComposerMinimumStabilityValidator
tags:
......
<?php
declare(strict_types = 1);
namespace Drupal\package_manager\Validator;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\package_manager\ComposerInspector;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\Event\PreOperationStageEvent;
use Drupal\package_manager\Event\StatusCheckEvent;
use Drupal\package_manager\PathLocator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Validates certain Composer settings.
*
* @internal
* This is an internal part of Package Manager and may be changed or removed
* at any time without warning. External code should not interact with this
* class.
*/
final class ComposerSettingsValidator implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* Constructs a ComposerSettingsValidator object.
*
* @param \Drupal\package_manager\ComposerInspector $inspector
* The Composer inspector service.
* @param \Drupal\package_manager\PathLocator $pathLocator
* The path locator service.
*/
public function __construct(protected ComposerInspector $inspector, protected PathLocator $pathLocator) {
}
/**
* Validates Composer settings.
*/
public function validate(PreOperationStageEvent $event): void {
$dir = $event instanceof PreApplyEvent
? $event->stage->getStageDirectory()
: $this->pathLocator->getProjectRoot();
$settings = [];
foreach (['disable-tls', 'secure-http'] as $key) {
try {
$settings[$key] = ComposerInspector::toBoolean($this->inspector->getConfig($key, $dir) ?: '0');
}
catch (\Throwable $throwable) {
$event->addErrorFromThrowable($throwable, $this->t('Unable to determine Composer <code>@key</code> setting.', [
'@key' => $key,
]));
return;
}
}
// If disable-tls is enabled, it overrides secure-http and sets its value to
// FALSE, even if secure-http is set to TRUE explicitly.
$messages = [];
if ($settings['disable-tls'] === TRUE) {
$messages[] = $this->t('TLS must be enabled for HTTPS Composer downloads. See <a href=":url">the Composer documentation</a> for more information.', [
':url' => 'https://getcomposer.org/doc/06-config.md#disable-tls',
]);
$messages[] = $this->t('You should also check the value of <code>secure-http</code> and make sure that it is set to <code>true</code> or not set at all.');
}
elseif ($settings['secure-http'] !== TRUE) {
$messages[] = $this->t('HTTPS must be enabled for Composer downloads. See <a href=":url">the Composer documentation</a> for more information.', [
':url' => 'https://getcomposer.org/doc/06-config.md#secure-http',
]);
}
if ($messages) {
$event->addError($messages, $this->t("Composer settings don't satisfy Package Manager's requirements."));
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
PreCreateEvent::class => 'validate',
PreApplyEvent::class => 'validate',
StatusCheckEvent::class => 'validate',
];
}
}
......@@ -7,6 +7,7 @@ namespace Drupal\package_manager\Validator;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Url;
use Drupal\package_manager\ComposerInspector;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Event\PreOperationStageEvent;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\package_manager\PathLocator;
......@@ -45,8 +46,12 @@ class ComposerValidator implements EventSubscriberInterface {
* Validates that the Composer executable is the correct version.
*/
public function validate(PreOperationStageEvent $event): void {
$messages = [];
$dir = $event instanceof PreApplyEvent
? $event->stage->getStageDirectory()
: $this->pathLocator->getProjectRoot();
try {
$this->composerInspector->validate($this->pathLocator->getProjectRoot());
$this->composerInspector->validate($dir);
}
catch (\Throwable $e) {
if ($this->moduleHandler->moduleExists('help')) {
......@@ -63,7 +68,38 @@ class ComposerValidator implements EventSubscriberInterface {
else {
$event->addErrorFromThrowable($e);
}
return;
}
$settings = [];
foreach (['disable-tls', 'secure-http'] as $key) {
try {
$settings[$key] = ComposerInspector::toBoolean($this->composerInspector->getConfig($key, $dir) ?: '0');
}
catch (\Throwable $e) {
$event->addErrorFromThrowable($e, $this->t('Unable to determine Composer <code>@key</code> setting.', [
'@key' => $key,
]));
return;
}
}
// If disable-tls is enabled, it overrides secure-http and sets its value to
// FALSE, even if secure-http is set to TRUE explicitly.
if ($settings['disable-tls'] === TRUE) {
$messages[] = $this->t('TLS must be enabled for HTTPS Composer downloads. See <a href=":url">the Composer documentation</a> for more information.', [
':url' => 'https://getcomposer.org/doc/06-config.md#disable-tls',
]);
$messages[] = $this->t('You should also check the value of <code>secure-http</code> and make sure that it is set to <code>true</code> or not set at all.');
}
elseif ($settings['secure-http'] !== TRUE) {
$messages[] = $this->t('HTTPS must be enabled for Composer downloads. See <a href=":url">the Composer documentation</a> for more information.', [
':url' => 'https://getcomposer.org/doc/06-config.md#secure-http',
]);
}
if ($messages) {
$event->addError($messages, $this->t("Composer settings don't satisfy Package Manager's requirements."));
}
}
......
......@@ -10,11 +10,11 @@ use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\ValidationResult;
/**
* @covers \Drupal\package_manager\Validator\ComposerSettingsValidator
* @covers \Drupal\package_manager\Validator\ComposerValidator
* @group package_manager
* @internal
*/
class ComposerSettingsValidatorTest extends PackageManagerKernelTestBase {
class ComposerValidatorTest extends PackageManagerKernelTestBase {
/**
* Data provider for testComposerSettingsValidation().
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment