checkComposerLockUpToDate.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. require_once __DIR__ . '/Maintenance.php';
  3. /**
  4. * Checks whether your composer-installed dependencies are up to date
  5. *
  6. * Composer creates a "composer.lock" file which specifies which versions are installed
  7. * (via `composer install`). It has a hash, which can be compared to the value of
  8. * the composer.json file to see if dependencies are up to date.
  9. */
  10. class CheckComposerLockUpToDate extends Maintenance {
  11. public function __construct() {
  12. parent::__construct();
  13. $this->addDescription(
  14. 'Checks whether your composer.lock file is up to date with the current composer.json' );
  15. }
  16. public function execute() {
  17. global $IP;
  18. $lockLocation = "$IP/composer.lock";
  19. $jsonLocation = "$IP/composer.json";
  20. if ( !file_exists( $lockLocation ) ) {
  21. // Maybe they're using mediawiki/vendor?
  22. $lockLocation = "$IP/vendor/composer.lock";
  23. if ( !file_exists( $lockLocation ) ) {
  24. $this->error(
  25. 'Could not find composer.lock file. Have you run "composer install --no-dev"?',
  26. 1
  27. );
  28. }
  29. }
  30. $lock = new ComposerLock( $lockLocation );
  31. $json = new ComposerJson( $jsonLocation );
  32. // Check all the dependencies to see if any are old
  33. $found = false;
  34. $installed = $lock->getInstalledDependencies();
  35. foreach ( $json->getRequiredDependencies() as $name => $version ) {
  36. if ( isset( $installed[$name] ) ) {
  37. if ( $installed[$name]['version'] !== $version ) {
  38. $this->output(
  39. "$name: {$installed[$name]['version']} installed, $version required.\n"
  40. );
  41. $found = true;
  42. }
  43. } else {
  44. $this->output( "$name: not installed, $version required.\n" );
  45. $found = true;
  46. }
  47. }
  48. if ( $found ) {
  49. $this->error(
  50. 'Error: your composer.lock file is not up to date. ' .
  51. 'Run "composer update --no-dev" to install newer dependencies',
  52. 1
  53. );
  54. } else {
  55. // We couldn't find any out-of-date dependencies, so assume everything is ok!
  56. $this->output( "Your composer.lock file is up to date with current dependencies!\n" );
  57. }
  58. }
  59. }
  60. $maintClass = 'CheckComposerLockUpToDate';
  61. require_once RUN_MAINTENANCE_IF_MAIN;