AssetsHelper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Bundle\FrameworkBundle\Templating\Helper;
  11. use Symfony\Component\Asset\Packages;
  12. use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
  13. use Symfony\Component\Templating\Helper\Helper;
  14. /**
  15. * AssetsHelper helps manage asset URLs.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class AssetsHelper extends Helper
  20. {
  21. private $packages;
  22. public function __construct(Packages $packages)
  23. {
  24. $this->packages = $packages;
  25. }
  26. /**
  27. * Returns the public url/path of an asset.
  28. *
  29. * If the package used to generate the path is an instance of
  30. * UrlPackage, you will always get a URL and not a path.
  31. *
  32. * @param string $path A public path
  33. * @param string $packageName The name of the asset package to use
  34. *
  35. * @return string The public path of the asset
  36. */
  37. public function getUrl($path, $packageName = null, $version = null)
  38. {
  39. // BC layer to be removed in 3.0
  40. if (3 === \func_num_args()) {
  41. @trigger_error('Forcing a version for an asset was deprecated in 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
  42. $args = \func_get_args();
  43. return $this->getLegacyAssetUrl($path, $packageName, $args[2]);
  44. }
  45. return $this->packages->getUrl($path, $packageName);
  46. }
  47. /**
  48. * Returns the version of an asset.
  49. *
  50. * @param string $path A public path
  51. * @param string $packageName The name of the asset package to use
  52. *
  53. * @return string The asset version
  54. */
  55. public function getVersion($path = null, $packageName = null)
  56. {
  57. // no arguments means old getVersion() for default package
  58. if (null === $path) {
  59. @trigger_error('The getVersion() method requires a path as a first argument since Symfony 2.7 and will be enforced as of 3.0.', E_USER_DEPRECATED);
  60. return $this->packages->getVersion('/', $packageName);
  61. }
  62. // path and packageName can only be for the new version
  63. if (null !== $packageName) {
  64. return $this->packages->getVersion($path, $packageName);
  65. }
  66. // packageName is null and path not, so path is a path or a packageName
  67. try {
  68. $this->packages->getPackage($path);
  69. } catch (\InvalidArgumentException $e) {
  70. // path is not a package, so it should be a path
  71. return $this->packages->getVersion($path);
  72. }
  73. // path is a packageName, old version
  74. @trigger_error('The getVersion() method requires a path as a first argument since Symfony 2.7 and will be enforced as of 3.0.', E_USER_DEPRECATED);
  75. return $this->packages->getVersion('/', $path);
  76. }
  77. private function getLegacyAssetUrl($path, $packageName = null, $version = null)
  78. {
  79. if ($version) {
  80. $package = $this->packages->getPackage($packageName);
  81. $v = new \ReflectionProperty('Symfony\Component\Asset\Package', 'versionStrategy');
  82. $v->setAccessible(true);
  83. $currentVersionStrategy = $v->getValue($package);
  84. $f = new \ReflectionProperty($currentVersionStrategy, 'format');
  85. $f->setAccessible(true);
  86. $format = $f->getValue($currentVersionStrategy);
  87. $v->setValue($package, new StaticVersionStrategy($version, $format));
  88. }
  89. $url = $this->packages->getUrl($path, $packageName);
  90. if ($version) {
  91. $v->setValue($package, $currentVersionStrategy);
  92. }
  93. return $url;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function getName()
  99. {
  100. return 'assets';
  101. }
  102. }