Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
ComposerJson | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRequiredDependencies | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
normalizeVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Wikimedia\Composer; |
4 | |
5 | /** |
6 | * Reads a composer.json file and provides accessors to get |
7 | * its hash and the required dependencies |
8 | * |
9 | * @since 1.25 |
10 | */ |
11 | class ComposerJson { |
12 | /** |
13 | * @var array[] |
14 | */ |
15 | private $contents; |
16 | |
17 | /** |
18 | * @param string $location |
19 | */ |
20 | public function __construct( $location ) { |
21 | $this->contents = json_decode( file_get_contents( $location ), true ); |
22 | } |
23 | |
24 | /** |
25 | * Dependencies as specified by composer.json |
26 | * |
27 | * @return string[] |
28 | */ |
29 | public function getRequiredDependencies() { |
30 | $deps = []; |
31 | if ( isset( $this->contents['require'] ) ) { |
32 | foreach ( $this->contents['require'] as $package => $version ) { |
33 | // Examples of package dependencies that don't have a / in the name: |
34 | // php, ext-xml, composer-plugin-api |
35 | if ( strpos( $package, '/' ) !== false ) { |
36 | $deps[$package] = self::normalizeVersion( $version ); |
37 | } |
38 | } |
39 | } |
40 | |
41 | return $deps; |
42 | } |
43 | |
44 | /** |
45 | * Strip a leading "v" from the version name |
46 | * |
47 | * @param string $version |
48 | * @return string |
49 | */ |
50 | public static function normalizeVersion( $version ) { |
51 | // Composer auto-strips the "v" in front of the tag name |
52 | return ltrim( $version, 'v' ); |
53 | } |
54 | |
55 | } |