-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from marcelthole/check-package-name-before-ins…
…tall Only handle own install or update events
- Loading branch information
Showing
4 changed files
with
64 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters