Skip to content
Merged
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
19 changes: 11 additions & 8 deletions packages/console/src/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,30 @@ public function __construct(
/**
* Boots the console application.
*
* @param string $name The name of the console application.
* @param string|null $root The root directory of the application. By default, the current working directory.
* @param \Tempest\Discovery\DiscoveryLocation[] $discoveryLocations The locations to use for class discovery.
* @param string|null $internalStorage The *absolute* internal storage directory for Tempest.
* @param string $name The name of the console application.
* @param bool $loadBuiltInCommands Whether to load built-in Tempest console commands.
*/
public static function boot(
string $name = 'Tempest',
?string $root = null,
array $discoveryLocations = [],
?string $internalStorage = null,
?string $name = null,
?bool $loadBuiltInCommands = true,
): self {
$internalStorage ??= '.' . Str\to_kebab_case($name);
$container = Tempest::boot($root, $discoveryLocations, $internalStorage);
if (! $internalStorage && $name) {
$internalStorage = sprintf('.%s', Str\to_kebab_case($name));
}

$application = $container->get(ConsoleApplication::class);
$container = Tempest::boot($root, $discoveryLocations, $internalStorage);

// Application-specific config
$consoleConfig = $container->get(ConsoleConfig::class);
$consoleConfig->name = $name;
$consoleConfig->name ??= $name;
$consoleConfig->loadBuiltInCommands = $loadBuiltInCommands ?? $consoleConfig->loadBuiltInCommands;

return $application;
return $container->get(ConsoleApplication::class);
}

public function run(): never
Expand Down
37 changes: 29 additions & 8 deletions packages/console/src/ConsoleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,36 @@

final class ConsoleConfig
{
public function __construct(
public string $name = 'Tempest',

/** @var ConsoleCommand[] $commands */
public array $commands = [],
public ?string $logPath = null,
/**
* List of registered console commands.
*
* @var ConsoleCommand[] $commands
*/
public array $commands = [];

/**
* The path to the log file where console output will be recorded.
*/
public ?string $logPath = null;

/**
* Middleware stack for console commands.
*
* @see https://tempestphp.com/current/essentials/console-commands#middleware
*
* @var Middleware<\Tempest\Console\ConsoleMiddleware>
*/
public Middleware $middleware {
get => $this->middleware ??= new Middleware();
}

/** @var Middleware<\Tempest\Console\ConsoleMiddleware> */
public Middleware $middleware = new Middleware(),
/**
* @param ?string $name The name of the application. Will appear in console command menus.
* @param bool $loadBuiltInCommands Whether to load built-in Tempest commands.
*/
public function __construct(
public ?string $name = null,
public bool $loadBuiltInCommands = true,
) {}

public function addCommand(MethodReflector $handler, ConsoleCommand $consoleCommand): self
Expand Down
4 changes: 4 additions & 0 deletions packages/console/src/Discovery/ConsoleCommandDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function discover(DiscoveryLocation $location, ClassReflector $class): vo
continue;
}

if (! $this->consoleConfig->loadBuiltInCommands && $location->isTempest()) {
continue;
}

$this->discoveryItems->add($location, [$method, $consoleCommand]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use Tempest\Container\Initializer;
use Tempest\Container\Singleton;
use Tempest\Core\Kernel;

use function Tempest\Support\path;
use Tempest\Support\Path;

final readonly class LogOutputBufferInitializer implements Initializer
{
Expand All @@ -21,7 +20,7 @@ public function initialize(Container $container): LogOutputBuffer
$consoleConfig = $container->get(ConsoleConfig::class);
$kernel = $container->get(Kernel::class);

$path = $consoleConfig->logPath ?? path($kernel->root, 'console.log')->toString();
$path = $consoleConfig->logPath ?? Path\normalize($kernel->root, 'console.log');

return new LogOutputBuffer($path);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/console/src/Middleware/OverviewMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Tempest\Console\ConsoleMiddlewareCallable;
use Tempest\Console\ExitCode;
use Tempest\Console\Initializers\Invocation;
use Tempest\Core\AppConfig;
use Tempest\Core\DiscoveryCache;
use Tempest\Core\Priority;

Expand All @@ -23,6 +24,7 @@
{
public function __construct(
private Console $console,
private AppConfig $appConfig,
private ConsoleConfig $consoleConfig,
private DiscoveryCache $discoveryCache,
) {}
Expand All @@ -41,7 +43,7 @@ public function __invoke(Invocation $invocation, ConsoleMiddlewareCallable $next
private function renderOverview(bool $showHidden = false): void
{
$this->console->header(
header: $this->consoleConfig->name,
header: $this->consoleConfig->name ?? $this->appConfig->name ?? 'Tempest',
subheader: 'This is an overview of available commands.' . PHP_EOL . 'Type <em><command> --help</em> to get more help about a specific command.',
);

Expand Down
3 changes: 2 additions & 1 deletion packages/console/src/Testing/ConsoleTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Tempest\Console\OutputBuffer;
use Tempest\Container\Container;
use Tempest\Highlight\Highlighter;
use Tempest\Validation\Validator;

final class ConsoleTester
{
Expand Down Expand Up @@ -174,7 +175,7 @@ public function getBuffer(?callable $callback = null): array

public function useInteractiveTerminal(): self
{
$this->componentRenderer = new InteractiveComponentRenderer();
$this->componentRenderer = new InteractiveComponentRenderer($this->container->get(Validator::class));

return $this;
}
Expand Down
20 changes: 8 additions & 12 deletions packages/core/src/Tempest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@

final readonly class Tempest
{
public static function boot(
?string $root = null,
/** @var \Tempest\Discovery\DiscoveryLocation[] $discoveryLocations */
array $discoveryLocations = [],
?string $internalStorage = null,
): Container {
$root ??= getcwd();

// Kernel
return FrameworkKernel::boot(
root: $root,
/** @param \Tempest\Discovery\DiscoveryLocation[] $discoveryLocations */
public static function boot(?string $root = null, array $discoveryLocations = [], ?string $internalStorage = null): Container
{
$kernel = FrameworkKernel::boot(
root: $root ?? getcwd(),
discoveryLocations: $discoveryLocations,
internalStorage: $internalStorage,
)->container;
);

return $kernel->container;
}
}
7 changes: 6 additions & 1 deletion packages/discovery/src/DiscoveryLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ public static function fromNamespace(Psr4Namespace $namespace): self
return new self($namespace->namespace, $namespace->path);
}

public function isTempest(): bool
{
return str_starts_with($this->namespace, 'Tempest');
}

public function isVendor(): bool
{
return str_contains($this->path, '/vendor/') || str_contains($this->path, '\\vendor\\') || str_starts_with($this->namespace, 'Tempest');
return str_contains($this->path, '/vendor/') || str_contains($this->path, '\\vendor\\') || $this->isTempest();
}

public function toClassName(string $path): string
Expand Down