Commit 7d40ee30 authored by omkar podey's avatar omkar podey Committed by Adam G-H
Browse files

Issue #3303900 by phenaproxima, omkar.podey: Remove PreApply check in...

Issue #3303900 by phenaproxima, omkar.podey: Remove PreApply check in PackagesInstalledWithComposerValidator
parent b7f76e54
Loading
Loading
Loading
Loading
+0 −6
Changes for automatic_updates_extensions/automatic_updates_extensions.services.yml: 0 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -13,12 +13,6 @@ services:
      - '@datetime.time'
      - '@PhpTuf\ComposerStager\Infrastructure\Factory\Path\PathFactoryInterface'
      - '@package_manager.failure_marker'
  automatic_updates_extensions.validator.packages_installed_with_composer:
    class: Drupal\automatic_updates_extensions\Validator\PackagesInstalledWithComposerValidator
    arguments:
      - '@string_translation'
    tags:
      - { name: event_subscriber }
  automatic_updates_extensions.validator.target_release:
    class: Drupal\automatic_updates_extensions\Validator\UpdateReleaseValidator
    tags:
+0 −98
Changes for automatic_updates_extensions/src/Validator/PackagesInstalledWithComposerValidator.php: 0 added lines, 98 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\automatic_updates_extensions\Validator;

use Composer\Package\PackageInterface;
use Drupal\automatic_updates_extensions\ExtensionUpdater;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\package_manager\Event\PreApplyEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Validates packages are installed via Composer.
 *
 * @todo Remove this validator completely in https://www.drupal.org/i/3303900.
 */
class PackagesInstalledWithComposerValidator implements EventSubscriberInterface {

  use StringTranslationTrait;

  /**
   * Constructs a InstalledPackagesValidator object.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   The translation service.
   */
  public function __construct(TranslationInterface $translation) {
    $this->setStringTranslation($translation);
  }

  /**
   * Validates that packages are installed with composer or not.
   *
   * @param \Drupal\package_manager\Event\PreApplyEvent $event
   *   The event object.
   */
  public function checkPackagesInstalledWithComposer(PreApplyEvent $event): void {
    $stage = $event->getStage();

    if (!$stage instanceof ExtensionUpdater) {
      return;
    }

    $missing_packages = $this->getPackagesNotInstalledWithComposer($event);
    if ($missing_packages) {
      // Removing drupal/ from package names for better user presentation.
      $missing_projects = str_replace('drupal/', '', array_keys($missing_packages));
      $event->addError($missing_projects, $this->t('Automatic Updates can only update projects that were installed via Composer. The following packages are not installed through composer:'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      PreApplyEvent::class => 'checkPackagesInstalledWithComposer',
    ];
  }

  /**
   * Gets the packages which aren't installed via composer.
   *
   * @param \Drupal\package_manager\Event\PreApplyEvent $event
   *   The event object.
   *
   * @return \Composer\Package\PackageInterface[]
   *   Packages not installed via composer.
   */
  protected function getPackagesNotInstalledWithComposer(PreApplyEvent $event): array {
    $stage = $event->getStage();
    $active_composer = $stage->getActiveComposer();

    $missing_packages = $stage->getStageComposer()->getPackagesNotIn($active_composer);

    // The core update system can only fetch release information for modules,
    // themes, or profiles that are in the active code base (whether they're
    // installed or not). If a package is not one of those types, ignore it
    // even if its vendor namespace is `drupal`.
    $types = [
      'drupal-module',
      'drupal-theme',
      'drupal-profile',
    ];
    $filter = function (PackageInterface $package) use ($types): bool {
      return in_array($package->getType(), $types, TRUE);
    };
    $missing_packages = array_filter($missing_packages, $filter);

    // The core update system can only fetch release information for drupal
    // projects, so saving only the packages whose name starts with drupal/.
    $missing_packages = array_filter($missing_packages, function (string $key) {
      return str_starts_with($key, 'drupal/');
    }, ARRAY_FILTER_USE_KEY);
    return $missing_packages;
  }

}
+0 −44
Changes for automatic_updates_extensions/tests/fixtures/packages_installed_with_composer_validator/active/vendor/composer/installed.json: 0 added lines, 44 removed lines.
Original line number Diff line number Diff line
{
    "packages": [
        {
            "name": "drupal/core-recommended",
            "version": "9.8.0",
            "require": {
                "drupal/core": "9.8.0"
            }
        },
        {
            "name": "drupal/core",
            "version": "9.8.0"
        },
        {
            "name": "drupal/my_module",
            "version": "9.8.0",
            "type": "drupal-module"
        },
        {
            "name": "drupal/my_dev_module",
            "version": "9.8.1",
            "type": "drupal-module"
        },
        {
          "name": "drupal/existing_module",
          "version": "9.8.0",
          "type": "drupal-module"
        },
        {
          "name": "drupal/existing_theme",
          "version": "9.8.0",
          "type": "drupal-theme"
        },
        {
          "name": "drupal/existing_profile",
          "version": "9.8.0",
          "type": "drupal-profile"
        }
    ],
    "dev": true,
    "dev-package-names": [
        "drupal/my_dev_module"
    ]
}
+0 −34
Changes for automatic_updates_extensions/tests/fixtures/packages_installed_with_composer_validator/module_not_installed_stage/vendor/composer/installed.json: 0 added lines, 34 removed lines.
Original line number Diff line number Diff line
{
    "packages": [
        {
            "name": "drupal/core-recommended",
            "version": "9.8.0",
            "require": {
                "drupal/core": "9.8.0"
            }
        },
        {
            "name": "drupal/core",
            "version": "9.8.0"
        },
        {
            "name": "drupal/my_module",
            "version": "9.8.0",
            "type": "drupal-module"
        },
        {
            "name": "drupal/my_dev_module",
            "version": "9.8.1",
            "type": "drupal-module"
        },
        {
          "name": "drupal/new_module",
          "version": "9.8.0",
          "type": "drupal-module"
        }
    ],
    "dev": true,
    "dev-package-names": [
        "drupal/my_dev_module"
    ]
}
+0 −54
Changes for automatic_updates_extensions/tests/fixtures/packages_installed_with_composer_validator/module_theme_profile_dependency_not_installed_stage/vendor/composer/installed.json: 0 added lines, 54 removed lines.
Original line number Diff line number Diff line
{
    "packages": [
        {
            "name": "drupal/core-recommended",
            "version": "9.8.0",
            "require": {
                "drupal/core": "9.8.0"
            }
        },
        {
            "name": "drupal/core",
            "version": "9.8.0"
        },
        {
            "name": "drupal/my_module",
            "version": "9.8.0",
            "type": "drupal-module"
        },
        {
            "name": "drupal/my_dev_module",
            "version": "9.8.1",
            "type": "drupal-module"
        },
        {
          "name": "drupal/new_module",
          "version": "9.8.0",
          "type": "drupal-module"
        },
        {
          "name": "not-drupal/new_module1",
          "version": "9.8.0",
          "type": "drupal-module"
        },
        {
          "name": "drupal/new_theme",
          "version": "9.8.0",
          "type": "drupal-theme"
        },
        {
          "name": "drupal/new_profile",
          "version": "9.8.0",
          "type": "drupal-profile"
        },
        {
          "name": "drupal/new_dependency",
          "version": "9.8.0",
          "type": "drupal-library"
        }
    ],
    "dev": true,
    "dev-package-names": [
        "drupal/my_dev_module"
    ]
}
Loading