|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope\Analyzers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
|
|
7
|
|
|
class ComposerJson |
|
8
|
|
|
{ |
|
9
|
|
|
private static $result = []; |
|
10
|
|
|
|
|
11
|
|
|
public static $fakeComposerPath = null; |
|
12
|
|
|
|
|
13
|
|
|
public static function readKey($key, $composerPath = null) |
|
14
|
|
|
{ |
|
15
|
|
|
$path = $composerPath ?: ''; |
|
16
|
|
|
|
|
17
|
|
|
if (isset(self::$result[$path][$key])) { |
|
18
|
|
|
return self::$result[$path][$key]; |
|
19
|
|
|
} |
|
20
|
|
|
$fullPath = self::$fakeComposerPath ?: app()->basePath($path.'composer.json'); |
|
21
|
|
|
$composer = \json_decode(\file_get_contents($fullPath), true); |
|
22
|
|
|
|
|
23
|
|
|
$value = (array) data_get($composer, $key, []); |
|
24
|
|
|
|
|
25
|
|
|
if (\in_array($key, ['autoload.psr-4', 'autoload-dev.psr-4'])) { |
|
26
|
|
|
$value = self::normalizePaths($value, $path); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return self::$result[$path][$key] = $value; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function isInAppSpace($class) |
|
33
|
|
|
{ |
|
34
|
|
|
return Str::startsWith($class, \array_keys(ComposerJson::readAutoload())); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function readAutoload() |
|
38
|
|
|
{ |
|
39
|
|
|
$composers = []; |
|
40
|
|
|
foreach (self::readKey('repositories') as $repo) { |
|
41
|
|
|
if ($repo['type'] == 'path') { |
|
42
|
|
|
// here we exclude local packages outside of the root folder. |
|
43
|
|
|
! Str::contains($repo['url'], '../') && $composers[] = \trim(\trim($repo['url'], '.'), '/').DIRECTORY_SEPARATOR.''; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$res = []; |
|
48
|
|
|
foreach ($composers as $path) { |
|
49
|
|
|
// We avoid autoload-dev for repositories. |
|
50
|
|
|
$res = $res + self::readKey('autoload.psr-4', $path); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// add the root composer.json |
|
54
|
|
|
$root = self::readKey('autoload.psr-4') + self::readKey('autoload-dev.psr-4'); |
|
55
|
|
|
|
|
56
|
|
|
return self::removedIgnored($res + $root); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private static function normalizePaths($value, $path) |
|
60
|
|
|
{ |
|
61
|
|
|
foreach ($value as $namespace => $_path) { |
|
62
|
|
|
if (! Str::endsWith($_path, ['/'])) { |
|
63
|
|
|
$value[$namespace] .= '/'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$value[$namespace] = $path.$value[$namespace]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $value; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private static function removedIgnored($mapping) |
|
73
|
|
|
{ |
|
74
|
|
|
$result = []; |
|
75
|
|
|
$ignored = config('microscope.ignored_namespaces', []); |
|
76
|
|
|
foreach ($mapping as $namespace => $path) { |
|
77
|
|
|
if (! in_array($namespace, $ignored)) { |
|
78
|
|
|
$result[$namespace] = $path; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|