|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Rector; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Route; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use PhpParser\BuilderHelpers; |
|
8
|
|
|
use PhpParser\Node; |
|
9
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
|
10
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
|
11
|
|
|
|
|
12
|
|
|
class RenameRoutes extends LaravelAwareRectorRule |
|
13
|
|
|
{ |
|
14
|
|
|
public static $ROUTES = null; |
|
15
|
|
|
public $baseDir = null; |
|
16
|
|
|
|
|
17
|
|
|
public function getRuleDefinition(): RuleDefinition |
|
18
|
|
|
{ |
|
19
|
|
|
return new RuleDefinition( |
|
20
|
|
|
'Change usages of admin. routes', [ |
|
21
|
|
|
new CodeSample( |
|
22
|
|
|
'route("admin.blogs");', |
|
23
|
|
|
'route("twill.blogs");' |
|
24
|
|
|
), |
|
25
|
|
|
] |
|
26
|
|
|
); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getNodeTypes(): array |
|
30
|
|
|
{ |
|
31
|
|
|
return [Node\Expr\FuncCall::class, Node\Expr\StaticCall::class]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function refactor(Node $node) |
|
35
|
|
|
{ |
|
36
|
|
|
$isRouteCall = false; |
|
37
|
|
|
if ($node instanceof Node\Expr\StaticCall) { |
|
38
|
|
|
$isRouteCall = $node->name->name === 'route'; |
|
39
|
|
|
} elseif ($node instanceof Node\Expr\FuncCall) { |
|
40
|
|
|
if ($node->name->parts ?? false) { |
|
41
|
|
|
$isRouteCall = $node->name->parts[0] === 'route'; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if ($isRouteCall) { |
|
46
|
|
|
$routes = $this->loadRoutes(); |
|
47
|
|
|
|
|
48
|
|
|
if ($node->getArgs()[0] ?? false) { |
|
|
|
|
|
|
49
|
|
|
/** @var \PhpParser\Node\Arg $arg */ |
|
50
|
|
|
$arg = $node->getArgs()[0]; |
|
51
|
|
|
if (array_key_exists($arg->value->value, $routes)) { |
|
52
|
|
|
$node->args[0] = new Node\Arg(BuilderHelpers::normalizeValue($routes[$arg->value->value])); |
|
|
|
|
|
|
53
|
|
|
return $node; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function loadRoutes(): array |
|
62
|
|
|
{ |
|
63
|
|
|
if (self::$ROUTES === null) { |
|
64
|
|
|
// Get all twill routes so we can process them properly. |
|
65
|
|
|
$app = $this->getLaravel(); |
|
|
|
|
|
|
66
|
|
|
$routes = Route::getRoutes(); |
|
67
|
|
|
|
|
68
|
|
|
$twillRouteList = []; |
|
69
|
|
|
foreach ($routes->getRoutes() as $route) { |
|
70
|
|
|
if (Str::startsWith($route->getName(), 'twill.')) { |
|
71
|
|
|
$twillRouteList[Str::replaceFirst('twill.', 'admin.', $route->getName())] = $route->getName(); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
self::$ROUTES = $twillRouteList; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return self::$ROUTES; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|