AssetsHelper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * @param string $path The path
  83. *
  84. * @return string The base URL
  85. */
  86. public function getBaseURL($path)
  87. {
  88. $count = count($this->baseURLs);
  89. if (0 === $count) {
  90. return '';
  91. }
  92. if (1 === $count) {
  93. return $this->baseURLs[0];
  94. }
  95. return $this->baseURLs[fmod(hexdec(substr(md5($path), 0, 10)), $count)];
  96. }
  97. /**
  98. * Gets the base URLs.
  99. *
  100. * @return array The base URLs
  101. */
  102. public function getBaseURLs()
  103. {
  104. return $this->baseURLs;
  105. }
  106. /**
  107. * Sets the base URLs.
  108. *
  109. * If you pass an array, the getBaseURL() will return a
  110. * random one each time it is called.
  111. *
  112. * @param string|array $baseURLs The base URLs
  113. */
  114. public function setBaseURLs($baseURLs)
  115. {
  116. if (!is_array($baseURLs)) {
  117. $baseURLs = array($baseURLs);
  118. }
  119. $this->baseURLs = array();
  120. foreach ($baseURLs as $URL) {
  121. $this->baseURLs[] = rtrim($URL, '/');
  122. }
  123. }
  124. /**
  125. * Returns the public path.
  126. *
  127. * @param string $path A public path
  128. *
  129. * @return string A public path which takes into account the base path and URL path
  130. */
  131. public function getUrl($path)
  132. {
  133. if (false !== strpos($path, '://')) {
  134. return $path;
  135. }
  136. $base = $this->getBaseURL($path);
  137. if (0 !== strpos($path, '/')) {
  138. $path = $base ? '/'.$path : $this->basePath.$path;
  139. }
  140. return $base.$path.($this->version ? '?'.$this->version : '');
  141. }
  142. /**
  143. * Returns the canonical name of this helper.
  144. *
  145. * @return string The canonical name
  146. */
  147. public function getName()
  148. {
  149. return 'assets';
  150. }
  151. }