|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Enjoys\AssetsCollector; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
|
|
7
|
|
|
class AssetsCollection |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array<mixed> |
|
11
|
|
|
*/ |
|
12
|
|
|
private array $assets = []; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var LoggerInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private LoggerInterface $logger; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
10 |
|
public function __construct(Environment $environment) |
|
20
|
|
|
{ |
|
21
|
10 |
|
$this->logger = $environment->getLogger(); |
|
22
|
10 |
|
} |
|
23
|
|
|
|
|
24
|
10 |
|
public function add(Asset $asset, string $namespace): void |
|
25
|
|
|
{ |
|
26
|
10 |
|
if ($asset->getPath() === false || $asset->getId() === null) { |
|
27
|
2 |
|
$this->logger->notice(sprintf('Path invalid: %s', $asset->getOrigPath())); |
|
28
|
2 |
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
10 |
|
if ($this->has($asset, $namespace)) { |
|
32
|
1 |
|
$this->logger->notice(sprintf('Duplicate path: %s', $asset->getOrigPath())); |
|
33
|
1 |
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
10 |
|
$this->assets[$asset->getType()][$namespace][$asset->getId()] = $asset; |
|
38
|
10 |
|
} |
|
39
|
|
|
|
|
40
|
10 |
|
public function has(Asset $asset, string $namespace): bool |
|
41
|
|
|
{ |
|
42
|
10 |
|
if (isset($this->assets[$asset->getType()][$namespace][$asset->getId()])) { |
|
43
|
1 |
|
return true; |
|
44
|
|
|
} |
|
45
|
10 |
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $type |
|
50
|
|
|
* @param string $namespace |
|
51
|
|
|
* @return array<Asset> |
|
52
|
|
|
*/ |
|
53
|
8 |
|
public function get(string $type, string $namespace): array |
|
54
|
|
|
{ |
|
55
|
8 |
|
if (!isset($this->assets[$type][$namespace])) { |
|
56
|
1 |
|
return []; |
|
57
|
|
|
} |
|
58
|
8 |
|
return $this->assets[$type][$namespace]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
6 |
|
public function push(AssetsCollection $collection) |
|
62
|
|
|
{ |
|
63
|
6 |
|
$this->assets = array_merge_recursive($this->getAssets(), $collection->getAssets()); |
|
64
|
6 |
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function unshift(AssetsCollection $collection) |
|
67
|
|
|
{ |
|
68
|
1 |
|
$this->assets = array_merge_recursive($collection->getAssets(), $this->getAssets()); |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
7 |
|
public function getAssets(): array |
|
72
|
|
|
{ |
|
73
|
7 |
|
return $this->assets; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|