PackageManifest.php
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace Illuminate\Foundation;
use Exception;
use Illuminate\Filesystem\Filesystem;
class PackageManifest
{
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
public $files;
/**
* The base path.
*
* @var string
*/
public $basePath;
/**
* The vendor path.
*
* @var string
*/
public $vendorPath;
/**
* The manifest path.
*
* @var string|null
*/
public $manifestPath;
/**
* The loaded manifest array.
*
* @var array
*/
public $manifest;
/**
* Create a new package manifest instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param string $basePath
* @param string $manifestPath
* @return void
*/
public function __construct(Filesystem $files, $basePath, $manifestPath)
{
$this->files = $files;
$this->basePath = $basePath;
$this->manifestPath = $manifestPath;
$this->vendorPath = $basePath.'/vendor';
}
/**
* Get all of the service provider class names for all packages.
*
* @return array
*/
public function providers()
{
return $this->config('providers');
}
/**
* Get all of the aliases for all packages.
*
* @return array
*/
public function aliases()
{
return $this->config('aliases');
}
/**
* Get all of the values for all packages for the given configuration name.
*
* @param string $key
* @return array
*/
public function config($key)
{
return collect($this->getManifest())->flatMap(function ($configuration) use ($key) {
return (array) ($configuration[$key] ?? []);
})->filter()->all();
}
/**
* Get the current package manifest.
*
* @return array
*/
protected function getManifest()
{
if (! is_null($this->manifest)) {
return $this->manifest;
}
if (! is_file($this->manifestPath)) {
$this->build();
}
return $this->manifest = is_file($this->manifestPath) ?
$this->files->getRequire($this->manifestPath) : [];
}
/**
* Build the manifest and write it to disk.
*
* @return void
*/
public function build()
{
$packages = [];
if ($this->files->exists($path = $this->vendorPath.'/composer/installed.json')) {
$installed = json_decode($this->files->get($path), true);
$packages = $installed['packages'] ?? $installed;
}
$ignoreAll = in_array('*', $ignore = $this->packagesToIgnore());
$this->write(collect($packages)->mapWithKeys(function ($package) {
return [$this->format($package['name']) => $package['extra']['laravel'] ?? []];
})->each(function ($configuration) use (&$ignore) {
$ignore = array_merge($ignore, $configuration['dont-discover'] ?? []);
})->reject(function ($configuration, $package) use ($ignore, $ignoreAll) {
return $ignoreAll || in_array($package, $ignore);
})->filter()->all());
}
/**
* Format the given package name.
*
* @param string $package
* @return string
*/
protected function format($package)
{
return str_replace($this->vendorPath.'/', '', $package);
}
/**
* Get all of the package names that should be ignored.
*
* @return array
*/
protected function packagesToIgnore()
{
if (! is_file($this->basePath.'/composer.json')) {
return [];
}
return json_decode(file_get_contents(
$this->basePath.'/composer.json'
), true)['extra']['laravel']['dont-discover'] ?? [];
}
/**
* Write the given manifest array to disk.
*
* @param array $manifest
* @return void
*
* @throws \Exception
*/
protected function write(array $manifest)
{
if (! is_writable($dirname = dirname($this->manifestPath))) {
throw new Exception("The {$dirname} directory must be present and writable.");
}
$this->files->replace(
$this->manifestPath, '<?php return '.var_export($manifest, true).';'
);
}
}