Skip to content

Commit

Permalink
feat artisan: dynamically load in (and prefix) extension artisan co…
Browse files Browse the repository at this point in the history
…mmands #35
  • Loading branch information
prplwtf committed May 14, 2024
1 parent 9d5600f commit 9bf7e77
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
65 changes: 65 additions & 0 deletions app/Providers/Blueprint/ExtensionCommandServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Pterodactyl\Providers\Blueprint;

use Illuminate\Support\ServiceProvider;
use Illuminate\Console\Application as Artisan;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionClass;

class ExtensionCommandServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*/
public function register()
{
// Register the commands when the application is booted.
$this->app->booted(function () {
$this->loadCommandsFrom(base_path('app/Console/Commands/BlueprintFramework/Extensions'));
});
}

/**
* Load commands from the given directory.
*
* @param string $directory
*/
protected function loadCommandsFrom($directory)
{
$namespace = 'App\\Console\\Commands\\BlueprintFramework\\Extensions';

// Iterate through the directory to find command files
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory),
RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
// Get the relative path
$relativePath = str_replace([$directory . DIRECTORY_SEPARATOR, '.php'], '', $file->getPathname());

// Convert file path to class name
$className = $namespace . '\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath);

// Use Reflection to check if the class exists and is a command
if (class_exists($className)) {
$reflection = new ReflectionClass($className);
if ($reflection->isSubclassOf('Illuminate\Console\Command') && !$reflection->isAbstract()) {
// Extract prefix from the parent folder
$prefix = $file->getPathInfo()->getPathInfo()->getFilename();

// Register the command with the prefix
Artisan::starting(function ($artisan) use ($className, $prefix) {
$command = $artisan->resolve($className);
$command->setName($prefix . ':' . $command->getName());
$artisan->add($command);
});
}
}
}
}
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
* Blueprint Service Providers
*/
Pterodactyl\Providers\Blueprint\RouteServiceProvider::class,
Pterodactyl\Providers\Blueprint\ExtensionCommandServiceProvider::class,

/*
* Additional Dependencies
Expand Down

0 comments on commit 9bf7e77

Please sign in to comment.