|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Assetic package, an OpenSky project. |
|
5 * |
|
6 * (c) 2010-2011 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\Filter\FilterInterface; |
|
15 |
|
16 /** |
|
17 * Represents an asset loaded via an HTTP request. |
|
18 * |
|
19 * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
|
20 */ |
|
21 class HttpAsset extends BaseAsset |
|
22 { |
|
23 private $sourceUrl; |
|
24 private $ignoreErrors; |
|
25 |
|
26 /** |
|
27 * Constructor. |
|
28 * |
|
29 * @param string $sourceUrl The source URL |
|
30 * @param array $filters An array of filters |
|
31 * |
|
32 * @throws InvalidArgumentException If the first argument is not an URL |
|
33 */ |
|
34 public function __construct($sourceUrl, $filters = array(), $ignoreErrors = false) |
|
35 { |
|
36 if (0 === strpos($sourceUrl, '//')) { |
|
37 $sourceUrl = 'http:'.$sourceUrl; |
|
38 } elseif (false === strpos($sourceUrl, '://')) { |
|
39 throw new \InvalidArgumentException(sprintf('"%s" is not a valid URL.', $sourceUrl)); |
|
40 } |
|
41 |
|
42 $this->sourceUrl = $sourceUrl; |
|
43 $this->ignoreErrors = $ignoreErrors; |
|
44 |
|
45 list($scheme, $url) = explode('://', $sourceUrl, 2); |
|
46 list($host, $path) = explode('/', $url, 2); |
|
47 |
|
48 parent::__construct($filters, $scheme.'://'.$host, $path); |
|
49 } |
|
50 |
|
51 public function load(FilterInterface $additionalFilter = null) |
|
52 { |
|
53 if (false === $content = @file_get_contents($this->sourceUrl)) { |
|
54 if ($this->ignoreErrors) { |
|
55 return; |
|
56 } else { |
|
57 throw new \RuntimeException(sprintf('Unable to load asset from URL "%s"', $this->sourceUrl)); |
|
58 } |
|
59 } |
|
60 |
|
61 $this->doLoad($content, $additionalFilter); |
|
62 } |
|
63 |
|
64 public function getLastModified() |
|
65 { |
|
66 if (false !== @file_get_contents($this->sourceUrl, false, stream_context_create(array('http' => array('method' => 'HEAD'))))) { |
|
67 foreach ($http_response_header as $header) { |
|
68 if (0 === stripos($header, 'Last-Modified: ')) { |
|
69 list(, $mtime) = explode(':', $header, 2); |
|
70 |
|
71 return strtotime(trim($mtime)); |
|
72 } |
|
73 } |
|
74 } |
|
75 } |
|
76 } |