Skip to content

Commit

Permalink
Merge pull request #21 from marcelthole/check-package-name-before-ins…
Browse files Browse the repository at this point in the history
…tall

Only handle own install or update events
  • Loading branch information
sebastianfeldmann authored Apr 21, 2021
2 parents 925104e + a67bc7c commit 34af1d3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/ConfiguredMediator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function uninstall(Composer $composer, IOInterface $io)

public function installOrUpdateFunction(PackageEvent $event): void
{
if (!$this->isDesiredPackageEvent($event, $this->config->package())) {
return;
}
$gnuPG = $this->createGnuPG();
// we do not want to crash if no GnuPG was found
// but display a noticeable warning to the user
Expand Down
53 changes: 53 additions & 0 deletions src/OperationPackage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types = 1);
namespace PharIo\ComposerDistributor;

use Composer\Composer;
use Composer\DependencyResolver\GenericRule;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\Installer\PackageEvent;
use Composer\Package\CompletePackage;
use Composer\Package\Package;
use Composer\Semver\Constraint\MultiConstraint;
use RuntimeException;

class OperationPackage {

public static function createFromEvent(PackageEvent $event, string $pluginName): Package
{
if (0 >= \version_compare('2.0.0', Composer::VERSION)) {
$operation = $event->getOperation();

switch (true) {
case $operation instanceof InstallOperation:
case $operation instanceof UninstallOperation:
$package = $operation->getPackage();

break;
case $operation instanceof UpdateOperation:
$package = $operation->getTargetPackage();

break;
default:
throw new RuntimeException('No valid operation found');
}
} else {
/** @var GenericRule $rule */
$rule = $event->getOperation()->getReason();
/** @var MultiConstraint $constraint */
$constraint = $rule->getJob()['constraint'];

if ($rule->getRequiredPackage() !== $pluginName) {
throw SomebodyElsesProblem::here($pluginName);
}

/** @var CompletePackage $packages */
$package = $event->getInstalledRepo()->findPackage($rule->getRequiredPackage(), $constraint->getPrettyString());
}

return $package;
}
}
27 changes: 1 addition & 26 deletions src/PackageVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,7 @@ public function preRelease() : string

public static function fromPackageEvent(PackageEvent $event, string $pluginName) : self
{
$package = null;
if (0 >= version_compare('2.0.0', Composer::VERSION)) {
$operation = $event->getOperation();
switch (true) {
case $operation instanceof InstallOperation:
case $operation instanceof UninstallOperation:
$package = $operation->getPackage();
break;
case $operation instanceof UpdateOperation:
$package = $operation->getTargetPackage();
break;
default:
throw new RuntimeException('No valid operation found');
}
} else {
/** @var GenericRule $rule */
$rule = $event->getOperation()->getReason();
/** @var MultiConstraint $constraint */
$constraint = $rule->getJob()['constraint'];
if ($rule->getRequiredPackage() !== $pluginName) {
throw SomebodyElsesProblem::here($pluginName);
}

/** @var CompletePackage $packages */
$package = $event->getInstalledRepo()->findPackage($rule->getRequiredPackage(), $constraint->getPrettyString());
}
$package = OperationPackage::createFromEvent($event, $pluginName);
return new self($package->getName(), $package->getFullPrettyVersion());
}
}
7 changes: 7 additions & 0 deletions src/PluginBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@ protected function getComposer(): Composer
}
return $this->composer;
}

protected function isDesiredPackageEvent(PackageEvent $event, string $pluginName): bool
{
$package = OperationPackage::createFromEvent($event, $pluginName);

return $package->getName() === $pluginName;
}
}

0 comments on commit 34af1d3

Please sign in to comment.