Skip to content

Commit 118125e

Browse files
authored
Add path option to Trace external models (#488)
1 parent c729183 commit 118125e

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

src/Commands/TraceCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class TraceCommand extends Command
1515
*
1616
* @var string
1717
*/
18-
protected $signature = 'blueprint:trace';
18+
protected $signature = 'blueprint:trace
19+
{--path=* : List of paths to search in }
20+
';
1921

2022
/**
2123
* The console command description.
@@ -50,7 +52,8 @@ public function __construct(Filesystem $filesystem, Tracer $tracer)
5052
public function handle()
5153
{
5254
$blueprint = resolve(Blueprint::class);
53-
$definitions = $this->tracer->execute($blueprint, $this->filesystem);
55+
$path = $this->option('path');
56+
$definitions = $this->tracer->execute($blueprint, $this->filesystem, $path);
5457

5558
if (empty($definitions)) {
5659
$this->error('No models found');

src/Tracer.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ class Tracer
1111
/** @var Filesystem */
1212
private $filesystem;
1313

14-
public function execute(Blueprint $blueprint, Filesystem $filesystem): array
14+
public function execute(Blueprint $blueprint, Filesystem $filesystem, array $paths = null): array
1515
{
1616
$this->filesystem = $filesystem;
1717

18+
if (empty($paths)) {
19+
$paths = [Blueprint::appPath()];
20+
21+
if (config('blueprint.models_namespace')) {
22+
$paths[0] .= '/'.str_replace('\\', '/', config('blueprint.models_namespace'));
23+
}
24+
}
25+
1826
$definitions = [];
19-
foreach ($this->appClasses() as $class) {
27+
foreach ($this->appClasses($paths) as $class) {
2028
$model = $this->loadModel($class);
2129
if (is_null($model)) {
2230
continue;
@@ -41,25 +49,24 @@ public function execute(Blueprint $blueprint, Filesystem $filesystem): array
4149
return $definitions;
4250
}
4351

44-
private function appClasses()
52+
private function appClasses($paths)
4553
{
46-
$dir = Blueprint::appPath();
47-
48-
if (config('blueprint.models_namespace')) {
49-
$dir .= '/' . str_replace('\\', '/', config('blueprint.models_namespace'));
50-
}
54+
$classes = [];
55+
foreach ($paths as $path) {
56+
if (!$this->filesystem->exists($path)) {
57+
continue;
58+
}
5159

52-
if (!$this->filesystem->exists($dir)) {
53-
return [];
60+
$classes = array_merge($classes, $this->filesystem->allFiles($path));
5461
}
5562

5663
return array_map(function (\SplFIleInfo $file) {
57-
return str_replace(
58-
[Blueprint::appPath() . '/', '/'],
59-
[config('blueprint.namespace') . '\\', '\\'],
60-
$file->getPath() . '/' . $file->getBasename('.php')
61-
);
62-
}, $this->filesystem->allFiles($dir));
64+
$content = $this->filesystem->get($file->getPathName());
65+
preg_match("/namespace ([\w\\\\]+)/", $content, $namespace);
66+
preg_match("/class (\w+)/", $content, $class);
67+
68+
return ($namespace[1] ?? '').'\\'.($class[1] ?? '');
69+
}, $classes);
6370
}
6471

6572
private function loadModel(string $class)

tests/Feature/Commands/EraseCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ public function it_calls_the_trace_command()
8383
->assertExitCode(0);
8484

8585
$tracer->shouldHaveReceived('execute')
86-
->with(resolve(Blueprint::class), $this->filesystem);
86+
->with(resolve(Blueprint::class), $this->filesystem, []);
8787
}
8888
}

tests/Feature/Commands/TraceCommandTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function it_shows_error_if_no_model_found()
2323
$tracer = $this->mock(Tracer::class);
2424

2525
$tracer->shouldReceive('execute')
26-
->with(resolve(Blueprint::class), $this->files)
26+
->with(resolve(Blueprint::class), $this->files, [])
2727
->andReturn([]);
2828

2929
$this->artisan('blueprint:trace')
@@ -37,7 +37,7 @@ public function it_shows_the_number_of_traced_models()
3737
$tracer = $this->mock(Tracer::class);
3838

3939
$tracer->shouldReceive('execute')
40-
->with(resolve(Blueprint::class), $this->files)
40+
->with(resolve(Blueprint::class), $this->files, [])
4141
->andReturn([
4242
"Model" => [],
4343
"OtherModel" => [],
@@ -47,4 +47,21 @@ public function it_shows_the_number_of_traced_models()
4747
->assertExitCode(0)
4848
->expectsOutput('Traced 2 models');
4949
}
50+
51+
/** @test */
52+
public function it_passes_the_command_path_to_tracer()
53+
{
54+
$this->filesystem->shouldReceive('exists')
55+
->with('test.yml')
56+
->andReturnTrue();
57+
58+
$builder = $this->mock(Builder::class);
59+
60+
$builder->shouldReceive('execute')
61+
->with(resolve(Blueprint::class), $this->files, 'vendor/package/src/app/Models')
62+
->andReturn([]);
63+
64+
$this->artisan('blueprint:trace --path=vendor/package/src/app/Models')
65+
->assertExitCode(0);
66+
}
5067
}

0 commit comments

Comments
 (0)