1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AssetManager\Core\Asset; |
4
|
|
|
|
5
|
|
|
use Assetic\Asset\BaseAsset; |
6
|
|
|
use Assetic\Filter\FilterInterface; |
7
|
|
|
use AssetManager\Core\Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Represents a concatented string asset. |
11
|
|
|
*/ |
12
|
|
|
class AggregateAsset extends BaseAsset |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var int Timestamp of last modified date from asset |
16
|
|
|
*/ |
17
|
|
|
private $lastModified; |
18
|
|
|
|
19
|
|
|
public $mimetype; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor. |
23
|
|
|
* |
24
|
|
|
* @param array $content The array of assets to be merged |
25
|
|
|
* @param array $filters Filters for the asset |
26
|
|
|
* @param string $sourceRoot The source asset root directory |
27
|
|
|
* @param string $sourcePath The source asset path |
28
|
|
|
*/ |
29
|
|
|
public function __construct(array $content = array(), $filters = array(), $sourceRoot = null, $sourcePath = null) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($filters, $sourceRoot, $sourcePath); |
32
|
|
|
$this->processContent($content); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* load asset |
37
|
|
|
* |
38
|
|
|
* @param FilterInterface $additionalFilter |
39
|
|
|
*/ |
40
|
|
|
public function load(FilterInterface $additionalFilter = null) |
41
|
|
|
{ |
42
|
|
|
$this->doLoad($this->getContent(), $additionalFilter); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* set last modified value of asset |
47
|
|
|
* |
48
|
|
|
* this is useful for cache mechanism detection id file has changed |
49
|
|
|
* |
50
|
|
|
* @param int $lastModified |
51
|
|
|
*/ |
52
|
|
|
public function setLastModified($lastModified) |
53
|
|
|
{ |
54
|
|
|
$this->lastModified = $lastModified; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* get last modified value from asset |
59
|
|
|
* |
60
|
|
|
* @return int|null |
61
|
|
|
*/ |
62
|
|
|
public function getLastModified() |
63
|
|
|
{ |
64
|
|
|
return $this->lastModified; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Loop through assets and merge content |
69
|
|
|
* |
70
|
|
|
* @param array $content |
71
|
|
|
* |
72
|
|
|
* @throws Exception\RuntimeException |
73
|
|
|
*/ |
74
|
|
|
private function processContent($content) |
75
|
|
|
{ |
76
|
|
|
$this->mimetype = null; |
77
|
|
|
foreach ($content as $asset) { |
78
|
|
|
if (null === $this->mimetype) { |
79
|
|
|
$this->mimetype = $asset->mimetype; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($asset->mimetype !== $this->mimetype) { |
83
|
|
|
throw new Exception\RuntimeException( |
84
|
|
|
sprintf( |
85
|
|
|
'Asset "%s" doesn\'t have the expected mime-type "%s".', |
86
|
|
|
$asset->getTargetPath(), |
87
|
|
|
$this->mimetype |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->setLastModified( |
93
|
|
|
max( |
94
|
|
|
$asset->getLastModified(), |
95
|
|
|
$this->getLastModified() |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
$this->setContent( |
99
|
|
|
$this->getContent() . $asset->dump() |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|