diff --git a/src/Commands/TraceCommand.php b/src/Commands/TraceCommand.php index 57e0f57c..ff656348 100644 --- a/src/Commands/TraceCommand.php +++ b/src/Commands/TraceCommand.php @@ -15,7 +15,9 @@ class TraceCommand extends Command * * @var string */ - protected $signature = 'blueprint:trace'; + protected $signature = 'blueprint:trace + {--path=* : List of paths to search in } + '; /** * The console command description. @@ -50,7 +52,8 @@ public function __construct(Filesystem $filesystem, Tracer $tracer) public function handle() { $blueprint = resolve(Blueprint::class); - $definitions = $this->tracer->execute($blueprint, $this->filesystem); + $path = $this->option('path'); + $definitions = $this->tracer->execute($blueprint, $this->filesystem, $path); if (empty($definitions)) { $this->error('No models found'); diff --git a/src/Tracer.php b/src/Tracer.php index a3ba9fff..f4fd15a1 100644 --- a/src/Tracer.php +++ b/src/Tracer.php @@ -11,12 +11,20 @@ class Tracer /** @var Filesystem */ private $filesystem; - public function execute(Blueprint $blueprint, Filesystem $filesystem): array + public function execute(Blueprint $blueprint, Filesystem $filesystem, array $paths = null): array { $this->filesystem = $filesystem; + if (empty($paths)) { + $paths = [Blueprint::appPath()]; + + if (config('blueprint.models_namespace')) { + $paths[0] .= '/'.str_replace('\\', '/', config('blueprint.models_namespace')); + } + } + $definitions = []; - foreach ($this->appClasses() as $class) { + foreach ($this->appClasses($paths) as $class) { $model = $this->loadModel($class); if (is_null($model)) { continue; @@ -41,25 +49,24 @@ public function execute(Blueprint $blueprint, Filesystem $filesystem): array return $definitions; } - private function appClasses() + private function appClasses($paths) { - $dir = Blueprint::appPath(); - - if (config('blueprint.models_namespace')) { - $dir .= '/' . str_replace('\\', '/', config('blueprint.models_namespace')); - } + $classes = []; + foreach ($paths as $path) { + if (!$this->filesystem->exists($path)) { + continue; + } - if (!$this->filesystem->exists($dir)) { - return []; + $classes = array_merge($classes, $this->filesystem->allFiles($path)); } return array_map(function (\SplFIleInfo $file) { - return str_replace( - [Blueprint::appPath() . '/', '/'], - [config('blueprint.namespace') . '\\', '\\'], - $file->getPath() . '/' . $file->getBasename('.php') - ); - }, $this->filesystem->allFiles($dir)); + $content = $this->filesystem->get($file->getPathName()); + preg_match("/namespace ([\w\\\\]+)/", $content, $namespace); + preg_match("/class (\w+)/", $content, $class); + + return ($namespace[1] ?? '').'\\'.($class[1] ?? ''); + }, $classes); } private function loadModel(string $class) diff --git a/tests/Feature/Commands/EraseCommandTest.php b/tests/Feature/Commands/EraseCommandTest.php index ed3c69c9..b7c4d53c 100644 --- a/tests/Feature/Commands/EraseCommandTest.php +++ b/tests/Feature/Commands/EraseCommandTest.php @@ -83,6 +83,6 @@ public function it_calls_the_trace_command() ->assertExitCode(0); $tracer->shouldHaveReceived('execute') - ->with(resolve(Blueprint::class), $this->filesystem); + ->with(resolve(Blueprint::class), $this->filesystem, []); } } diff --git a/tests/Feature/Commands/TraceCommandTest.php b/tests/Feature/Commands/TraceCommandTest.php index 4a281319..ae09e526 100644 --- a/tests/Feature/Commands/TraceCommandTest.php +++ b/tests/Feature/Commands/TraceCommandTest.php @@ -23,7 +23,7 @@ public function it_shows_error_if_no_model_found() $tracer = $this->mock(Tracer::class); $tracer->shouldReceive('execute') - ->with(resolve(Blueprint::class), $this->files) + ->with(resolve(Blueprint::class), $this->files, []) ->andReturn([]); $this->artisan('blueprint:trace') @@ -37,7 +37,7 @@ public function it_shows_the_number_of_traced_models() $tracer = $this->mock(Tracer::class); $tracer->shouldReceive('execute') - ->with(resolve(Blueprint::class), $this->files) + ->with(resolve(Blueprint::class), $this->files, []) ->andReturn([ "Model" => [], "OtherModel" => [], @@ -47,4 +47,21 @@ public function it_shows_the_number_of_traced_models() ->assertExitCode(0) ->expectsOutput('Traced 2 models'); } + + /** @test */ + public function it_passes_the_command_path_to_tracer() + { + $this->filesystem->shouldReceive('exists') + ->with('test.yml') + ->andReturnTrue(); + + $builder = $this->mock(Builder::class); + + $builder->shouldReceive('execute') + ->with(resolve(Blueprint::class), $this->files, 'vendor/package/src/app/Models') + ->andReturn([]); + + $this->artisan('blueprint:trace --path=vendor/package/src/app/Models') + ->assertExitCode(0); + } }