Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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