PathPackage.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\Component\Templating\Asset;
  11. /**
  12. * The path packages adds a version and a base path to asset URLs.
  13. *
  14. * @author Kris Wallsmith <kris@symfony.com>
  15. */
  16. class PathPackage extends Package
  17. {
  18. private $basePath;
  19. /**
  20. * Constructor.
  21. *
  22. * @param string $basePath The base path to be prepended to relative paths
  23. * @param string $version The package version
  24. * @param string $format The format used to apply the version
  25. */
  26. public function __construct($basePath = null, $version = null, $format = null)
  27. {
  28. parent::__construct($version, $format);
  29. if (!$basePath) {
  30. $this->basePath = '/';
  31. } else {
  32. if ('/' != $basePath[0]) {
  33. $basePath = '/'.$basePath;
  34. }
  35. $this->basePath = rtrim($basePath, '/').'/';
  36. }
  37. }
  38. public function getUrl($path)
  39. {
  40. if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
  41. return $path;
  42. }
  43. $url = $this->applyVersion($path);
  44. // apply the base path
  45. if ('/' !== substr($url, 0, 1)) {
  46. $url = $this->basePath.$url;
  47. }
  48. return $url;
  49. }
  50. /**
  51. * Returns the base path.
  52. *
  53. * @return string The base path
  54. */
  55. public function getBasePath()
  56. {
  57. return $this->basePath;
  58. }
  59. }