|
3 | 3 | namespace Backpack\Generators\Console\Commands; |
4 | 4 |
|
5 | 5 | use Illuminate\Console\Command; |
6 | | -use Illuminate\Support\Arr; |
| 6 | +use Illuminate\Database\Eloquent\Model; |
7 | 7 | use Illuminate\Support\Str; |
8 | 8 |
|
9 | 9 | class BuildBackpackCommand extends Command |
@@ -41,36 +41,73 @@ public function handle() |
41 | 41 | return false; |
42 | 42 | } |
43 | 43 |
|
44 | | - foreach ($models as $key => $model) { |
| 44 | + foreach ($models as $model) { |
45 | 45 | $this->call('backpack:crud', ['name' => $model, '--validation' => $this->option('validation')]); |
46 | 46 | $this->line(' <fg=gray>----------</>'); |
47 | 47 | } |
48 | 48 |
|
49 | 49 | $this->deleteLines(); |
50 | 50 | } |
51 | 51 |
|
52 | | - private function getModels($path) |
| 52 | + private function getModels(string $path): array |
53 | 53 | { |
54 | 54 | $out = []; |
55 | 55 | $results = scandir($path); |
56 | 56 |
|
57 | 57 | foreach ($results as $result) { |
58 | | - if ($result === '.' or $result === '..') { |
| 58 | + $filepath = "$path/$result"; |
| 59 | + |
| 60 | + // ignore `.` (dot) prefixed files |
| 61 | + if ($result[0] === '.') { |
59 | 62 | continue; |
60 | 63 | } |
61 | | - $filename = $path.'/'.$result; |
62 | | - |
63 | | - if (is_dir($filename)) { |
64 | | - $out = array_merge($out, $this->getModels($filename)); |
65 | | - } else { |
66 | | - $file_content = file_get_contents($filename); |
67 | | - if (Str::contains($file_content, 'Illuminate\Database\Eloquent\Model') && |
68 | | - Str::contains($file_content, 'extends Model')) { |
69 | | - $out[] = Arr::last(explode('/', substr($filename, 0, -4))); |
70 | | - } |
| 64 | + |
| 65 | + if (is_dir($filepath)) { |
| 66 | + $out = array_merge($out, $this->getModels($filepath)); |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + // Try to load it by path as namespace |
| 71 | + $class = Str::of($filepath) |
| 72 | + ->after(base_path()) |
| 73 | + ->trim('\\/') |
| 74 | + ->replace('/', '\\') |
| 75 | + ->before('.php') |
| 76 | + ->ucfirst() |
| 77 | + ->value(); |
| 78 | + |
| 79 | + $result = $this->validateModelClass($class); |
| 80 | + if ($result) { |
| 81 | + $out[] = $result; |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + // Try to load it from file content |
| 86 | + $fileContent = Str::of(file_get_contents($filepath)); |
| 87 | + $namespace = $fileContent->match('/namespace (.*);/')->value(); |
| 88 | + $classname = $fileContent->match('/class (\w+)/')->value(); |
| 89 | + |
| 90 | + $result = $this->validateModelClass("$namespace\\$classname"); |
| 91 | + if ($result) { |
| 92 | + $out[] = $result; |
| 93 | + continue; |
71 | 94 | } |
72 | 95 | } |
73 | 96 |
|
74 | 97 | return $out; |
75 | 98 | } |
| 99 | + |
| 100 | + private function validateModelClass(string $class): ?string |
| 101 | + { |
| 102 | + try { |
| 103 | + $reflection = new \ReflectionClass($class); |
| 104 | + |
| 105 | + if ($reflection->isSubclassOf(Model::class) && ! $reflection->isAbstract()) { |
| 106 | + return Str::of($class)->afterLast('\\'); |
| 107 | + } |
| 108 | + } catch (\Throwable$e) { |
| 109 | + } |
| 110 | + |
| 111 | + return null; |
| 112 | + } |
76 | 113 | } |
0 commit comments