twigc/src/Twigc/ComposerHelper.php
dana 9cbe7e75f9 Significantly rework application: 0.3.0
- Get rid of Symfony Console application/command/input components

  These were causing me serious problems — not least of all was the fact that
  the Console argument parser chokes on command lines lke `twigc -j - foo.twig`.
  I am not happy with the way i've structured this, but i just needed to get it
  *done*, so here we are. If someone has any advice on to how to make this nicer
  (maybe break it up into different classes, &c.), i would be appreciative

- Add GetOpt.php for argument parsing and usage-help printing

  The usage help is really ugly, tbh. But whatever

- Support multiple input data sources at once

- Add `-E` short option for `--env`

- Display an error when `-E` is used without the appropriate `variables_order`
  configuration in place

- Add shell (`sh`) auto-escape method

- Simplify/eliminate a lot of code (handling of version numbers, validation, &c.)

- Change a lot of white space and formatting stuff (RIP `blame`)
2018-06-02 01:58:59 -05:00

89 lines
2.2 KiB
PHP

<?php
/**
* This file is part of twigc.
*
* @author dana <dana@dana.is>
* @license MIT
*/
namespace Dana\Twigc;
/**
* Helper class with various functions for interacting with Composer's lock
* file.
*/
class ComposerHelper {
/**
* Get an array of the installed non-dev packages listed in a Composer lock
* file.
*
* @param string|null (optional) The path to the lock file to parse.
*
* @return object[]
*/
public function getPackages(string $lockFile = null) {
$packages = $this->parseLockFile($lockFile)['packages'];
return $this->massagePackages($packages);
}
/**
* Get an array of the installed dev packages listed in a Composer lock file.
*
* @param string|null (optional) The path to the lock file to parse.
*
* @return object[]
*/
public function getDevPackages(string $lockFile = null) {
$packages = $this->parseLockFile($lockFile)['packages-dev'];
return $this->massagePackages($packages);
}
/**
* Get an array of data representing a Composer lock file.
*
* @param string $path
* (optional) The path to the lock file to parse. The default is the lock
* file associated with the current project.
*
* @return array
*
* @throws \RuntimeException if composer.lock doesn't exist
* @throws \RuntimeException if composer.lock can't be decoded
*/
public function parseLockFile(string $lockFile = null): array {
$lockFile = $lockFile ?? __DIR__ . '/../../composer.lock';
if ( ! file_exists($lockFile) ) {
throw new \RuntimeException('Missing ' . basename($lockFile));
}
$lock = json_decode(file_get_contents($lockFile), true);
if ( empty($lock) || ! isset($lock['packages']) ) {
throw new \RuntimeException('Error decoding ' . basename($lockFile));
}
return $lock;
}
/**
* Sort and object-ify an array of package data.
*
* @param array $packages Package data from composer.lock.
*
* @return object[]
*/
public function massagePackages(array $packages): array {
usort($packages, function ($a, $b) {
return strcasecmp($a['name'], $b['name']);
});
foreach ( $packages as &$package ) {
$package = (object) $package;
}
return $packages;
}
}