2026-10-05 18:10:18 -04:00
<? php
declare ( strict_types = 1 );
namespace Pterodactyl\Services\Extensions ;
use Composer\Autoload\ClassLoader ;
use Illuminate\Contracts\Foundation\Application ;
2026-10-07 16:27:19 -04:00
use Illuminate\Database\Migrations\Migrator ;
2026-10-06 14:02:29 -04:00
use Illuminate\Routing\RouteCollection ;
use Illuminate\Routing\Router ;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Collection ;
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException ;
use Pterodactyl\Extensions\ExtensionProvider ;
use Throwable ;
class ExtensionProviderLoader
{
private ? ClassLoader $classLoader = null ;
/** @var array<string, true> */
private array $autoloadedPaths = [];
/** @var array<string, ExtensionProvider> */
private array $providers = [];
2026-10-07 16:27:19 -04:00
/** @var array<string, true> */
private array $migrationPaths = [];
2026-10-05 18:10:18 -04:00
public function __construct (
private readonly Application $app ,
private readonly ExtensionRepository $extensions ,
) {}
/**
* @param Collection<string, ExtensionManifest> $enabled
*/
public function registerProviders ( Collection $enabled ) : void
{
$this -> registerAutoloader ( $enabled );
2026-10-06 14:02:29 -04:00
$router = $this -> app -> make ( Router :: class );
2026-10-05 18:10:18 -04:00
$registered = [];
foreach ( $enabled as $manifest ) {
if ( isset ( $this -> providers [ $manifest -> id ])) {
continue ;
}
$provider = null ;
2026-10-06 14:02:29 -04:00
// Cached routes are compiled and are not copied, so nothing is taken back then.
$routes = $router -> getRoutes ();
$routes = $routes instanceof RouteCollection ? clone $routes : null ;
2026-10-05 18:10:18 -04:00
try {
foreach ( array_keys ( $manifest -> requiredExtensions ) as $dependency ) {
throw_unless ( in_array ( $dependency , $registered , true ) || isset ( $this -> providers [ $dependency ]), InvalidExtensionException :: class , "Required extension \" { $dependency } \" failed to load." );
}
$this -> registerComposerAutoloader ( $manifest );
2026-10-07 16:27:19 -04:00
if ( $manifest -> provider !== null ) {
$provider = $this -> makeProvider ( $manifest );
$provider -> beginRegistration ();
$this -> app -> register ( $provider , force : true );
$provider -> commitRegistration ();
$this -> providers [ $manifest -> id ] = $provider ;
2026-10-05 18:10:18 -04:00
}
2026-10-07 16:27:19 -04:00
$this -> registerMigrations ( $manifest );
2026-10-05 18:10:18 -04:00
$registered [] = $manifest -> id ;
} catch ( Throwable $exception ) {
$provider ?-> discardRegistration ();
2026-10-06 14:02:29 -04:00
// Routes the provider added with the Route facade instead of the staged
// helpers would otherwise stay live; see ExtensionProvider for what remains.
if ( $routes instanceof RouteCollection ) {
$router -> setRoutes ( $routes );
}
2026-10-05 18:10:18 -04:00
$this -> extensions -> recordFailure (
$manifest -> id ,
$exception -> getMessage (),
$exception ,
$this -> registeredProvider ( $provider ) ? 'boot' : 'register' ,
);
}
}
$this -> extensions -> clearErrors ( $registered );
}
/**
* @param Collection<string, ExtensionManifest> $manifests
*/
public function registerAutoloader ( Collection $manifests ) : void
{
foreach ( $manifests as $manifest ) {
foreach ( $manifest -> autoload as $prefix => $src ) {
$path = $manifest -> path ( $src );
$key = $prefix . '|' . $path ;
if ( isset ( $this -> autoloadedPaths [ $key ])) {
continue ;
}
$this -> loader () -> addPsr4 ( $prefix , $path );
$this -> autoloadedPaths [ $key ] = true ;
}
}
}
private function registerComposerAutoloader ( ExtensionManifest $manifest ) : void
{
$path = $manifest -> path ( 'vendor' , 'autoload.php' );
if ( ! is_file ( $path ) || isset ( $this -> autoloadedPaths [ $path ])) {
return ;
}
2026-10-07 16:16:18 -04:00
// Declaring an autoloader class a second time is a fatal error no handler can catch,
// so a package that shares its suffix with one already loaded is refused instead.
$suffix = ExtensionComposerInspector :: autoloaderSuffix ( $manifest -> directory );
throw_if ( $suffix !== null && ExtensionComposerInspector :: autoloaderLoaded ( $suffix ), InvalidExtensionException :: class , sprintf ( 'Extension vendor/autoload.php declares the Composer autoloader ComposerAutoloaderInit%s, which another package already loaded. Set "config.autoloader-suffix" in the extension\'s composer.json and run composer dump-autoload.' , $suffix ));
2026-10-05 18:10:18 -04:00
$loader = require $path ;
throw_unless ( $loader instanceof ClassLoader , InvalidExtensionException :: class , 'Extension vendor/autoload.php must return a Composer ClassLoader.' );
2026-10-06 13:50:14 -04:00
// Composer registers its loader in front of every other one. Behind the panel's, a
// package the extension bundles can never replace the copy the panel loads.
2026-10-05 18:10:18 -04:00
$loader -> unregister ();
$loader -> register ( prepend : false );
$this -> autoloadedPaths [ $path ] = true ;
}
2026-10-07 16:27:19 -04:00
/**
* Enabling an extension runs its database/migrations, so the migrator is told about the
* directory the way Laravel's loadMigrationsFrom() does: migrate:status lists those
* migrations and migrate:rollback can find them again.
*/
private function registerMigrations ( ExtensionManifest $manifest ) : void
{
$path = $manifest -> path ( 'database' , 'migrations' );
if ( isset ( $this -> migrationPaths [ $path ]) || ! is_dir ( $path )) {
return ;
}
$this -> migrationPaths [ $path ] = true ;
$this -> app -> afterResolving ( 'migrator' , function ( Migrator $migrator ) use ( $path ) : void {
$migrator -> path ( $path );
});
if ( $this -> app -> resolved ( 'migrator' )) {
$this -> app -> make ( Migrator :: class ) -> path ( $path );
}
}
2026-10-05 18:10:18 -04:00
private function makeProvider ( ExtensionManifest $manifest ) : ExtensionProvider
{
if ( $manifest -> provider === null || ! class_exists ( $manifest -> provider )) {
throw new InvalidExtensionException ( "Provider class { $manifest -> provider } was not found via the manifest autoload map." );
}
if ( ! is_subclass_of ( $manifest -> provider , ExtensionProvider :: class )) {
throw new InvalidExtensionException ( " { $manifest -> provider } must extend " . ExtensionProvider :: class . '.' );
}
return new ( $manifest -> provider )( $this -> app , $manifest );
}
private function loader () : ClassLoader
{
if ( ! $this -> classLoader instanceof ClassLoader ) {
2026-10-06 13:50:14 -04:00
// Appended behind the panel's own loader, so the manifest's PSR-4 map only
// serves classes the panel and its packages do not provide.
2026-10-05 18:10:18 -04:00
$this -> classLoader = new ClassLoader ;
2026-10-06 13:50:14 -04:00
$this -> classLoader -> register ( prepend : false );
2026-10-05 18:10:18 -04:00
}
return $this -> classLoader ;
}
private function registeredProvider ( ? ExtensionProvider $provider ) : bool
{
return $provider instanceof ExtensionProvider && $this -> app -> getProvider ( $provider :: class ) === $provider ;
}
}