diff --git a/src/Common/Utils.php b/src/Common/Utils.php index f7c1921c10ec11522473aeabb8893ca9630a788a..5979530d6e1c06f821a7d1e4b29204a43bb252a0 100644 --- a/src/Common/Utils.php +++ b/src/Common/Utils.php @@ -7,7 +7,6 @@ use App\Common\DzqConst; use App\Common\ResponseCode; use Discuz\Base\DzqCache; use Discuz\Base\DzqLog; -use Illuminate\Http\Request; use Illuminate\Support\Str; use Discuz\Http\DiscuzResponseFactory; @@ -177,17 +176,15 @@ class Utils $cacheConfig = DzqCache::get(CacheKey::PLUGIN_LOCAL_CONFIG); if ($cacheConfig) return $cacheConfig; $pluginDir = base_path('plugin'); - $directories = scandir($pluginDir); + $directories = array_diff(scandir($pluginDir), ['.', '..']); $plugins = []; foreach ($directories as $dirName) { - if ($dirName == '.' || $dirName == '..') continue; - $subPlugins = scandir($pluginDir . '/' . $dirName); + $subPlugins = array_diff(scandir($pluginDir . '/' . $dirName), ['.', '..']); $configName = ''; $viewName = ''; $databaseName = ''; $consoleName = ''; foreach ($subPlugins as $item) { - if ($dirName == '.' || $dirName == '..') continue; switch (strtolower($item)) { case 'config.php': $configName = $item; diff --git a/src/Console/Kernel.php b/src/Console/Kernel.php index 33ff4bc077172b68ecf20eb3f258d0a6c7087fd9..b67e9015015673055dc6c05c7653f99cd670178b 100644 --- a/src/Console/Kernel.php +++ b/src/Console/Kernel.php @@ -18,6 +18,7 @@ namespace Discuz\Console; +use Discuz\Base\DzqKernel; use Discuz\Common\Utils; use Discuz\Console\Event\Configuring; use Discuz\Foundation\SiteApp; @@ -62,12 +63,33 @@ class Kernel extends SiteApp implements KernelContract protected function defineConsoleSchedule() { $this->app->singleton(Schedule::class, function ($app) { - return tap(new Schedule($this->scheduleTimezone()), function (Schedule $schedule) { - $this->schedule($schedule->useCache($this->scheduleCache())); + return tap(new Schedule($this->scheduleTimezone()), function (Schedule &$schedule) use ($app) { + $this->schedule($schedule); + $pluginList = Utils::getPluginList(); + foreach ($pluginList as $item) { + $consolePath = $item['plugin_' . $item['app_id']]['console']; + if (empty($consolePath) || !file_exists($consolePath)) continue; + $commands = Finder::create()->in($consolePath)->files(); + foreach ($commands as $command) { + $command = $this->getPluginFileClass($command); + if (is_subclass_of($command, DzqKernel::class)) { + $kernel = new $command($app); + if (method_exists($kernel, 'schedule')) { + $kernel->schedule($schedule); + } + break; + } + } + } }); }); } + private function getPluginFileClass($pluginFinderCommand) + { + return 'Plugin' . str_replace(['/', '.php'], ['\\', ''], Str::after($pluginFinderCommand->getPathname(), base_path('plugin'))); + } + /** * @throws \ReflectionException * @throws \Exception @@ -203,10 +225,10 @@ EOF; $pluginList = Utils::getPluginList(); foreach ($pluginList as $item) { $consolePath = $item['plugin_' . $item['app_id']]['console']; - if (empty($consolePath)) continue; + if (empty($consolePath) || !file_exists($consolePath)) continue; $commands = Finder::create()->in($consolePath)->files(); foreach ($commands as $command) { - $command = "Plugin" . str_replace(['/', '.php'], ['\\', ''], Str::after($command->getPathname(), base_path('plugin'))); + $command = $this->getPluginFileClass($command); $this->doCommand($console, $command); } }