Skip to content

Commit

Permalink
Update Make.php
Browse files Browse the repository at this point in the history
  • Loading branch information
wimurk authored Aug 11, 2023
1 parent 36d0727 commit f8bc617
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions src/Commands/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\CarbonImmutable;
use File;
use Illuminate\Console\Command;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
Expand All @@ -13,7 +14,9 @@
use ReflectionMethod;
use Symfony\Component\Finder\SplFileInfo;
use Throwable;
use function app_path;
use function base_path;
use function collect;
use function config;
use function str;

Expand Down Expand Up @@ -539,7 +542,8 @@ private function extractModels(): void
}

if ($this->option('path') && $this->option('path') === 'auto') {
$paths = $this->extractDeclaredClasses();
$this->extractDeclaredClasses();
return;
}

foreach ($paths as $modelPath) {
Expand All @@ -566,23 +570,48 @@ private function filterModels(): void
}

/**
* @param array $paths
* @return array
* @return void
*/
private function extractDeclaredClasses(array $paths = []): array
private function extractDeclaredClasses(): void
{
foreach (get_declared_classes() as $class) {
$models = $this->getModels();

foreach ($models as $model) {
$this->models[] = [
'basePath' => base_path("$model.php"),
'relativePath' => base_path(str($model)->beforeLast('\\')->value),
'namespace' => str($model)->beforeLast('\\')->value,
'model' => str($model)->afterLast('\\')->value,
];
}

$reflector = new \ReflectionClass($class);
$path = str($reflector->getFileName())->after(base_path())->trim('/\\');
$this->filterModels();
}

if (is_subclass_of($class, Model::class) && !$path->contains('vendor')) {
$parsed = $this->parseRelativePath($path->replace('.php', '') . "/../");
$paths[$parsed] = $parsed;
private function getModels(): Collection2
{
$models = collect(File::allFiles(app_path()))
->map(function ($item) {
$path = $item->getRelativePathName();
$class = sprintf('\%s%s',
Container::getInstance()->getNamespace(),
strtr(substr($path, 0, strrpos($path, '.')), '/', '\\'));

return $class;
})
->filter(function ($class) {
$valid = false;

if (class_exists($class)) {
$reflection = new \ReflectionClass($class);
$valid = $reflection->isSubclassOf(Model::class) &&
!$reflection->isAbstract();
}
}

return $paths;
return $valid;
});

return $models->values();
}

/**
Expand Down

0 comments on commit f8bc617

Please sign in to comment.