Created
October 5, 2020 15:41
-
-
Save ohnotnow/f60dbf019e5b6cb71ea30cda371450cb to your computer and use it in GitHub Desktop.
Artisan command to convert laravel routes to the version 8 syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
class ReformatRoutes extends Command | |
{ | |
protected $signature = 'route:reformat-l8 {--file=web} {--dry-run}'; | |
protected $description = 'Reformat routes file to laravel 8 format'; | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
public function handle() | |
{ | |
$fileName = base_path() . "/routes/" . $this->option('file') . ".php"; | |
$contents = file_get_contents($fileName); | |
$newContents = collect(explode(PHP_EOL, $contents))->map(function ($line) { | |
if (! str_contains($line, '@')) { | |
return $line; | |
} | |
$controllerSection = []; | |
if (preg_match('/, (\"|\')([A-Za-z0-9\\\\]+@[a-zA-Z]+)(\"|\')/', $line, $controllerSection) === 0) { | |
return $line; | |
}; | |
[$controllerName, $methodName] = explode('@', $controllerSection[2]); | |
$classPrefix = 'App\\Http\\Controllers\\'; | |
if (str_contains($controllerName, $classPrefix)) { | |
$classPrefix = ''; | |
} | |
$newLine = str_replace( | |
$controllerSection[0], | |
", [{$classPrefix}" . $controllerName . "::class, " . "'{$methodName}']", | |
$line | |
); | |
return $newLine; | |
}); | |
if ($this->option('dry-run')) { | |
$this->info($newContents->implode(PHP_EOL)); | |
return; | |
} | |
file_put_contents($fileName, $newContents->implode(PHP_EOL)); | |
$this->info('Done. Remember to set the $namespace in RouteServiceProvider to null.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment