Skip to content

Commit d510a6d

Browse files
committed
Add ProgressBar component, use proper session driver and connection
1 parent d8f7f50 commit d510a6d

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Native\Laravel\Concerns;
4+
5+
use Closure;
6+
use Native\Laravel\ProgressBar;
7+
8+
trait InteractsWithNativeApp
9+
{
10+
public function withProgressBar($totalSteps, Closure $callback)
11+
{
12+
$bar = ProgressBar::create(
13+
is_iterable($totalSteps) ? count($totalSteps) : $totalSteps
14+
);
15+
16+
$bar->start();
17+
18+
if (is_iterable($totalSteps)) {
19+
foreach ($totalSteps as $value) {
20+
$callback($value, $bar);
21+
22+
$bar->advance();
23+
}
24+
} else {
25+
$callback($bar);
26+
}
27+
28+
$bar->finish();
29+
30+
if (is_iterable($totalSteps)) {
31+
return $totalSteps;
32+
}
33+
}
34+
}

src/NativeServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ protected function configureApp()
6767
]]);
6868

6969
config(['database.default' => 'nativephp']);
70-
config(['session.connection' => 'file']);
70+
config(['session.driver' => 'database']);
71+
config(['session.connection' => 'nativephp']);
7172

7273
config(['queue.default' => 'database']);
7374
}

src/ProgressBar.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Native\Laravel;
4+
5+
use Native\Laravel\Client\Client;
6+
7+
class ProgressBar
8+
{
9+
protected float $percent = 0;
10+
protected int $step = 0;
11+
protected float $lastWriteTime = 0;
12+
protected float $minSecondsBetweenRedraws = 0.1;
13+
14+
public function __construct(protected int $maxSteps, protected Client $client)
15+
{
16+
}
17+
18+
public static function create(int $maxSteps): static
19+
{
20+
return new static($maxSteps, new Client());
21+
}
22+
23+
public function start()
24+
{
25+
$this->lastWriteTime = microtime(true);
26+
$this->setProgress(0);
27+
}
28+
29+
public function advance($step = 1)
30+
{
31+
$this->setProgress($this->step + $step);
32+
}
33+
34+
public function setProgress(int $step)
35+
{
36+
if ($this->maxSteps && $step > $this->maxSteps) {
37+
$this->maxSteps = $step;
38+
} elseif ($step < 0) {
39+
$step = 0;
40+
}
41+
42+
$redrawFreq = 1;
43+
$prevPeriod = (int) ($this->step / $redrawFreq);
44+
$currPeriod = (int) ($step / $redrawFreq);
45+
46+
$this->step = $step;
47+
$this->percent = $this->maxSteps ? (float) $this->step / $this->maxSteps : 0;
48+
$timeInterval = microtime(true) - $this->lastWriteTime;
49+
50+
// Draw regardless of other limits
51+
if ($this->maxSteps === $step) {
52+
$this->display();
53+
54+
return;
55+
}
56+
57+
// Throttling
58+
if ($timeInterval < $this->minSecondsBetweenRedraws) {
59+
return;
60+
}
61+
62+
// Draw each step period, but not too late
63+
if ($prevPeriod !== $currPeriod || $timeInterval >= $this->maxSecondsBetweenRedraws) {
64+
$this->display();
65+
}
66+
}
67+
68+
public function finish()
69+
{
70+
$this->client->post('progress-bar/update', [
71+
'percent' => -1,
72+
]);
73+
}
74+
75+
public function display()
76+
{
77+
$this->lastWriteTime = microtime(true);
78+
79+
$this->client->post('progress-bar/update', [
80+
'percent' => $this->percent,
81+
]);
82+
}
83+
84+
}

0 commit comments

Comments
 (0)