Files
MeshVault/src/Console/LazyConsoleServices.php
T

40 lines
959 B
PHP

<?php
declare(strict_types=1);
namespace MeshVault\Console;
use Closure;
/**
* Defers engine construction until a command actually needs it, so usage
* errors and help never require a configured database or library.
*/
final class LazyConsoleServices implements ConsoleServices
{
private readonly Closure $factory;
private ?ConsoleServices $delegate = null;
/** @param callable(): ConsoleServices $factory */
public function __construct(callable $factory)
{
$this->factory = Closure::fromCallable($factory);
}
public function import(string $sourcePath, array $extensions): int
{
return $this->delegate()->import($sourcePath, $extensions);
}
public function runWorkerOnce(callable $shouldStop): ?int
{
return $this->delegate()->runWorkerOnce($shouldStop);
}
private function delegate(): ConsoleServices
{
return $this->delegate ??= ($this->factory)();
}
}