Merge "StringUtils: Add a utility for checking if a string is a valid regex"
[lhc/web/wiklou.git] / includes / libs / composer / ComposerJson.php
1 <?php
2
3 /**
4 * Reads a composer.json file and provides accessors to get
5 * its hash and the required dependencies
6 *
7 * @since 1.25
8 */
9 class ComposerJson {
10 /**
11 * @var array[]
12 */
13 private $contents;
14
15 /**
16 * @param string $location
17 */
18 public function __construct( $location ) {
19 $this->contents = json_decode( file_get_contents( $location ), true );
20 }
21
22 /**
23 * Dependencies as specified by composer.json
24 *
25 * @return string[]
26 */
27 public function getRequiredDependencies() {
28 $deps = [];
29 if ( isset( $this->contents['require'] ) ) {
30 foreach ( $this->contents['require'] as $package => $version ) {
31 if ( $package !== "php" && strpos( $package, 'ext-' ) !== 0 ) {
32 $deps[$package] = self::normalizeVersion( $version );
33 }
34 }
35 }
36
37 return $deps;
38 }
39
40 /**
41 * Strip a leading "v" from the version name
42 *
43 * @param string $version
44 * @return string
45 */
46 public static function normalizeVersion( $version ) {
47 if ( strpos( $version, 'v' ) === 0 ) {
48 // Composer auto-strips the "v" in front of the tag name
49 $version = ltrim( $version, 'v' );
50 }
51
52 return $version;
53 }
54
55 }