AssetsHelper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Symfony\Component\Templating\Helper;
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * AssetsHelper is the base class for all helper classes that manages assets.
  13. *
  14. * Usage:
  15. *
  16. * <code>
  17. * <img src="<?php echo $view['assets']->getUrl('foo.png') ?>" />
  18. * </code>
  19. *
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class AssetsHelper extends Helper
  23. {
  24. protected $version;
  25. protected $baseURLs;
  26. protected $basePath;
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $basePath The base path
  31. * @param string|array $baseURLs The domain URL or an array of domain URLs
  32. * @param string $version The version
  33. */
  34. public function __construct($basePath = null, $baseURLs = array(), $version = null)
  35. {
  36. $this->setBasePath($basePath);
  37. $this->setBaseURLs($baseURLs);
  38. $this->version = $version;
  39. }
  40. /**
  41. * Gets the version to add to public URL.
  42. *
  43. * @return string The current version
  44. */
  45. public function getVersion()
  46. {
  47. return $this->version;
  48. }
  49. /**
  50. * Sets the version that is added to each public URL.
  51. *
  52. * @param string $id The version
  53. */
  54. public function setVersion($version)
  55. {
  56. $this->version = $version;
  57. }
  58. /**
  59. * Gets the base path.
  60. *
  61. * @return string The base path
  62. */
  63. public function getBasePath()
  64. {
  65. return $this->basePath;
  66. }
  67. /**
  68. * Sets the base path.
  69. *
  70. * @param string $basePath The base path
  71. */
  72. public function setBasePath($basePath)
  73. {
  74. if (strlen($basePath) && '/' != $basePath[0]) {
  75. $basePath = '/'.$basePath;
  76. }
  77. $this->basePath = rtrim($basePath, '/').'/';
  78. }
  79. /**
  80. * Gets the base URL.
  81. *
  82. * If multiple base URLs have been defined a random one will be picked for each asset.
  83. * In other words: for one asset path the same base URL will always be picked among the available base URLs.
  84. *
  85. * @param string $path The path
  86. *
  87. * @return string The base URL
  88. */
  89. public function getBaseURL($path)
  90. {
  91. $count = count($this->baseURLs);
  92. if (0 === $count) {
  93. return '';
  94. }
  95. if (1 === $count) {
  96. return $this->baseURLs[0];
  97. }
  98. return $this->baseURLs[fmod(hexdec(substr(md5($path), 0, 10)), $count)];
  99. }
  100. /**
  101. * Gets the base URLs.
  102. *
  103. * @return array The base URLs
  104. */
  105. public function getBaseURLs()
  106. {
  107. return $this->baseURLs;
  108. }
  109. /**
  110. * Sets the base URLs.
  111. *
  112. * If you pass an array, the getBaseURL() will return a
  113. * randomly pick one to use for each asset.
  114. *
  115. * @param string|array $baseURLs The base URLs
  116. */
  117. public function setBaseURLs($baseURLs)
  118. {
  119. if (!is_array($baseURLs)) {
  120. $baseURLs = array($baseURLs);
  121. }
  122. $this->baseURLs = array();
  123. foreach ($baseURLs as $URL) {
  124. $this->baseURLs[] = rtrim($URL, '/');
  125. }
  126. }
  127. /**
  128. * Returns the public path.
  129. *
  130. * @param string $path A public path
  131. *
  132. * @return string A public path which takes into account the base path and URL path
  133. */
  134. public function getUrl($path)
  135. {
  136. if (false !== strpos($path, '://')) {
  137. return $path;
  138. }
  139. $base = $this->getBaseURL($path);
  140. if (0 !== strpos($path, '/')) {
  141. $path = $base ? '/'.$path : $this->basePath.$path;
  142. }
  143. return $base.$path.($this->version ? '?'.$this->version : '');
  144. }
  145. /**
  146. * Returns the canonical name of this helper.
  147. *
  148. * @return string The canonical name
  149. */
  150. public function getName()
  151. {
  152. return 'assets';
  153. }
  154. }