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