1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AssetManager\Core\Resolver; |
4
|
|
|
|
5
|
|
|
use Assetic\Asset\AssetCollection; |
6
|
|
|
use Assetic\Asset\AssetInterface; |
7
|
|
|
use AssetManager\Core\Exception; |
8
|
|
|
use AssetManager\Core\Service\AssetFilterManager; |
9
|
|
|
use AssetManager\Core\Service\AssetFilterManagerAwareInterface; |
10
|
|
|
use Traversable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This resolver allows the resolving of collections. |
14
|
|
|
* Collections are strictly checked by mime-type, |
15
|
|
|
* and added to an AssetCollection when all checks passed. |
16
|
|
|
*/ |
17
|
|
|
class CollectionResolver implements |
18
|
|
|
ResolverInterface, |
19
|
|
|
AggregateResolverAwareInterface, |
20
|
|
|
AssetFilterManagerAwareInterface |
21
|
|
|
{ |
22
|
|
|
use CollectionTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ResolverInterface |
26
|
|
|
*/ |
27
|
|
|
protected $aggregateResolver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var AssetFilterManager The filterManager service. |
31
|
|
|
*/ |
32
|
|
|
protected $filterManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor |
36
|
|
|
* |
37
|
|
|
* Instantiate and optionally populate collections. |
38
|
|
|
* |
39
|
|
|
* @param array|Traversable $collections |
40
|
|
|
*/ |
41
|
|
|
public function __construct($collections = array()) |
42
|
|
|
{ |
43
|
|
|
$this->setCollections($collections); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set the aggregate resolver. |
48
|
|
|
* |
49
|
|
|
* @param ResolverInterface $aggregateResolver |
50
|
|
|
*/ |
51
|
|
|
public function setAggregateResolver(ResolverInterface $aggregateResolver) |
52
|
|
|
{ |
53
|
|
|
$this->aggregateResolver = $aggregateResolver; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the aggregate resolver. |
58
|
|
|
* |
59
|
|
|
* @return ResolverInterface |
60
|
|
|
*/ |
61
|
|
|
public function getAggregateResolver() |
62
|
|
|
{ |
63
|
|
|
return $this->aggregateResolver; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritDoc} |
68
|
|
|
*/ |
69
|
|
|
public function resolve($name) |
70
|
|
|
{ |
71
|
|
|
$collections = $this->getCollections(); |
72
|
|
|
|
73
|
|
|
if (!isset($collections[$name])) { |
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!is_array($collections[$name])) { |
78
|
|
|
throw new Exception\RuntimeException( |
79
|
|
|
"Collection with name $name is not an an array." |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$collection = new AssetCollection; |
84
|
|
|
$mimeType = null; |
85
|
|
|
$collection->setTargetPath($name); |
86
|
|
|
|
87
|
|
|
foreach ($collections[$name] as $asset) { |
88
|
|
|
if (!is_string($asset)) { |
89
|
|
|
throw new Exception\RuntimeException( |
90
|
|
|
'Asset should be of type string. got ' . gettype($asset) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (null === ($res = $this->getAggregateResolver()->resolve($asset))) { |
95
|
|
|
throw new Exception\RuntimeException("Asset '$asset' could not be found."); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$res instanceof AssetInterface) { |
99
|
|
|
throw new Exception\RuntimeException( |
100
|
|
|
"Asset '$asset' does not implement Assetic\\Asset\\AssetInterface." |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (null !== $mimeType && $res->mimetype !== $mimeType) { |
|
|
|
|
105
|
|
|
throw new Exception\RuntimeException(sprintf( |
106
|
|
|
'Asset "%s" from collection "%s" doesn\'t have the expected mime-type "%s".', |
107
|
|
|
$asset, |
108
|
|
|
$name, |
109
|
|
|
$mimeType |
110
|
|
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$mimeType = $res->mimetype; |
|
|
|
|
114
|
|
|
|
115
|
|
|
$this->getAssetFilterManager()->setFilters($asset, $res); |
116
|
|
|
|
117
|
|
|
$collection->add($res); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$collection->mimetype = $mimeType; |
|
|
|
|
121
|
|
|
|
122
|
|
|
return $collection; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set the AssetFilterManager. |
127
|
|
|
* |
128
|
|
|
* @param AssetFilterManager $filterManager |
129
|
|
|
*/ |
130
|
|
|
public function setAssetFilterManager(AssetFilterManager $filterManager) |
131
|
|
|
{ |
132
|
|
|
$this->filterManager = $filterManager; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the AssetFilterManager |
137
|
|
|
* |
138
|
|
|
* @return AssetFilterManager |
139
|
|
|
*/ |
140
|
|
|
public function getAssetFilterManager() |
141
|
|
|
{ |
142
|
|
|
return $this->filterManager; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritDoc} |
147
|
|
|
*/ |
148
|
|
|
public function collect() |
149
|
|
|
{ |
150
|
|
|
return array_keys($this->collections); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: