AssetsExtension.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\Bundle\TwigBundle\Extension;
  11. use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
  12. /**
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class AssetsExtension extends \Twig_Extension
  16. {
  17. /**
  18. * @var Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper
  19. */
  20. private $helper;
  21. public function __construct(AssetsHelper $helper)
  22. {
  23. $this->helper = $helper;
  24. }
  25. /**
  26. * Returns a list of functions to add to the existing list.
  27. *
  28. * @return array An array of functions
  29. */
  30. public function getFunctions()
  31. {
  32. return array(
  33. 'asset' => new \Twig_Function_Method($this, 'getAssetUrl'),
  34. 'assets_version' => new \Twig_Function_Method($this, 'getAssetsVersion'),
  35. );
  36. }
  37. /**
  38. * Returns the public path of an asset
  39. *
  40. * Absolute paths (i.e. http://...) are returned unmodified.
  41. *
  42. * @param string $path A public path
  43. * @param string $packageName The name of the asset package to use
  44. *
  45. * @return string A public path which takes into account the base path and URL path
  46. */
  47. public function getAssetUrl($path, $packageName = null)
  48. {
  49. return $this->helper->getUrl($path, $packageName);
  50. }
  51. /**
  52. * Returns the version of the assets in a package
  53. *
  54. * @param string $packageName
  55. * @return int
  56. */
  57. public function getAssetsVersion($packageName = null)
  58. {
  59. return $this->helper->getVersion($packageName);
  60. }
  61. /**
  62. * Returns the name of the extension.
  63. *
  64. * @return string The extension name
  65. */
  66. public function getName()
  67. {
  68. return 'assets';
  69. }
  70. }