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