Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Rclone.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Rclone
private static array $flags = []; // Global rclone flags to be applied to all commands.
private static array $envs = []; // Custom environment variables (usually rclone parameters).
private static string $input = ''; // Input string to be passed to rclone commands (e.g., for rcat).
private static array $version_cache = []; // Cache for rclone version.
private object $progress; // Object to store rclone progress information.
private static array $reset = [ // Default values for resetting static properties.
'timeout' => 120,
Expand Down Expand Up @@ -732,16 +733,26 @@ private function directTwinRun(string $command, ?string $left_path = NULL, ?stri
*/
public function version(bool $numeric = FALSE) : string|float
{
$cacheKey = $numeric ? 'numeric' : 'string';
if (isset(self::$version_cache[$cacheKey])) {
return self::$version_cache[$cacheKey];
}

$cmd_output = $this->simpleRun('version'); // Executes 'rclone version'.

// Parses version string like "rclone v1.2.3"
preg_match_all('/rclone\sv(.+)/m', $cmd_output, $version_matches, PREG_SET_ORDER, 0);

$result = $numeric ? 0.0 : '';

if (isset($version_matches[0][1])) {
$version_string = $version_matches[0][1];
return $numeric ? (float) $version_string : $version_string;
$result = $numeric ? (float) $version_string : $version_string;
}
return $numeric ? 0.0 : ''; // Should not happen with a valid rclone installation.

self::$version_cache[$cacheKey] = $result;

return $result;
}

/**
Expand All @@ -761,6 +772,9 @@ public static function getBIN() : string
*/
public static function setBIN(string $BIN) : void
{
if (isset(self::$BIN) && self::$BIN !== $BIN) {
self::$version_cache = [];
}
self::$BIN = $BIN;
}

Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/VersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Verseles\Flyclone\Test\Unit;

use PHPUnit\Framework\TestCase;
use Verseles\Flyclone\Providers\LocalProvider;
use Verseles\Flyclone\Rclone;

class VersionTest extends TestCase
{
public function testVersion()
{
$binPath = realpath(__DIR__ . '/../../bin/rclone');
if ($binPath) {
Rclone::setBIN($binPath);
} else {
try {
$bin = Rclone::guessBIN();
if (!file_exists($bin) || !is_executable($bin)) {
$this->markTestSkipped("Rclone binary guessed at $bin but not found or not executable.");
}
} catch (\Exception $e) {
$this->markTestSkipped('Rclone binary not found: ' . $e->getMessage());
}
}

$local = new LocalProvider('local');
$rclone = new Rclone($local);

try {
$v1 = $rclone->version();
$this->assertIsString($v1);
$this->assertMatchesRegularExpression('/^[\d.]+/', $v1);

$v2 = $rclone->version(true);
$this->assertIsFloat($v2);
$this->assertGreaterThan(0, $v2);
} catch (\Exception $e) {
$this->markTestSkipped('Failed to run rclone version: ' . $e->getMessage());
}
}
}