EnsureFilterWorker.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\Factory\Worker;
  11. use Assetic\Asset\AssetInterface;
  12. use Assetic\Filter\FilterInterface;
  13. /**
  14. * Applies a filter to an asset based on a source and/or target path match.
  15. *
  16. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  17. * @todo A better asset-matcher mechanism
  18. */
  19. class EnsureFilterWorker implements WorkerInterface
  20. {
  21. const CHECK_SOURCE = 1;
  22. const CHECK_TARGET = 2;
  23. private $pattern;
  24. private $filter;
  25. private $flags;
  26. /**
  27. * Constructor.
  28. *
  29. * @param string $pattern A regex for checking the asset's target URL
  30. * @param FilterInterface $filter A filter to apply if the regex matches
  31. * @param integer $flags Flags for what to check
  32. */
  33. public function __construct($pattern, FilterInterface $filter, $flags = null)
  34. {
  35. if (null === $flags) {
  36. $flags = self::CHECK_SOURCE | self::CHECK_TARGET;
  37. }
  38. $this->pattern = $pattern;
  39. $this->filter = $filter;
  40. $this->flags = $flags;
  41. }
  42. public function process(AssetInterface $asset)
  43. {
  44. if (
  45. (self::CHECK_SOURCE === (self::CHECK_SOURCE & $this->flags) && preg_match($this->pattern, $asset->getSourcePath()))
  46. ||
  47. (self::CHECK_TARGET === (self::CHECK_TARGET & $this->flags) && preg_match($this->pattern, $asset->getTargetPath()))
  48. ) {
  49. $asset->ensureFilter($this->filter);
  50. }
  51. }
  52. }