AssetsExtension.php 1.9 KB

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