Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] avoid backpack:build to generate interfaces for abstract models and others #146

Merged
merged 6 commits into from
Dec 20, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 51 additions & 14 deletions src/Console/Commands/BuildBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Backpack\Generators\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class BuildBackpackCommand extends Command
Expand Down Expand Up @@ -41,36 +41,73 @@ public function handle()
return;
}

foreach ($models as $key => $model) {
foreach ($models as $model) {
$this->call('backpack:crud', ['name' => $model, '--validation' => $this->option('validation')]);
$this->line(' <fg=gray>----------</>');
}

$this->deleteLines();
}

private function getModels($path)
private function getModels(string $path): array
{
$out = [];
$results = scandir($path);

foreach ($results as $result) {
if ($result === '.' or $result === '..') {
$filepath = "$path/$result";

// ignore hidden filed
promatik marked this conversation as resolved.
Show resolved Hide resolved
if ($result[0] === '.') {
continue;
}
$filename = $path.'/'.$result;

if (is_dir($filename)) {
$out = array_merge($out, $this->getModels($filename));
} else {
$file_content = file_get_contents($filename);
if (Str::contains($file_content, 'Illuminate\Database\Eloquent\Model') &&
Str::contains($file_content, 'extends Model')) {
$out[] = Arr::last(explode('/', substr($filename, 0, -4)));
}

if (is_dir($filepath)) {
$out = array_merge($out, $this->getModels($filepath));
continue;
}

// Try to load it by path as namespace
$class = Str::of($filepath)
->after(base_path())
->trim('\\/')
->replace('/', '\\')
->before('.php')
->ucfirst()
->value();

$result = $this->validateModelClass($class);
if ($result) {
$out[] = $result;
continue;
}

// Try to load it from file content
$fileContent = Str::of(file_get_contents($filepath));
$namespace = $fileContent->match('/namespace (.*);/')->value();
$classname = $fileContent->match('/class (\w+)/')->value();

$result = $this->validateModelClass("$namespace\\$classname");
if ($result) {
$out[] = $result;
continue;
}
}

return $out;
}

private function validateModelClass(string $class): ?string
{
try {
$reflection = new \ReflectionClass($class);

if ($reflection->isSubclassOf(Model::class) && ! $reflection->isAbstract()) {
return Str::of($class)->afterLast('\\');
}
} catch (\Throwable$e) {
}

return null;
}
}