-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathFileAsset.php
80 lines (68 loc) · 2.33 KB
/
FileAsset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php namespace October\Rain\Assetic\Asset;
use File;
use October\Rain\Assetic\Filter\FilterInterface;
use October\Rain\Assetic\Util\VarUtils;
use InvalidArgumentException;
use RuntimeException;
/**
* FileAsset represents an asset loaded from a file.
*
* @author Kris Wallsmith <[email protected]>
*/
class FileAsset extends BaseAsset
{
/**
* @var string source path
*/
protected $source;
/**
* __construct.
*
* @param string $source An absolute path
* @param array $filters An array of filters
* @param string $sourceRoot The source asset root directory
* @param string $sourcePath The source asset path
* @param array $vars
*
* @throws InvalidArgumentException If the supplied root doesn't match the source when guessing the path
*/
public function __construct($source, $filters = [], $sourceRoot = null, $sourcePath = null, array $vars = [])
{
if ($sourceRoot === null) {
$sourceRoot = dirname($source);
if ($sourcePath === null) {
$sourcePath = basename($source);
}
}
elseif ($sourcePath === null) {
if (strpos($source, $sourceRoot) !== 0) {
throw new InvalidArgumentException(sprintf('The source "%s" is not in the root directory "%s"', $source, $sourceRoot));
}
$sourcePath = substr($source, strlen($sourceRoot) + 1);
}
$this->source = $source;
parent::__construct($filters, $sourceRoot, $sourcePath, $vars);
}
/**
* load
*/
public function load(?FilterInterface $additionalFilter = null)
{
$source = VarUtils::resolve($this->source, $this->getVars(), $this->getValues());
if (!is_file($source)) {
throw new RuntimeException(sprintf('The source file "%s" does not exist.', File::nicePath($source)));
}
$this->doLoad(file_get_contents($source), $additionalFilter);
}
/**
* getLastModified
*/
public function getLastModified()
{
$source = VarUtils::resolve($this->source, $this->getVars(), $this->getValues());
if (!is_file($source)) {
throw new RuntimeException(sprintf('The source file "%s" does not exist.', File::nicePath($source)));
}
return filemtime($source);
}
}