|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LumoSolutions\Actionable\Actions\Console; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Illuminate\Support\Facades\File; |
| 7 | +use LumoSolutions\Actionable\Actions\Generation\GenerateDocBlocks; |
| 8 | +use LumoSolutions\Actionable\Actions\Generation\UpdateClassDocBlock; |
| 9 | +use LumoSolutions\Actionable\Dtos\Generation\DocBlockGenDto; |
| 10 | +use LumoSolutions\Actionable\Dtos\Generation\DocBlockUpdateDto; |
| 11 | +use LumoSolutions\Actionable\Support\ClassAnalyser; |
| 12 | +use LumoSolutions\Actionable\Support\DocBlockHelper; |
| 13 | +use LumoSolutions\Actionable\Traits\IsRunnable; |
| 14 | + |
| 15 | +class UpdateActionDocBlocks |
| 16 | +{ |
| 17 | + use IsRunnable; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + protected ClassAnalyser $classAnalyser |
| 21 | + ) {} |
| 22 | + |
| 23 | + public function handle(string $namespace = '\\App', bool $dryRun = false): array |
| 24 | + { |
| 25 | + $namespace = ltrim($namespace, '\\'); |
| 26 | + $classes = $this->findClassesInNamespace($namespace); |
| 27 | + $results = []; |
| 28 | + |
| 29 | + foreach ($classes as $className) { |
| 30 | + try { |
| 31 | + $diff = $this->processClass($className, $dryRun); |
| 32 | + |
| 33 | + if (! empty($diff)) { |
| 34 | + $results[$className] = $diff; |
| 35 | + } |
| 36 | + } catch (Exception $e) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return $results; |
| 42 | + } |
| 43 | + |
| 44 | + public function findClassesInNamespace(string $namespace): array |
| 45 | + { |
| 46 | + $classes = []; |
| 47 | + $namespacePath = str_replace('\\', '/', $namespace); |
| 48 | + |
| 49 | + $searchPaths = []; |
| 50 | + if (str_starts_with($namespace, 'App')) { |
| 51 | + $relativePath = str_replace('App', '', $namespacePath); |
| 52 | + $relativePath = ltrim($relativePath, '/'); |
| 53 | + $searchPaths[] = app_path($relativePath); |
| 54 | + } |
| 55 | + |
| 56 | + $searchPaths[] = base_path('src/'.$namespacePath); |
| 57 | + $searchPaths[] = base_path($namespacePath); |
| 58 | + |
| 59 | + foreach ($searchPaths as $path) { |
| 60 | + if (! is_dir($path)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + $files = File::allFiles($path); |
| 65 | + foreach ($files as $file) { |
| 66 | + if ($file->getExtension() !== 'php') { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + $relativePath = $file->getRelativePathname(); |
| 71 | + $relativePath = str_replace(['/', '.php'], ['\\', ''], $relativePath); |
| 72 | + $classes[] = $namespace.$relativePath; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return array_unique($classes); |
| 77 | + } |
| 78 | + |
| 79 | + public function processClass(string $className, bool $dryRun): array|bool |
| 80 | + { |
| 81 | + // Analyze the class |
| 82 | + $data = rescue( |
| 83 | + fn () => $this->classAnalyser->analyse($className), |
| 84 | + fn ($e) => null, |
| 85 | + false |
| 86 | + ); |
| 87 | + |
| 88 | + if ($data == null) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + $actionableTraits = collect($data->traits) |
| 93 | + ->filter(fn ($trait) => $trait->namespace === 'LumoSolutions\\Actionable\\Traits'); |
| 94 | + |
| 95 | + if ($actionableTraits->isEmpty()) { |
| 96 | + return $dryRun ? [] : false; |
| 97 | + } |
| 98 | + |
| 99 | + $currentDocBlocks = ! empty($data->docBlock) |
| 100 | + ? DocBlockHelper::extract($data->docBlock) |
| 101 | + : []; |
| 102 | + |
| 103 | + $newBlocks = GenerateDocBlocks::run( |
| 104 | + new DocBlockGenDto( |
| 105 | + isRunnable: (bool) $actionableTraits->firstWhere('name', 'IsRunnable'), |
| 106 | + isDispatchable: (bool) $actionableTraits->firstWhere('name', 'IsDispatchable'), |
| 107 | + handle: collect($data->methods)->firstWhere('name', 'handle'), |
| 108 | + docBlocks: $currentDocBlocks, |
| 109 | + usings: $data->includes ?? [] |
| 110 | + ) |
| 111 | + ); |
| 112 | + |
| 113 | + return UpdateClassDocBlock::run( |
| 114 | + new DocBlockUpdateDto( |
| 115 | + filePath: $data->filePath, |
| 116 | + className: $data->className, |
| 117 | + currentDocBlocks: $currentDocBlocks, |
| 118 | + newDocBlocks: $newBlocks |
| 119 | + ), |
| 120 | + $dryRun |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
0 commit comments