diff options
Diffstat (limited to 'includes/libs/composer/ComposerJson.php')
-rw-r--r-- | includes/libs/composer/ComposerJson.php | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/includes/libs/composer/ComposerJson.php b/includes/libs/composer/ComposerJson.php new file mode 100644 index 00000000..796acb56 --- /dev/null +++ b/includes/libs/composer/ComposerJson.php @@ -0,0 +1,54 @@ +<?php + +/** + * Reads a composer.json file and provides accessors to get + * its hash and the required dependencies + * + * @since 1.25 + */ +class ComposerJson { + + /** + * @param string $location + */ + public function __construct( $location ) { + $this->hash = md5_file( $location ); + $this->contents = json_decode( file_get_contents( $location ), true ); + } + + public function getHash() { + return $this->hash; + } + + /** + * Dependencies as specified by composer.json + * + * @return array + */ + public function getRequiredDependencies() { + $deps = array(); + foreach ( $this->contents['require'] as $package => $version ) { + if ( $package !== "php" && strpos( $package, 'ext-' ) !== 0 ) { + $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 ) { + if ( strpos( $version, 'v' ) === 0 ) { + // Composer auto-strips the "v" in front of the tag name + $version = ltrim( $version, 'v' ); + } + + return $version; + } + +} |