feat ExtensionCommandService: Add debug lines and attempt to handle symlinks correctly.

This commit is contained in:
prplwtf 2024-05-15 11:05:56 +02:00
parent 6730116228
commit 1afa732a6c

View file

@ -7,18 +7,27 @@ use Illuminate\Console\Application as Artisan;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
use RecursiveIteratorIterator; use RecursiveIteratorIterator;
use ReflectionClass; use ReflectionClass;
use Log;
class ExtensionCommandServiceProvider extends ServiceProvider class ExtensionCommandServiceProvider extends ServiceProvider
{ {
/** /**
* Register the service provider. * Register any application services.
*/ */
public function register() public function register()
{ {
// Register the commands when the application is booted. // This method is intentionally left blank
$this->app->booted(function () { }
/**
* Bootstrap any application services.
*/
public function boot()
{
// Log the base path for debugging
Log::info('Base path: ' . base_path());
$this->loadCommandsFrom(base_path('app/Console/Commands/BlueprintFramework/Extensions')); $this->loadCommandsFrom(base_path('app/Console/Commands/BlueprintFramework/Extensions'));
});
} }
/** /**
@ -28,18 +37,21 @@ class ExtensionCommandServiceProvider extends ServiceProvider
*/ */
protected function loadCommandsFrom($directory) protected function loadCommandsFrom($directory)
{ {
Log::info('Loading commands from: ' . $directory);
$namespace = 'Pterodactyl\\Console\\Commands\\BlueprintFramework\\Extensions'; $namespace = 'Pterodactyl\\Console\\Commands\\BlueprintFramework\\Extensions';
// Iterate through the directory to find command files // Iterate through the directory to find command files
$iterator = new RecursiveIteratorIterator( $iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory), new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::FOLLOW_SYMLINKS),
RecursiveIteratorIterator::LEAVES_ONLY RecursiveIteratorIterator::LEAVES_ONLY
); );
foreach ($iterator as $file) { foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'php') { if ($file->isFile() && $file->getExtension() === 'php') {
// Get the relative path // Get the real path of the file
$relativePath = str_replace([$directory . DIRECTORY_SEPARATOR, '.php'], '', $file->getPathname()); $realPath = $file->getRealPath();
$relativePath = str_replace([$directory . DIRECTORY_SEPARATOR, '.php'], '', $realPath);
// Convert file path to class name // Convert file path to class name
$className = $namespace . '\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath); $className = $namespace . '\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath);
@ -49,7 +61,7 @@ class ExtensionCommandServiceProvider extends ServiceProvider
$reflection = new ReflectionClass($className); $reflection = new ReflectionClass($className);
if ($reflection->isSubclassOf('Illuminate\Console\Command') && !$reflection->isAbstract()) { if ($reflection->isSubclassOf('Illuminate\Console\Command') && !$reflection->isAbstract()) {
// Extract prefix from the parent folder // Extract prefix from the parent folder
$prefix = $file->getPathInfo()->getPathInfo()->getFilename(); $prefix = basename(dirname($file->getPath()));
// Register the command with the prefix // Register the command with the prefix
Artisan::starting(function ($artisan) use ($className, $prefix) { Artisan::starting(function ($artisan) use ($className, $prefix) {
@ -57,7 +69,13 @@ class ExtensionCommandServiceProvider extends ServiceProvider
$command->setName('ext:' . $prefix . ':' . $command->getName()); $command->setName('ext:' . $prefix . ':' . $command->getName());
$artisan->add($command); $artisan->add($command);
}); });
Log::info("Registered command: {$prefix}:{$className}");
} else {
Log::warning("Class {$className} is not a valid command.");
} }
} else {
Log::warning("Class {$className} does not exist.");
} }
} }
} }