Skip to content
Snippets Groups Projects

Issue #3261847: Add helpful methods to compute the difference between two ComposerUtility objects

Merged Issue #3261847: Add helpful methods to compute the difference between two ComposerUtility objects
All threads resolved!
All threads resolved!
Files
8
@@ -6,6 +6,7 @@ use Composer\Composer;
@@ -6,6 +6,7 @@ use Composer\Composer;
use Composer\Factory;
use Composer\Factory;
use Composer\IO\NullIO;
use Composer\IO\NullIO;
use Composer\Package\PackageInterface;
use Composer\Package\PackageInterface;
 
use Composer\Semver\Comparator;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Serialization\Json;
/**
/**
@@ -133,35 +134,13 @@ class ComposerUtility {
@@ -133,35 +134,13 @@ class ComposerUtility {
return array_values($core_packages);
return array_values($core_packages);
}
}
/**
* Returns all Drupal extension packages in the lock file.
*
* The following package types are considered Drupal extension packages:
* drupal-module, drupal-theme, drupal-custom-module, and drupal-custom-theme.
*
* @return \Composer\Package\PackageInterface[]
* All Drupal extension packages in the lock file, keyed by name.
*/
public function getDrupalExtensionPackages(): array {
$filter = function (PackageInterface $package): bool {
$drupal_package_types = [
'drupal-module',
'drupal-theme',
'drupal-custom-module',
'drupal-custom-theme',
];
return in_array($package->getType(), $drupal_package_types, TRUE);
};
return array_filter($this->getInstalledPackages(), $filter);
}
/**
/**
* Returns information on all installed packages.
* Returns information on all installed packages.
*
*
* @return \Composer\Package\PackageInterface[]
* @return \Composer\Package\PackageInterface[]
* All installed packages, keyed by name.
* All installed packages, keyed by name.
*/
*/
protected function getInstalledPackages(): array {
public function getInstalledPackages(): array {
$installed_packages = $this->getComposer()
$installed_packages = $this->getComposer()
->getRepositoryManager()
->getRepositoryManager()
->getLocalRepository()
->getLocalRepository()
@@ -175,4 +154,57 @@ class ComposerUtility {
@@ -175,4 +154,57 @@ class ComposerUtility {
return $packages;
return $packages;
}
}
 
/**
 
* Returns the packages added in another directory.
 
*
 
* @param self $that
 
* A Composer utility wrapper around a different directory.
 
*
 
* @return \Composer\Package\PackageInterface[]
 
* The packages that were added in the other directory, keyed by name.
 
*/
 
public function getPackagesAddedBy(self $that): array {
 
// Return the packages that are there, but not here.
 
return array_diff_key($that->getInstalledPackages(), $this->getInstalledPackages());
 
}
 
 
/**
 
* Returns the packages that were removed in another directory.
 
*
 
* @param self $that
 
* A Composer utility wrapper around a different directory.
 
*
 
* @return \Composer\Package\PackageInterface[]
 
* The packages that were removed in the other directory, keyed by name.
 
*/
 
public function getPackagesRemovedBy(self $that): array {
 
// Return the packages that are here, but not there.
 
return array_diff_key($this->getInstalledPackages(), $that->getInstalledPackages());
 
}
 
 
/**
 
* Returns the packages that were updated in another directory.
 
*
 
* A package will be considered "updated" if its version in the other
 
* directory has changed *at all*.
 
*
 
* @param self $that
 
* A Composer utility wrapper around a different directory.
 
*
 
* @return \Composer\Package\PackageInterface[]
 
* The packages in the current directory that were updated in the other
 
* directory, keyed by name.
 
*/
 
public function getPackagesUpdatedBy(self $that): array {
 
$theirs = $that->getInstalledPackages();
 
 
// Only compare packages that are both here and there.
 
$packages = array_intersect_key($this->getInstalledPackages(), $theirs);
 
 
$filter = function (PackageInterface $package, string $name) use ($theirs): bool {
 
return Comparator::notEqualTo($package->getVersion(), $theirs[$name]->getVersion());
 
};
 
return array_filter($packages, $filter, ARRAY_FILTER_USE_BOTH);
 
}
 
}
}
Loading