glastree_on_gitea
This commit is contained in:
+107
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Prompts\Support;
|
||||
|
||||
class Logger
|
||||
{
|
||||
/**
|
||||
* Create a new Logger instance.
|
||||
*
|
||||
* @param resource|null $socket
|
||||
*/
|
||||
public function __construct(protected string $identifier, protected $socket = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* The buffer for streaming text.
|
||||
*/
|
||||
protected string $streamBuffer = '';
|
||||
|
||||
/**
|
||||
* Log a line to the process log.
|
||||
*/
|
||||
public function line(string $message): void
|
||||
{
|
||||
$this->write(rtrim($message));
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a chunk of text, accumulating on the current line(s).
|
||||
*/
|
||||
public function partial(string $chunk): void
|
||||
{
|
||||
$this->streamBuffer .= $chunk;
|
||||
$this->write($this->streamBuffer, 'partial');
|
||||
}
|
||||
|
||||
/**
|
||||
* Commit the accumulated partial text and start fresh.
|
||||
*/
|
||||
public function commitPartial(): void
|
||||
{
|
||||
$this->streamBuffer = '';
|
||||
$this->write('', 'commitpartial');
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a success message to the process log.
|
||||
*/
|
||||
public function success(string $message): void
|
||||
{
|
||||
$this->write($message, 'success');
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a warning message to the process log.
|
||||
*/
|
||||
public function warning(string $message): void
|
||||
{
|
||||
$this->write($message, 'warning');
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an error message to the process log.
|
||||
*/
|
||||
public function error(string $message): void
|
||||
{
|
||||
$this->write($message, 'error');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the label of the process log.
|
||||
*/
|
||||
public function label(string $message): void
|
||||
{
|
||||
$this->write($message, 'label');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the sub-label of the process log. Pass an empty string to clear.
|
||||
*/
|
||||
public function subLabel(string $message): void
|
||||
{
|
||||
$this->write($message, 'sublabel');
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a message to the socket.
|
||||
*/
|
||||
protected function write(string $message, ?string $type = null): void
|
||||
{
|
||||
if ($type !== null) {
|
||||
fwrite($this->socket, $this->prefix($type, $message).PHP_EOL);
|
||||
} else {
|
||||
fwrite($this->socket, $message.PHP_EOL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefix a message with the identifier and type.
|
||||
*/
|
||||
protected function prefix(string $type, string $message): string
|
||||
{
|
||||
return $this->identifier.'_'.$type.':'.rtrim($message, PHP_EOL);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Prompts\Support;
|
||||
|
||||
/**
|
||||
* Result.
|
||||
*
|
||||
* This is a 'sentinel' value. It wraps a return value, which can
|
||||
* allow us to differentiate between a `null` return value and
|
||||
* a `null` return value that's intended to continue a loop.
|
||||
*/
|
||||
final class Result
|
||||
{
|
||||
public function __construct(public readonly mixed $value)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public static function from(mixed $value): self
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Prompts\Support;
|
||||
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class Utils
|
||||
{
|
||||
/**
|
||||
* Determine if all items in an array match a truth test.
|
||||
*
|
||||
* @param array<array-key, mixed> $values
|
||||
*/
|
||||
public static function allMatch(array $values, Closure $callback): bool
|
||||
{
|
||||
foreach ($values as $key => $value) {
|
||||
if (! $callback($value, $key)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last item from an array or null if it doesn't exist.
|
||||
*
|
||||
* @param array<array-key, mixed> $array
|
||||
*/
|
||||
public static function last(array $array): mixed
|
||||
{
|
||||
return array_reverse($array)[0] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the key of the first element in the array that satisfies the callback.
|
||||
*
|
||||
* @param array<array-key, mixed> $array
|
||||
*/
|
||||
public static function search(array $array, Closure $callback): int|string|false
|
||||
{
|
||||
foreach ($array as $key => $value) {
|
||||
if ($callback($value, $key)) {
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user