FileAsset.php 1.8KB

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