autoload([ base_path('project/Commands') ]); } /** * @throws \ReflectionException */ private function autoload($paths) { $paths = array_unique(is_array($paths) ? $paths : [$paths]); $paths = array_filter($paths, 'is_dir'); if (empty($paths)) { return; } $autoloadNamespaces = $this->getAutoloadNamespaces(); foreach ((new Finder())->in($paths)->files() as $file) { $path = $file->getRealPath(); foreach ($autoloadNamespaces as $basePath => $namespace) { if (substr($path, 0, strlen($basePath)) !== $basePath) { continue; } $class = $namespace . str_replace(['/', '.php'], ['\\', ''], substr($path, strlen($basePath))); if (is_subclass_of($class, Command::class) && !(new ReflectionClass($class))->isAbstract()) { Artisan::starting(function ($artisan) use ($class) { $artisan->resolve($class); }); } } } } private function getAutoloadNamespaces(): array { $result = [app_path() . DIRECTORY_SEPARATOR => app()->getNamespace()]; $composer = json_decode(file_get_contents(base_path('composer.json')), true); foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) { $path = realpath(base_path($path)) ?: realpath($path); if ($path) { $result[$path . DIRECTORY_SEPARATOR] = $namespace; } } return $result; } }