1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of the Assetic package, an OpenSky project. |
---|
5 | * |
---|
6 | * (c) 2010-2012 OpenSky Project Inc |
---|
7 | * |
---|
8 | * For the full copyright and license information, please view the LICENSE |
---|
9 | * file that was distributed with this source code. |
---|
10 | */ |
---|
11 | |
---|
12 | namespace Assetic\Asset; |
---|
13 | |
---|
14 | use Assetic\Util\PathUtils; |
---|
15 | use Assetic\Filter\FilterInterface; |
---|
16 | |
---|
17 | /** |
---|
18 | * Represents an asset loaded from a file. |
---|
19 | * |
---|
20 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
21 | */ |
---|
22 | class FileAsset extends BaseAsset |
---|
23 | { |
---|
24 | private $source; |
---|
25 | |
---|
26 | /** |
---|
27 | * Constructor. |
---|
28 | * |
---|
29 | * @param string $source An absolute path |
---|
30 | * @param array $filters An array of filters |
---|
31 | * @param string $sourceRoot The source asset root directory |
---|
32 | * @param string $sourcePath The source asset path |
---|
33 | * @param array $vars |
---|
34 | * |
---|
35 | * @throws \InvalidArgumentException If the supplied root doesn't match the source when guessing the path |
---|
36 | */ |
---|
37 | public function __construct($source, $filters = array(), $sourceRoot = null, $sourcePath = null, array $vars = array()) |
---|
38 | { |
---|
39 | if (null === $sourceRoot) { |
---|
40 | $sourceRoot = dirname($source); |
---|
41 | if (null === $sourcePath) { |
---|
42 | $sourcePath = basename($source); |
---|
43 | } |
---|
44 | } elseif (null === $sourcePath) { |
---|
45 | if (0 !== strpos($source, $sourceRoot)) { |
---|
46 | throw new \InvalidArgumentException(sprintf('The source "%s" is not in the root directory "%s"', $source, $sourceRoot)); |
---|
47 | } |
---|
48 | |
---|
49 | $sourcePath = substr($source, strlen($sourceRoot) + 1); |
---|
50 | } |
---|
51 | |
---|
52 | $this->source = $source; |
---|
53 | |
---|
54 | parent::__construct($filters, $sourceRoot, $sourcePath, $vars); |
---|
55 | } |
---|
56 | |
---|
57 | public function load(FilterInterface $additionalFilter = null) |
---|
58 | { |
---|
59 | $source = PathUtils::resolvePath($this->source, $this->getVars(), $this->getValues()); |
---|
60 | |
---|
61 | if (!is_file($source)) { |
---|
62 | throw new \RuntimeException(sprintf('The source file "%s" does not exist.', $source)); |
---|
63 | } |
---|
64 | |
---|
65 | $this->doLoad(file_get_contents($source), $additionalFilter); |
---|
66 | } |
---|
67 | |
---|
68 | public function getLastModified() |
---|
69 | { |
---|
70 | $source = PathUtils::resolvePath($this->source, $this->getVars(), $this->getValues()); |
---|
71 | |
---|
72 | if (!is_file($source)) { |
---|
73 | throw new \RuntimeException(sprintf('The source file "%s" does not exist.', $source)); |
---|
74 | } |
---|
75 | |
---|
76 | return filemtime($source); |
---|
77 | } |
---|
78 | } |
---|