Composer manipulation
This commit is contained in:
43
common/Define/ArrayOutput.php
Normal file
43
common/Define/ArrayOutput.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace ProVM\Common\Define;
|
||||
|
||||
use Symfony\Component\Console\Output\Output;
|
||||
|
||||
class ArrayOutput extends Output {
|
||||
private $lines;
|
||||
private $delta;
|
||||
|
||||
public function clear() {
|
||||
$this->lines = [];
|
||||
$this->delta = 0;
|
||||
}
|
||||
|
||||
public function fetch() {
|
||||
return $this->lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = false, $formatter = null) {
|
||||
parent::__construct($verbosity, $decorated, $formatter);
|
||||
$this->lines = [];
|
||||
$this->delta = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doWrite($message, $newline) {
|
||||
if (empty($this->lines[$this->delta])) {
|
||||
$this->lines[$this->delta] = [];
|
||||
}
|
||||
if ($message) {
|
||||
$this->lines[$this->delta][] = trim($message);
|
||||
}
|
||||
if ($newline) {
|
||||
$this->delta++;
|
||||
}
|
||||
}
|
||||
}
|
102
common/Service/Composer.php
Normal file
102
common/Service/Composer.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service;
|
||||
|
||||
use Psr\Container\ContainerInterface as Container;
|
||||
use Composer\Console\Application;
|
||||
use Symfony\Component\Console\Input\ArrayInput as Input;
|
||||
use ProVM\Common\Define\ArrayOutput as Output;
|
||||
|
||||
class Composer {
|
||||
const MAYOR = 0;
|
||||
const MINOR = 1;
|
||||
const PATCH = 2;
|
||||
const RELEASE = 3;
|
||||
|
||||
protected $composer_file;
|
||||
public function __construct(string $composer_file) {
|
||||
$this->composer_file = $composer_file;
|
||||
}
|
||||
|
||||
public function search($query): array {
|
||||
putenv('COMPOSER=' . $this->composer_file);
|
||||
$input = new Input([
|
||||
'command' => 'search',
|
||||
'tokens' => [$query]
|
||||
]);
|
||||
$output = new Output();
|
||||
$app = new Application();
|
||||
$app->setAutoExit(false);
|
||||
$app->run($input, $output);
|
||||
return $this->parseOutput($output->fetch());
|
||||
}
|
||||
protected function parseOutput(array $input): array {
|
||||
$output = [];
|
||||
foreach ($input as $line) {
|
||||
if (strpos($line[0], '<') !== false) {
|
||||
continue;
|
||||
}
|
||||
$pos = strpos($line[0], ' ');
|
||||
$name = substr($line[0], 0, $pos);
|
||||
$desc = substr($line[0], $pos + 1);
|
||||
$output []= (object) compact('name', 'desc');
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
public function latest(string $package): string {
|
||||
putenv('COMPOSER=' . $this->composer_file);
|
||||
$input = new Input([
|
||||
'command' => 'show',
|
||||
'--all' => true,
|
||||
'--latest' => true,
|
||||
'package' => $package
|
||||
]);
|
||||
$output = new Output();
|
||||
$app = new Application();
|
||||
$app->setAutoExit(false);
|
||||
$app->run($input, $output);
|
||||
$arr = $output->fetch();
|
||||
$latest = '*';
|
||||
foreach ($arr as $line) {
|
||||
if (strpos($line[0], 'latest') === false) {
|
||||
continue;
|
||||
}
|
||||
list($option, $value) = explode(':', $line[0]);
|
||||
if (trim($option) == 'latest') {
|
||||
$latest = trim($value);
|
||||
}
|
||||
}
|
||||
return $latest;
|
||||
}
|
||||
public function getLatest(array $packages, int $semver = Composer::MINOR): array {
|
||||
$output = [];
|
||||
foreach ($packages as $package) {
|
||||
$latest = $this->semver($this->latest($package), $semver);
|
||||
$output[$package] = $latest;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
protected function semver(string $version, int $semver) {
|
||||
switch ($semver) {
|
||||
case Composer::PATCH:
|
||||
case Composer::RELEASE:
|
||||
return $version;
|
||||
case Composer::MINOR:
|
||||
if (strpos($version, '.') === false) {
|
||||
return $version;
|
||||
}
|
||||
$v = explode('.', $version);
|
||||
if (count($v) < 3) {
|
||||
return '^' . $version;
|
||||
}
|
||||
array_pop($v);
|
||||
$version = '^' . implode('.', $v);
|
||||
return $version;
|
||||
case Composer::MAYOR:
|
||||
if (strpos($version, '.') === false) {
|
||||
return $version;
|
||||
}
|
||||
$v = explode('.', $version);
|
||||
return '^' . $v[0];
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user