123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace ba;
- class Version
- {
-
- public static function compare(string $v1, $v2): bool
- {
- if (!$v2) {
- return false;
- }
-
- if (strtolower($v1[0]) == 'v') {
- $v1 = substr($v1, 1);
- }
- if (strtolower($v2[0]) == 'v') {
- $v2 = substr($v2, 1);
- }
- if ($v1 == "*" || $v1 == $v2) {
- return true;
- }
-
- if (strpos($v1, '-') !== false) $v1 = explode('-', $v1)[0];
- if (strpos($v2, '-') !== false) $v2 = explode('-', $v2)[0];
- $v1 = explode('.', $v1);
- $v2 = explode('.', $v2);
-
- for ($i = 0; $i < count($v1); $i++) {
- if (!isset($v2[$i])) {
- break;
- }
- if ($v1[$i] == $v2[$i]) {
- continue;
- }
- if ($v1[$i] > $v2[$i]) {
- return false;
- }
- if ($v1[$i] < $v2[$i]) {
- return true;
- }
- }
- if (count($v1) != count($v2)) {
- return !(count($v1) > count($v2));
- }
- return false;
- }
-
- public static function checkDigitalVersion($version): bool
- {
- if (!$version) {
- return false;
- }
- if (strtolower($version[0]) == 'v') {
- $version = substr($version, 1);
- }
- $rule1 = '/\.{2,10}/';
- $rule2 = '/^\d+(\.\d+){0,10}$/';
- if (!preg_match($rule1, (string)$version)) {
- return !!preg_match($rule2, (string)$version);
- }
- return false;
- }
-
- public static function getCnpmVersion(): string
- {
- $execOut = Terminal::getOutputFromProc('version.cnpm');
- if ($execOut) {
- $preg = '/cnpm@(.+?) \(/is';
- preg_match($preg, $execOut, $result);
- return $result[1] ?? '';
- } else {
- return '';
- }
- }
-
- public static function getVersion(string $name): string
- {
- if ($name == 'cnpm') {
- return self::getCnpmVersion();
- } elseif (in_array($name, ['npm', 'yarn', 'pnpm', 'node'])) {
- $execOut = Terminal::getOutputFromProc('version.' . $name);
- if ($execOut) {
- if (strripos($execOut, 'npm WARN') !== false) {
- $preg = '/\d+(\.\d+){0,2}/';
- preg_match($preg, $execOut, $matches);
- if (isset($matches[0]) && self::checkDigitalVersion($matches[0])) {
- return $matches[0];
- }
- }
- $execOut = preg_split('/\r\n|\r|\n/', $execOut);
-
- for ($i = 0; $i < 2; $i++) {
- if (isset($execOut[$i]) && self::checkDigitalVersion($execOut[$i])) {
- return $execOut[$i];
- }
- }
- } else {
- return '';
- }
- }
- return '';
- }
- }
|