File: ComposerJson.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (55 lines) | stat: -rw-r--r-- 1,165 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

namespace Wikimedia\Composer;

/**
 * Reads a composer.json file and provides accessors to get
 * its hash and the required dependencies
 *
 * @since 1.25
 */
class ComposerJson {
	/**
	 * @var array[]
	 */
	private $contents;

	/**
	 * @param string $location
	 */
	public function __construct( $location ) {
		$this->contents = json_decode( file_get_contents( $location ), true );
	}

	/**
	 * Dependencies as specified by composer.json
	 *
	 * @return string[]
	 */
	public function getRequiredDependencies() {
		$deps = [];
		if ( isset( $this->contents['require'] ) ) {
			foreach ( $this->contents['require'] as $package => $version ) {
				// Examples of package dependencies that don't have a / in the name:
				// php, ext-xml, composer-plugin-api
				if ( strpos( $package, '/' ) !== false ) {
					$deps[$package] = self::normalizeVersion( $version );
				}
			}
		}

		return $deps;
	}

	/**
	 * Strip a leading "v" from the version name
	 *
	 * @param string $version
	 * @return string
	 */
	public static function normalizeVersion( $version ) {
		// Composer auto-strips the "v" in front of the tag name
		return ltrim( $version, 'v' );
	}

}