Skip to content

Commit

Permalink
Sort imports alphabetically
Browse files Browse the repository at this point in the history
  • Loading branch information
netpok committed Sep 11, 2019
1 parent b4b3d8f commit 0c538a5
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Illuminate/Console/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function handle()
// stub files so that it gets the correctly formatted namespace and class name.
$this->makeDirectory($path);

$this->files->put($path, $this->buildClass($name));
$this->files->put($path, $this->sortImports($this->buildClass($name)));

$this->info($this->type.' created successfully.');
}
Expand Down Expand Up @@ -205,6 +205,24 @@ protected function replaceClass($stub, $name)
return str_replace('DummyClass', $class, $stub);
}

/**
* Alphabetically sorts the imports for the given stub.
*
* @param string $stub
* @return string
*/
protected function sortImports($stub){
if(preg_match('/(?P<imports>(?:use [^;]+;$\n?)+)/m', $stub, $match)){
$imports = explode("\n", trim($match['imports']));

sort($imports);

return str_replace(trim($match['imports']), implode("\n", $imports), $stub);
}

return $stub;
}

/**
* Get the desired class name from the input.
*
Expand Down

0 comments on commit 0c538a5

Please sign in to comment.