Loheide-eu-mitarbeiterplatt.../vendor/laravel/prompts/src/Spinner.php
Friederich Loheide 9c0f8de791
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s
Phase 0: Fundament für Inhaus
- Laravel 13 mit Filament 5 (Admin), Inertia 3 + Vue 3 + Vuetify 4 (Material 3)
- Anmeldung über Fortify (Login, Passwort vergessen/ändern), keine Selbstregistrierung
- Profil mit Profilbild (WebP in festen Größen), Personenseiten, Header mit Avatar-Menü
- Material-3-Farbschema aus der Primärfarbe, hell/dunkel/System pro Person
- Basis-Branding (Portalname, Logo, Primärfarbe) im Admin unter „Erscheinungsbild“
- Benutzerverwaltung im Admin, Zugang vorläufig über is_admin
- vendor/ (ohne Dev-Pakete) und Frontend-Build mitversioniert, Dev-Pakete in vendor-dev/
- PHPUnit-Tests gegen MySQL/MariaDB, Forgejo-CI-Matrix, Docker-Entwicklungsumgebung
- VERSION, CHANGELOG.md und CONTRIBUTING.md mit Release-Ablauf

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 20:35:30 +00:00

161 lines
3.5 KiB
PHP

<?php
namespace Laravel\Prompts;
use Closure;
use RuntimeException;
class Spinner extends Prompt
{
/**
* How long to wait between rendering each frame.
*/
public int $interval = 100;
/**
* The number of times the spinner has been rendered.
*/
public int $count = 0;
/**
* Whether the spinner can only be rendered once.
*/
public bool $static = false;
/**
* The process ID after forking.
*/
protected int $pid;
/**
* Create a new Spinner instance.
*/
public function __construct(public string $message = '')
{
//
}
/**
* Render the spinner and execute the callback.
*
* @template TReturn of mixed
*
* @param Closure(): TReturn $callback
* @return TReturn
*/
public function spin(Closure $callback): mixed
{
$this->capturePreviousNewLines();
if (! static::output()->isDecorated() || ! (function_exists('pcntl_fork') && function_exists('posix_kill'))) {
return $this->renderStatically($callback);
}
$originalAsync = pcntl_async_signals(true);
$originalSignalHandler = pcntl_signal_get_handler(SIGINT);
pcntl_signal(SIGINT, fn () => exit());
try {
$this->hideCursor();
$this->render();
$this->pid = pcntl_fork();
if ($this->pid === 0) {
while (true) { // @phpstan-ignore-line
$this->render();
$this->count++;
usleep($this->interval * 1000);
}
} else {
$result = $callback();
$this->resetTerminal($originalAsync, $originalSignalHandler);
return $result;
}
} catch (\Throwable $e) {
$this->resetTerminal($originalAsync, $originalSignalHandler);
throw $e;
}
}
/**
* Reset the terminal.
*/
protected function resetTerminal(bool $originalAsync, callable|int $originalSignalHandler = SIG_DFL): void
{
pcntl_async_signals($originalAsync);
pcntl_signal(SIGINT, $originalSignalHandler);
$this->eraseRenderedLines();
}
/**
* Render a static version of the spinner.
*
* @template TReturn of mixed
*
* @param Closure(): TReturn $callback
* @return TReturn
*/
protected function renderStatically(Closure $callback): mixed
{
$this->static = true;
try {
$this->hideCursor();
$this->render();
$result = $callback();
} finally {
$this->eraseRenderedLines();
}
return $result;
}
/**
* Disable prompting for input.
*
* @throws RuntimeException
*/
public function prompt(): never
{
throw new RuntimeException('Spinner cannot be prompted.');
}
/**
* Get the current value of the prompt.
*/
public function value(): bool
{
return true;
}
/**
* Clear the lines rendered by the spinner.
*/
protected function eraseRenderedLines(): void
{
$lines = explode(PHP_EOL, $this->prevFrame);
$this->moveCursor(-999, -count($lines) + 1);
$this->eraseDown();
}
/**
* Clean up after the spinner.
*/
public function __destruct()
{
if (! empty($this->pid)) {
posix_kill($this->pid, SIGHUP);
}
parent::__destruct();
}
}