Package.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Templating\Asset;
  11. /**
  12. * The basic package will add a version to asset URLs.
  13. *
  14. * @author Kris Wallsmith <kris@symfony.com>
  15. */
  16. class Package implements PackageInterface
  17. {
  18. private $version;
  19. private $format;
  20. /**
  21. * Constructor.
  22. *
  23. * @param string $version The package version
  24. * @param string $format The format used to apply the version
  25. */
  26. public function __construct($version = null, $format = null)
  27. {
  28. $this->version = $version;
  29. $this->format = $format ?: '%s?%s';
  30. }
  31. public function getVersion()
  32. {
  33. return $this->version;
  34. }
  35. public function getUrl($path)
  36. {
  37. if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
  38. return $path;
  39. }
  40. return $this->applyVersion($path);
  41. }
  42. /**
  43. * Applies version to the supplied path.
  44. *
  45. * @param string $path A path
  46. *
  47. * @return string The versionized path
  48. */
  49. protected function applyVersion($path)
  50. {
  51. if (null === $this->version) {
  52. return $path;
  53. }
  54. $versionized = sprintf($this->format, ltrim($path, '/'), $this->version);
  55. if ($path && '/' == $path[0]) {
  56. $versionized = '/'.$versionized;
  57. }
  58. return $versionized;
  59. }
  60. }