|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Application; |
|
6
|
|
|
|
|
7
|
|
|
use League\Flysystem\Filesystem; |
|
8
|
|
|
|
|
9
|
|
|
class Assets |
|
10
|
|
|
{ |
|
11
|
|
|
public const TYPE_DRIVER = 'driver'; |
|
12
|
|
|
|
|
13
|
|
|
private $filesystem; |
|
14
|
|
|
|
|
15
|
3 |
|
public function __construct(Filesystem $filesystem) |
|
16
|
|
|
{ |
|
17
|
3 |
|
$this->filesystem = $filesystem; |
|
18
|
3 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Get all assets. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string|null $type |
|
24
|
|
|
* |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function all(string $type = null): array |
|
28
|
|
|
{ |
|
29
|
2 |
|
$file = resolve('bootstrap_path').'/assets.json'; |
|
30
|
|
|
|
|
31
|
2 |
|
$contents = $this->filesystem->get($file)->read(); |
|
32
|
2 |
|
$assets = json_decode($contents, true); |
|
33
|
|
|
|
|
34
|
2 |
|
if ($type !== null) { |
|
35
|
1 |
|
return collect($assets) |
|
36
|
|
|
->filter(function ($_, $key) use ($type) { |
|
37
|
1 |
|
return $key === $type; |
|
38
|
1 |
|
}) |
|
39
|
1 |
|
->flatten() |
|
40
|
1 |
|
->toArray(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
return $assets; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Discover assets. |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function discover(): void |
|
50
|
|
|
{ |
|
51
|
1 |
|
$file = resolve('bootstrap_path').'/assets.json'; |
|
52
|
|
|
|
|
53
|
|
|
// Flush cached assets |
|
54
|
1 |
|
if ($this->filesystem->has($file)) { |
|
55
|
1 |
|
$this->filesystem->delete($file); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
$files = $this->filesystem->listContents('vendor', true); |
|
59
|
|
|
|
|
60
|
|
|
// Find all composer.json files |
|
61
|
|
|
// Then find suitable assets |
|
62
|
1 |
|
$assets = []; |
|
63
|
|
|
|
|
64
|
1 |
|
collect($files) |
|
65
|
|
|
->filter(function (array $file) { |
|
66
|
1 |
|
return $file['type'] === 'file' && $file['basename'] === 'composer.json'; |
|
67
|
1 |
|
}) |
|
68
|
|
|
->transform(function ($file) { |
|
69
|
1 |
|
$contents = $this->filesystem->get($file['path'])->read(); |
|
70
|
1 |
|
$manifest = json_decode($contents, true); |
|
71
|
|
|
|
|
72
|
1 |
|
if (isset($manifest['extra'], $manifest['extra']['fondbot'])) { |
|
73
|
1 |
|
return $manifest['extra']['fondbot']; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
return null; |
|
77
|
1 |
|
}) |
|
78
|
|
|
->filter(function ($item) { |
|
79
|
1 |
|
return $item !== null; |
|
80
|
1 |
|
}) |
|
81
|
1 |
|
->each(function ($item) use (&$assets) { |
|
82
|
1 |
|
$type = key($item); |
|
83
|
|
|
|
|
84
|
1 |
|
$assets[$type][] = $item[$type]; |
|
85
|
1 |
|
}); |
|
86
|
|
|
|
|
87
|
1 |
|
$this->filesystem->put($file, json_encode($assets)); |
|
88
|
1 |
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|