#!/usr/bin/env php addCommands(loadCoreCommands()); $application->addCommands(loadPluginCommands($application)); $application->run(); function loadCoreCommands(): array { $commands = require __DIR__ . '/commands.php'; return array_map(fn($command) => app($command), $commands); } function loadPluginCommands(Application $application): array { $pluginCommands = []; foreach (scanPluginDirectory() as $manifest) { $pluginCommands = array_merge($pluginCommands, getPluginCommands($application, $manifest)); } return $pluginCommands; } function scanPluginDirectory(): \Generator { $basepath = \Config::get()->PLUGINS_PATH; $pluginManager = \PluginManager::getInstance(); $iterator = createPluginManifestIterator($basepath); foreach ($iterator as $manifestFile) { $manifest = $pluginManager->getPluginManifest($manifestFile->getPath()); if (isValidPluginManifest($manifest, $basepath, $manifestFile)) { $manifest['path'] = $manifestFile->getPath(); yield $manifest; } } } function createPluginManifestIterator(string $basepath): \RegexIterator { return new \RegexIterator( new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $basepath, \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::UNIX_PATHS ) ), '/\/plugin\.manifest$/', \RegexIterator::MATCH ); } function isValidPluginManifest(array $manifest, string $basepath, \SplFileInfo $manifestFile): bool { if (!isset($manifest['pluginclassname'], $manifest['cli'])) { return false; } $pluginpath = $basepath . '/' . $manifest['origin'] . '/' . $manifest['pluginclassname']; return $pluginpath === $manifestFile->getPath(); } function getPluginCommands(Application $application, array $manifest): array { $cliFile = $manifest['path'] . '/' . $manifest['cli']; $commandsFn = require_once $cliFile; $commands = []; foreach ($commandsFn($application) as $class => $path) { require_once $path; $commands[] = app($class); } return $commands; }