|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope\SpyClasses; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson; |
|
7
|
|
|
use Imanghafoori\LaravelMicroscope\Analyzers\FunctionCall; |
|
8
|
|
|
use Imanghafoori\LaravelMicroscope\Analyzers\NamespaceCorrector; |
|
9
|
|
|
use Imanghafoori\LaravelMicroscope\LaravelPaths\FilePath; |
|
10
|
|
|
|
|
11
|
|
|
class RoutePaths |
|
12
|
|
|
{ |
|
13
|
|
|
public static function get() |
|
14
|
|
|
{ |
|
15
|
|
|
$routePaths = []; |
|
16
|
|
|
|
|
17
|
|
|
foreach (app('router')->routePaths as $path) { |
|
18
|
|
|
$routePaths[] = FilePath::normalize($path); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
foreach (config('app.providers') as $providerClass) { |
|
22
|
|
|
// we exclude the core or package service providers here. |
|
23
|
|
|
if (! Str::contains($providerClass, array_keys(ComposerJson::readAutoload()))) { |
|
24
|
|
|
continue; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// get tokens by class name |
|
28
|
|
|
$path = NamespaceCorrector::getRelativePathFromNamespace($providerClass); |
|
29
|
|
|
|
|
30
|
|
|
try { |
|
31
|
|
|
$methodCalls = self::readLoadedRouteFiles($path); |
|
32
|
|
|
} catch (\Throwable $e) { |
|
33
|
|
|
$methodCalls = []; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
foreach ($methodCalls as $calls) { |
|
37
|
|
|
$routeFilePath = self::fullPath($calls, $providerClass, $path); |
|
38
|
|
|
file_exists($routeFilePath) && $routePaths[] = $routeFilePath; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $routePaths; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private static function fullPath($calls, $providerClass, $path) |
|
46
|
|
|
{ |
|
47
|
|
|
$fullPath = ''; |
|
48
|
|
|
|
|
49
|
|
|
foreach ($calls[0] as $token) { |
|
50
|
|
|
if ($token[0] == T_DIR) { |
|
51
|
|
|
// remove class name from the end of string. |
|
52
|
|
|
$relativeDirPath = \trim(Str::replaceLast(class_basename($providerClass), '', $path), '\\'); |
|
53
|
|
|
|
|
54
|
|
|
$fullPath .= $relativeDirPath; |
|
55
|
|
|
} elseif ($token[0] == T_CONSTANT_ENCAPSED_STRING) { |
|
56
|
|
|
$firstParam = \trim($token[1], '\'\"'); |
|
57
|
|
|
$fullPath .= $firstParam; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return FilePath::normalize(base_path($fullPath)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private static function readLoadedRouteFiles($path) |
|
65
|
|
|
{ |
|
66
|
|
|
$tokens = token_get_all(file_get_contents(base_path($path).'.php')); |
|
67
|
|
|
|
|
68
|
|
|
$methodCalls = []; |
|
69
|
|
|
foreach ($tokens as $i => $routeFileToken) { |
|
70
|
|
|
if (FunctionCall::isMethodCallOnThis('loadRoutesFrom', $tokens, $i)) { |
|
71
|
|
|
$methodCalls[] = FunctionCall::readParameters($tokens, $i); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $methodCalls; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|