Skip to content

Commit

Permalink
Update for composer 2.0 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
netwarex committed Apr 6, 2021
1 parent 7687268 commit 30a8b82
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.phpunit.result.cache
/build/
/composer.*
!/composer.json
Expand Down
7 changes: 5 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Select image from https://hub.docker.com/_/php/
image: php:7.1-alpine
image: php:7.4-alpine3.10

# Select what we should cache between builds
cache:
Expand All @@ -10,7 +10,7 @@ before_script:
# Install additional php extensions
- docker-php-source extract
- apk add --no-cache build-base autoconf
- pecl install xdebug-2.5.3
- pecl install xdebug-2.9.0
- docker-php-ext-enable xdebug
# Install and run composer
- curl -sS https://getcomposer.org/installer | php
Expand All @@ -22,3 +22,6 @@ before_script:
test:
script:
- phpunit --configuration phpunit.xml.dist --coverage-text --colors=never
only:
- branches
- merge_requests
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"license": "MIT",
"type": "composer-plugin",
"require": {
"php": ">=7.1.0",
"composer-plugin-api": "^1.0"
"php": ">=7.4",
"composer-plugin-api": "^2.0"
},
"require-dev": {
"composer/composer": "^1.4",
"phpunit/phpunit": "^6.1"
"composer/composer": "^2.0",
"phpunit/phpunit": "^9.4"
},
"autoload": {
"psr-4": {
Expand Down
27 changes: 10 additions & 17 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
stopOnError="true"
stopOnFailure="true">

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false"
bootstrap="vendor/autoload.php" colors="true" stopOnError="false" stopOnFailure="false">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="LicenseCheck TestSuite">
<directory suffix="Test.php">tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
22 changes: 16 additions & 6 deletions src/Command/CheckLicensesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

final class CheckLicensesCommand extends BaseCommand
{
protected function configure()
protected function configure(): void
{
$this
->setName('check-licenses')
Expand All @@ -35,7 +35,7 @@ protected function configure()
;
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$composer = $this->getComposer();

Expand Down Expand Up @@ -67,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$table = new Table($output);
$table->setStyle('compact');
$table->getStyle()->setVerticalBorderChar('');
$table->getStyle()->setVerticalBorderChars('', '');
$table->getStyle()->setCellRowContentFormat('%s ');
$table->setHeaders(['Name', 'Version', 'License', 'Allowed to Use?']);
/** @noinspection ForeachSourceInspection */
Expand All @@ -76,9 +76,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$dependencyName,
$dependency['version'],
implode(', ', $dependency['license']) ?: 'none',
$dependency['allowed_to_use'] ? 'yes' : 'no',
$dependency['allowed_to_use'] ? 'yes' : 'no' . ($dependency['whitelisted'] ? ' (whitelisted)' : ''),
]);
$violationFound = $violationFound || !$dependency['allowed_to_use'];
$violationFound = $violationFound || (!$dependency['allowed_to_use'] && !$dependency['whitelisted']);
}
$table->render();
break;
Expand Down Expand Up @@ -116,11 +116,12 @@ private function calculatePackagesInfo(PackageInterface $rootPackage, array $pac

private function calculatePackageInfo(PackageInterface $rootPackage, CompletePackageInterface $package): array
{
$allowedToUse = true;
$allowedToUse = true; $whitelisted = false;

$extraConfigKey = 'metasyntactical/composer-plugin-license-check';
$whitelist = [];
$blacklist = [];
$whitelistedPackages = [];
if (array_key_exists($extraConfigKey, $rootPackage->getExtra())
&& is_array($rootPackage->getExtra()[$extraConfigKey])
) {
Expand All @@ -134,6 +135,11 @@ private function calculatePackageInfo(PackageInterface $rootPackage, CompletePac
) {
$blacklist = (array) $rootPackage->getExtra()[$extraConfigKey]['blacklist'];
}
if (array_key_exists('whitelisted-packages', $rootPackage->getExtra()[$extraConfigKey])
&& in_array(gettype($rootPackage->getExtra()[$extraConfigKey]['whitelisted-packages']), ['array'], true)
) {
$whitelistedPackages = (array) $rootPackage->getExtra()[$extraConfigKey]['whitelisted-packages'];
}
}

if ($allowedToUse && $blacklist) {
Expand All @@ -142,6 +148,9 @@ private function calculatePackageInfo(PackageInterface $rootPackage, CompletePac
if ($allowedToUse && $whitelist) {
$allowedToUse = !!array_intersect($package->getLicense(), $whitelist);
}
if (!$allowedToUse && array_key_exists($package->getPrettyName(), $whitelistedPackages)) {
$whitelisted = true;
}

if ($package->getName() === 'metasyntactical/composer-plugin-license-check') {
$allowedToUse = true;
Expand All @@ -151,6 +160,7 @@ private function calculatePackageInfo(PackageInterface $rootPackage, CompletePac
'version' => $package->getFullPrettyVersion(),
'license' => $package->getLicense(),
'allowed_to_use' => $allowedToUse,
'whitelisted' => $whitelisted,
];
}

Expand Down
58 changes: 32 additions & 26 deletions src/LicenseCheckPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,15 @@ final class LicenseCheckPlugin
{
private const PLUGIN_PACKAGE_NAME = 'metasyntactical/composer-plugin-license-check';

/**
* @var Composer
*/
private $composer;
private Composer $composer;

/**
* @var IOInterface
*/
private $io;
private IOInterface $io;

private $licenseWhitelist = [];
private array $licenseWhitelist = [];

private $licenseBlacklist = [];
private array $licenseBlacklist = [];

#
# PluginInterface
#
private array $whitelistedPackages = [];

public function activate(Composer $composer, IOInterface $io): void
{
Expand All @@ -63,12 +55,19 @@ public function activate(Composer $composer, IOInterface $io): void
) {
$this->licenseBlacklist = (array) $rootPackage->getExtra()[$extraConfigKey]['blacklist'];
}
if (array_key_exists('whitelisted-packages', $rootPackage->getExtra()[$extraConfigKey])
&& in_array(gettype($rootPackage->getExtra()[$extraConfigKey]['whitelisted-packages']), ['array'], true)
) {
$this->whitelistedPackages = (array) $rootPackage->getExtra()[$extraConfigKey]['whitelisted-packages'];
}
}
}

#
# CapableInterface
#
public function deactivate(Composer $composer, IOInterface $io)
{}

public function uninstall(Composer $composer, IOInterface $io)
{}

public function getCapabilities(): array
{
Expand All @@ -77,9 +76,6 @@ public function getCapabilities(): array
];
}

#
# EventSubscriberInterface
#
public static function getSubscribedEvents(): array
{
return [
Expand All @@ -104,21 +100,22 @@ public function handleEventAndOutputDebugMessage(CommandEvent $event): void
public function handleEventAndCheckLicense(PackageEvent $event): void
{
$operation = $event->getOperation();
if (!in_array($operation->getJobType(), ['install', 'update'], true)) {
$operationType = (method_exists($operation, 'getJobType')) ? $operation->getJobType() : $operation->getOperationType();
if (!in_array($operationType, ['install', 'update'], true)) {
return;
}

$package = null;
if ($event->getOperation()->getJobType() === 'install') {
if ($operationType === 'install') {
/** @var InstallOperation $operation */
$package = $operation->getPackage();
}
if ($event->getOperation()->getJobType() === 'update') {
if ($operationType === 'update') {
/** @var UpdateOperation $operation */
$package = $operation->getTargetPackage();
}

if ($package->getName() === self::PLUGIN_PACKAGE_NAME && $event->getOperation()->getJobType() === 'install') {
if ($package->getName() === self::PLUGIN_PACKAGE_NAME && $operationType === 'install') {
$this->composer->getEventDispatcher()->addSubscriber($this);
if ($event->getIO()->isVerbose()) {
$event->getIO()->writeError('<info>The Metasyntactical LicenseCheck Plugin has been enabled.</info>');
Expand Down Expand Up @@ -151,13 +148,22 @@ public function handleEventAndCheckLicense(PackageEvent $event): void
}

if (!$allowedToUse) {
throw new LicenseNotAllowedException(
if (!array_key_exists($package->getPrettyName(), $this->whitelistedPackages)) {
throw new LicenseNotAllowedException(
sprintf(
'ERROR: Licenses "%s" of package "%s" are not allowed to be used in the project. Installation failed.',
implode(', ', $packageLicenses),
$package->getPrettyName()
)
);
}
$this->io->writeError(
sprintf(
'License "%s" of package "%s" is not allowed to be used in the project. Installation failed.',
'WARNING: Licenses "%s" of package "%s" are not allowed to be used in the project but the package has been whitelisted.',
implode(', ', $packageLicenses),
$package->getPrettyName()
)
);
}
}
}
}
Loading

0 comments on commit 30a8b82

Please sign in to comment.