AssetsHelper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Symfony\Components\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 $this->assets->getUrl('foo.png') ?>" />
  18. * </code>
  19. *
  20. * @package symfony
  21. * @subpackage templating
  22. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23. */
  24. class AssetsHelper extends Helper
  25. {
  26. protected
  27. $version = null,
  28. $baseURLs = array(),
  29. $basePath = '';
  30. /**
  31. * Constructor.
  32. *
  33. * @param string $basePath The base path
  34. * @param string|array $baseURLs The domain URL or an array of domain URLs
  35. * @param string $version The version
  36. */
  37. public function __construct($basePath = null, $baseURLs = array(), $version = null)
  38. {
  39. $this->setBasePath($basePath);
  40. $this->setBaseURLs($baseURLs);
  41. $this->version = $version;
  42. }
  43. /**
  44. * Gets the version to add to public URL.
  45. *
  46. * @return string The current version
  47. */
  48. public function getVersion()
  49. {
  50. return $this->version;
  51. }
  52. /**
  53. * Sets the version that is added to each public URL.
  54. *
  55. * @param string $id The version
  56. */
  57. public function setVersion($version)
  58. {
  59. $this->version = $version;
  60. }
  61. /**
  62. * Gets the base path.
  63. *
  64. * @return string The base path
  65. */
  66. public function getBasePath()
  67. {
  68. return $this->basePath;
  69. }
  70. /**
  71. * Sets the base path.
  72. *
  73. * @param string $basePath The base path
  74. */
  75. public function setBasePath($basePath)
  76. {
  77. if (strlen($basePath) && '/' != $basePath[0])
  78. {
  79. $basePath = '/'.$basePath;
  80. }
  81. $this->basePath = rtrim($basePath, '/').'/';
  82. }
  83. /**
  84. * Gets the base URL.
  85. *
  86. * @param string $path The path
  87. *
  88. * @return string The base URL
  89. */
  90. public function getBaseURL($path)
  91. {
  92. $count = count($this->baseURLs);
  93. if (0 === $count)
  94. {
  95. return '';
  96. }
  97. if (1 === $count)
  98. {
  99. return $this->baseURLs[0];
  100. }
  101. return $this->baseURLs[fmod(hexdec(substr(md5($path), 0, 10)), $count)];
  102. }
  103. /**
  104. * Gets the base URLs.
  105. *
  106. * @return array The base URLs
  107. */
  108. public function getBaseURLs()
  109. {
  110. return $this->baseURLs;
  111. }
  112. /**
  113. * Sets the base URLs.
  114. *
  115. * If you pass an array, the getBaseURL() will return a
  116. * random one each time it is called.
  117. *
  118. * @param string|array $baseURLs The base URLs
  119. */
  120. public function setBaseURLs($baseURLs)
  121. {
  122. if (!is_array($baseURLs))
  123. {
  124. $baseURLs = array($baseURLs);
  125. }
  126. $this->baseURLs = array();
  127. foreach ($baseURLs as $URL)
  128. {
  129. $this->baseURLs[] = rtrim($URL, '/');
  130. }
  131. }
  132. /**
  133. * Returns the public path.
  134. *
  135. * @param string $path A public path
  136. *
  137. * @return string A public path which takes into account the base path and URL path
  138. */
  139. public function getUrl($path)
  140. {
  141. if (strpos($path, '://'))
  142. {
  143. return $path;
  144. }
  145. if (0 !== strpos($path, '/'))
  146. {
  147. $path = $this->basePath.$path;
  148. }
  149. return $this->getBaseURL($path).$path.($this->version ? '?'.$this->version : '');
  150. }
  151. /**
  152. * Returns the canonical name of this helper.
  153. *
  154. * @return string The canonical name
  155. */
  156. public function getName()
  157. {
  158. return 'assets';
  159. }
  160. }