Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckComposerLockUpToDate
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3require_once __DIR__ . '/Maintenance.php';
4
5use MediaWiki\Composer\LockFileChecker;
6use Wikimedia\Composer\ComposerJson;
7use Wikimedia\Composer\ComposerLock;
8
9/**
10 * Checks whether your composer-installed dependencies are up to date
11 *
12 * Composer creates a "composer.lock" file which specifies which versions are installed
13 * (via `composer install`). It has a hash, which can be compared to the value of
14 * the composer.json file to see if dependencies are up to date.
15 */
16class CheckComposerLockUpToDate extends Maintenance {
17    public function __construct() {
18        parent::__construct();
19        $this->addDescription(
20            'Checks whether your composer.lock file is up to date with the current composer.json' );
21    }
22
23    public function execute() {
24        global $IP;
25        $lockLocation = "$IP/composer.lock";
26        $jsonLocation = "$IP/composer.json";
27        if ( !file_exists( $lockLocation ) ) {
28            // Maybe they're using mediawiki/vendor?
29            $lockLocation = "$IP/vendor/composer.lock";
30            if ( !file_exists( $lockLocation ) ) {
31                $this->fatalError(
32                    'Could not find composer.lock file. Have you run "composer install --no-dev"?'
33                );
34            }
35        }
36
37        $lock = new ComposerLock( $lockLocation );
38        $json = new ComposerJson( $jsonLocation );
39
40        // Check all the dependencies to see if any are old
41        $checker = new LockFileChecker( $json, $lock );
42        $result = $checker->check();
43        if ( $result->isGood() ) {
44            // We couldn't find any out-of-date dependencies, so assume everything is ok!
45            $this->output( "Your composer.lock file is up to date with current dependencies!\n" );
46        } else {
47            // NOTE: wfMessage will fail if MediaWikiServices is not yet initialized.
48            // This can happen when this class is called directly from bootstrap code,
49            // e.g. by TestSetup. We get around this by having testSetup use quiet mode.
50            if ( !$this->isQuiet() ) {
51                foreach ( $result->getErrors() as $error ) {
52                    $this->error(
53                        wfMessage( $error['message'], ...$error['params'] )->inLanguage( 'en' )->plain() . "\n"
54                    );
55                }
56            }
57            $this->fatalError(
58                'Error: your composer.lock file is not up to date. ' .
59                'Run "composer update --no-dev" to install newer dependencies'
60            );
61        }
62    }
63}
64
65$maintClass = CheckComposerLockUpToDate::class;
66require_once RUN_MAINTENANCE_IF_MAIN;