PageRenderTime 64ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr1/src/vendor/symfony/src/Symfony/Components/Templating/Helper/AssetsHelper.php

https://github.com/ad2joe/php-framework-benchmarks
PHP | 180 lines | 79 code | 21 blank | 80 comment | 8 complexity | 88451956447401114f12d78886700a3e MD5 | raw file
  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 $version;
  27. protected $baseURLs;
  28. protected $basePath;
  29. /**
  30. * Constructor.
  31. *
  32. * @param string $basePath The base path
  33. * @param string|array $baseURLs The domain URL or an array of domain URLs
  34. * @param string $version The version
  35. */
  36. public function __construct($basePath = null, $baseURLs = array(), $version = null)
  37. {
  38. $this->setBasePath($basePath);
  39. $this->setBaseURLs($baseURLs);
  40. $this->version = $version;
  41. }
  42. /**
  43. * Gets the version to add to public URL.
  44. *
  45. * @return string The current version
  46. */
  47. public function getVersion()
  48. {
  49. return $this->version;
  50. }
  51. /**
  52. * Sets the version that is added to each public URL.
  53. *
  54. * @param string $id The version
  55. */
  56. public function setVersion($version)
  57. {
  58. $this->version = $version;
  59. }
  60. /**
  61. * Gets the base path.
  62. *
  63. * @return string The base path
  64. */
  65. public function getBasePath()
  66. {
  67. return $this->basePath;
  68. }
  69. /**
  70. * Sets the base path.
  71. *
  72. * @param string $basePath The base path
  73. */
  74. public function setBasePath($basePath)
  75. {
  76. if (strlen($basePath) && '/' != $basePath[0])
  77. {
  78. $basePath = '/'.$basePath;
  79. }
  80. $this->basePath = rtrim($basePath, '/').'/';
  81. }
  82. /**
  83. * Gets the base URL.
  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. {
  94. return '';
  95. }
  96. if (1 === $count)
  97. {
  98. return $this->baseURLs[0];
  99. }
  100. return $this->baseURLs[fmod(hexdec(substr(md5($path), 0, 10)), $count)];
  101. }
  102. /**
  103. * Gets the base URLs.
  104. *
  105. * @return array The base URLs
  106. */
  107. public function getBaseURLs()
  108. {
  109. return $this->baseURLs;
  110. }
  111. /**
  112. * Sets the base URLs.
  113. *
  114. * If you pass an array, the getBaseURL() will return a
  115. * random one each time it is called.
  116. *
  117. * @param string|array $baseURLs The base URLs
  118. */
  119. public function setBaseURLs($baseURLs)
  120. {
  121. if (!is_array($baseURLs))
  122. {
  123. $baseURLs = array($baseURLs);
  124. }
  125. $this->baseURLs = array();
  126. foreach ($baseURLs as $URL)
  127. {
  128. $this->baseURLs[] = rtrim($URL, '/');
  129. }
  130. }
  131. /**
  132. * Returns the public path.
  133. *
  134. * @param string $path A public path
  135. *
  136. * @return string A public path which takes into account the base path and URL path
  137. */
  138. public function getUrl($path)
  139. {
  140. if (strpos($path, '://'))
  141. {
  142. return $path;
  143. }
  144. if (0 !== strpos($path, '/'))
  145. {
  146. $path = $this->basePath.$path;
  147. }
  148. return $this->getBaseURL($path).$path.($this->version ? '?'.$this->version : '');
  149. }
  150. /**
  151. * Returns the canonical name of this helper.
  152. *
  153. * @return string The canonical name
  154. */
  155. public function getName()
  156. {
  157. return 'assets';
  158. }
  159. }