Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/Progressable.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected function getStorageKeyName(): string {
* Retrieve the prefix storage key for the PHP function.
*/
protected function getPrefixStorageKey(): string {
return $this->customPrefixStorageKey ?? config('progressable.prefix', $this->defaultPrefixStorageKey);
return $this->customPrefixStorageKey ?? (function_exists('app') && app()->bound('config') ? config('progressable.prefix', $this->defaultPrefixStorageKey) : $this->defaultPrefixStorageKey);
}

/**
Expand Down Expand Up @@ -554,7 +554,7 @@ protected function saveOverallProgressData(array $progressData): static {
* Get the storage time-to-live in minutes.
*/
public function getTTL(): int {
return $this->customTTL ?? config('progressable.ttl', $this->defaultTTL);
return $this->customTTL ?? (function_exists('app') && app()->bound('config') ? config('progressable.ttl', $this->defaultTTL) : $this->defaultTTL);
}

/**
Expand Down Expand Up @@ -606,7 +606,7 @@ public function setTTL(int $TTL): static {
* Get the precision for progress values.
*/
public function getPrecision(): int {
return $this->customPrecision ?? config('progressable.precision', $this->defaultPrecision);
return $this->customPrecision ?? (function_exists('app') && app()->bound('config') ? config('progressable.precision', $this->defaultPrecision) : $this->defaultPrecision);
}

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

namespace Verseles\Progressable\Tests;

use Illuminate\Container\Container;
use PHPUnit\Framework\TestCase;
use Verseles\Progressable\Progressable;

class WithoutLaravelTest extends TestCase {
public function test_can_use_progressable_without_laravel(): void {
// Ensure no Laravel instance is bound, simulating usage purely outside Laravel.
Container::setInstance(null);

$storage = [];
$saveCallback = function ($key, $data, $ttl) use (&$storage) {
$storage[$key] = $data;
};

$getCallback = function ($key) use (&$storage) {
return $storage[$key] ?? [];
};

$obj = new class {
use Progressable;
};

$obj->setCustomSaveData($saveCallback)
->setCustomGetData($getCallback)
->setOverallUniqueName('my-task-without-laravel')
->resetOverallProgress()
->setLocalProgress(25);

$this->assertEquals(25, $obj->getLocalProgress());
$this->assertEquals(25, $obj->getOverallProgress());
$this->assertEquals('progressable_my-task-without-laravel', array_key_first($storage));
}
}
Loading