Skip to content

Commit

Permalink
Feature mglaman#14
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiu-cristea authored Jul 10, 2024
1 parent 6f9e03f commit 847915c
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,76 @@
namespace ComposerDrupalLenient;

use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Package\Package;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PluginInterface;
use Composer\Plugin\PostFileDownloadEvent;
use Composer\Plugin\PrePoolCreateEvent;

final class Plugin implements PluginInterface, EventSubscriberInterface
{
private const string HOOK_IMPLEMENTATION = <<<HOOK_IMPLEMENTATION
/**
* Implements hook_system_info_alter().
*
* Added by mglaman/composer-drupal-lenient Composer plugin.
*/
function core_system_info_alter(&\$info, Extension \$file, \$type) {
\$info['core_incompatible'] = FALSE;
}
HOOK_IMPLEMENTATION;

private PackageRequiresAdjuster $packageRequiresAdjuster;
private Composer $composer;
private bool $hasAdjustedPackage = false;

public function modifyPackages(PrePoolCreateEvent $event): void
{
$packages = $event->getPackages();
foreach ($packages as $package) {
if ($this->packageRequiresAdjuster->applies($package)) {
$this->packageRequiresAdjuster->adjust($package);
$this->hasAdjustedPackage = true;
}
}
$event->setPackages($packages);

}

public function addSystemHookImplementation(PackageEvent $event): void
{
if (!$this->hasAdjustedPackage) {
return;
}

$operation = $event->getOperation();
assert($operation instanceof InstallOperation || $operation instanceof UpdateOperation);

$package = $this->getPackageFromOperation($operation);
if ($package->getType() === 'drupal-core') {
$systemModulePath = $this->composer->getInstallationManager()->getInstallPath($package) . '/modules/system/system.module';
$systemModule = file_get_contents($systemModulePath);
if (!str_contains($systemModule, self::HOOK_IMPLEMENTATION)) {
$systemModule .= self::HOOK_IMPLEMENTATION;
file_put_contents($systemModulePath, $systemModule);
}
}
}

public function activate(Composer $composer, IOInterface $io): void
{
$this->packageRequiresAdjuster = new PackageRequiresAdjuster($composer);
$this->composer = $composer;
}

public function deactivate(Composer $composer, IOInterface $io): void
Expand All @@ -43,6 +89,19 @@ public static function getSubscribedEvents(): array
{
return [
PluginEvents::PRE_POOL_CREATE => 'modifyPackages',
PackageEvents::POST_PACKAGE_INSTALL => 'addSystemHookImplementation',
PackageEvents::POST_PACKAGE_UPDATE => 'addSystemHookImplementation',
];
}

private function getPackageFromOperation(OperationInterface $operation): ?PackageInterface
{
if ($operation instanceof InstallOperation) {
return $operation->getPackage();
}
elseif ($operation instanceof UpdateOperation) {
return $operation->getTargetPackage();
}
throw new \Exception('Unknown operation: ' . get_class($operation));
}
}

0 comments on commit 847915c

Please sign in to comment.