Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions src/Commands/TraceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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');
Expand Down
39 changes: 23 additions & 16 deletions src/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Commands/EraseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, []);
}
}
21 changes: 19 additions & 2 deletions tests/Feature/Commands/TraceCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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" => [],
Expand All @@ -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);
}
}