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