2026-10-05 18:10:18 -04:00
<? php
declare ( strict_types = 1 );
namespace Pterodactyl\Services\Extensions ;
use Closure ;
use Illuminate\Console\Application as Artisan ;
use Illuminate\Console\Scheduling\Schedule ;
2026-10-06 13:57:18 -04:00
use Illuminate\Contracts\Foundation\Application ;
2026-10-05 18:10:18 -04:00
use InvalidArgumentException ;
2026-10-07 16:32:56 -04:00
use ReflectionClass ;
use Symfony\Component\Console\Attribute\AsCommand ;
2026-10-05 18:10:18 -04:00
use Symfony\Component\Console\Command\Command ;
use Throwable ;
/**
* The artisan commands and schedule callbacks extensions have registered this boot,
* keyed by extension id. Populated by ExtensionProvider::registerCommands() and
* registerSchedule() once a provider has booted successfully; consumed when the console
2026-10-06 13:57:18 -04:00
* application is created and when the scheduler is resolved, so nothing reaches Artisan
* for an extension that failed to boot or is not enabled.
2026-10-05 18:10:18 -04:00
*/
final class ExtensionConsoleRegistry
{
/** @var array<string, list<class-string<Command>>> */
private array $commands = [];
/** @var array<string, list<Closure(Schedule): void>> */
private array $schedules = [];
2026-10-06 13:57:18 -04:00
public function __construct (
private readonly Application $app ,
private readonly ExtensionRepository $extensions ,
) {}
2026-10-05 18:10:18 -04:00
/** @param list<string> $classes */
public function registerCommands ( string $identifier , array $classes ) : void
{
$commands = [];
foreach ( $classes as $class ) {
throw_unless ( is_subclass_of ( $class , Command :: class ), InvalidArgumentException :: class , sprintf ( 'Extension "%s" registered "%s", which is not a console command.' , $identifier , $class ));
$commands [] = $class ;
}
$this -> commands [ $identifier ] = array_values ( array_unique ([ ... $this -> commands [ $identifier ] ?? [], ... $commands ]));
}
/** @param Closure(Schedule): void $callback */
public function registerSchedule ( string $identifier , Closure $callback ) : void
{
$this -> schedules [ $identifier ][] = $callback ;
}
2026-10-06 13:57:18 -04:00
/**
* Hand the commands of every available extension to the console application, once the
* panel's own are registered (Pterodactyl\Console\Kernel). A command whose name or an
* alias does not start with `<id>:`, or that would replace a command that already
* exists, is refused and recorded against its extension; its other commands still load.
2026-10-07 16:32:56 -04:00
* As Laravel does for its own, a command that names itself with #[AsCommand] is only
* constructed when it runs.
2026-10-06 13:57:18 -04:00
*/
2026-10-05 18:10:18 -04:00
public function resolveCommands ( Artisan $artisan ) : void
{
2026-10-07 16:32:56 -04:00
$lazy = false ;
2026-10-05 18:10:18 -04:00
foreach ( $this -> commands as $identifier => $commands ) {
if ( ! $this -> extensions -> isAvailable ( $identifier )) {
continue ;
}
2026-10-06 13:57:18 -04:00
foreach ( $commands as $class ) {
try {
2026-10-07 16:32:56 -04:00
$command = null ;
$names = $this -> declaredNames ( $class );
if ( $names === null ) {
$command = $this -> app -> make ( $class );
throw_unless ( $command instanceof Command , InvalidArgumentException :: class , sprintf ( 'Extension "%s" registered "%s", which is not a console command.' , $identifier , $class ));
2026-10-11 19:44:06 -04:00
$names = [ $command -> getName (), ... array_filter ( $command -> getAliases (), is_string ( ... ))];
2026-10-07 16:32:56 -04:00
}
foreach ( $names as $name ) {
2026-10-06 13:57:18 -04:00
throw_unless ( $name !== null && str_starts_with ( $name , $identifier . ':' ), InvalidArgumentException :: class , sprintf ( 'Extension "%s" cannot register the command "%s": its names must start with "%s:".' , $identifier , $name , $identifier ));
throw_if ( $artisan -> has ( $name ), InvalidArgumentException :: class , sprintf ( 'Extension "%s" cannot register the command "%s": a command with that name already exists.' , $identifier , $name ));
}
2026-10-07 16:32:56 -04:00
if ( $command instanceof Command ) {
$artisan -> addCommand ( $command );
} else {
$artisan -> resolve ( $class );
$lazy = true ;
}
2026-10-06 13:57:18 -04:00
} catch ( Throwable $throwable ) {
$this -> extensions -> recordFailure ( $identifier , $throwable -> getMessage (), $throwable , 'command' );
}
2026-10-05 18:10:18 -04:00
}
}
2026-10-07 16:32:56 -04:00
// Laravel builds its lazy command loader from the command map once; it is built again
// so the map includes the extension commands just added to it.
if ( $lazy ) {
$artisan -> setContainerCommandLoader ();
}
2026-10-05 18:10:18 -04:00
}
/**
* Let every available extension define its scheduled tasks. Each task an extension
* defines only runs while that extension is still enabled, and a failed run is
* recorded against the extension. An extension whose callback throws schedules nothing.
*/
public function schedule ( Schedule $schedule ) : void
{
foreach ( $this -> schedules as $identifier => $callbacks ) {
if ( ! $this -> extensions -> isAvailable ( $identifier )) {
continue ;
}
foreach ( $callbacks as $callback ) {
$defined = count ( $schedule -> events ());
$failure = null ;
try {
$callback ( $schedule );
} catch ( Throwable $throwable ) {
$failure = $throwable ;
$this -> extensions -> recordFailure ( $identifier , $throwable -> getMessage (), $throwable , 'schedule' );
}
foreach ( array_slice ( $schedule -> events (), $defined ) as $event ) {
// Events cannot be removed from the schedule again, so a half-defined
// set is kept from ever running instead.
$event -> skip ( $failure instanceof Throwable )
-> when ( fn () : bool => $this -> extensions -> isAvailable ( $identifier ))
-> onFailure ( function () use ( $identifier , $event ) : void {
$this -> extensions -> recordFailure ( $identifier , sprintf ( 'Scheduled task "%s" failed.' , $event -> getSummaryForDisplay ()), phase : 'schedule' );
});
}
}
}
}
/** @return array{commands: array<string, list<class-string<Command>>>, schedules: array<string, list<Closure(Schedule): void>>} */
public function snapshot () : array
{
return [ 'commands' => $this -> commands , 'schedules' => $this -> schedules ];
}
/** @param array{commands: array<string, list<class-string<Command>>>, schedules: array<string, list<Closure(Schedule): void>>} $snapshot */
public function restore ( array $snapshot ) : void
{
$this -> commands = $snapshot [ 'commands' ];
$this -> schedules = $snapshot [ 'schedules' ];
}
2026-10-07 16:32:56 -04:00
/**
* The name and aliases a command declares with #[AsCommand], or null without one. A
* hidden command's declaration starts with an empty name, which is not one of its names.
*
* @param class-string<Command> $class
* @return list<string>|null
*/
private function declaredNames ( string $class ) : ? array
{
$attribute = ( new ReflectionClass ( $class )) -> getAttributes ( AsCommand :: class )[ 0 ] ?? null ;
return $attribute === null ? null : array_values ( array_filter ( explode ( '|' , $attribute -> newInstance () -> name ), fn ( string $name ) : bool => $name !== '' ));
}
2026-10-05 18:10:18 -04:00
}