AsseticHelper.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Bundle\AsseticBundle\Templating;
  11. use Assetic\Asset\AssetInterface;
  12. use Assetic\Factory\AssetFactory;
  13. use Assetic\Util\TraversableString;
  14. use Symfony\Component\Templating\Helper\Helper;
  15. /**
  16. * The "assetic" templating helper.
  17. *
  18. * @author Kris Wallsmith <kris@symfony.com>
  19. */
  20. abstract class AsseticHelper extends Helper
  21. {
  22. protected $factory;
  23. /**
  24. * Constructor.
  25. *
  26. * @param AssetFactory $factory The asset factory
  27. */
  28. public function __construct(AssetFactory $factory)
  29. {
  30. $this->factory = $factory;
  31. }
  32. /**
  33. * Returns an array of javascript urls.
  34. */
  35. public function javascripts($inputs = array(), $filters = array(), array $options = array())
  36. {
  37. if (!isset($options['output'])) {
  38. $options['output'] = 'js/*.js';
  39. }
  40. return $this->getAssetUrls($inputs, $filters, $options);
  41. }
  42. /**
  43. * Returns an array of stylesheet urls.
  44. */
  45. public function stylesheets($inputs = array(), $filters = array(), array $options = array())
  46. {
  47. if (!isset($options['output'])) {
  48. $options['output'] = 'css/*.css';
  49. }
  50. return $this->getAssetUrls($inputs, $filters, $options);
  51. }
  52. /**
  53. * Returns an array of one image url.
  54. */
  55. public function image($inputs = array(), $filters = array(), array $options = array())
  56. {
  57. if (!isset($options['output'])) {
  58. $options['output'] = 'images/*';
  59. }
  60. $options['single'] = true;
  61. return $this->getAssetUrls($inputs, $filters, $options);
  62. }
  63. /**
  64. * Gets the URLs for the configured asset.
  65. *
  66. * Usage looks something like this:
  67. *
  68. * <?php foreach ($view['assetic']->assets('@jquery, js/src/core/*', '?yui_js') as $url): ?>
  69. * <script src="<?php echo $url ?>" type="text/javascript"></script>
  70. * <?php endforeach; ?>
  71. *
  72. * When in debug mode, the helper returns an array of one or more URLs.
  73. * When not in debug mode it returns an array of one URL.
  74. *
  75. * @param array|string $inputs An array or comma-separated list of input strings
  76. * @param array|string $filters An array or comma-separated list of filter names
  77. * @param array $options An array of options
  78. *
  79. * @return array An array of URLs for the asset
  80. */
  81. private function getAssetUrls($inputs = array(), $filters = array(), array $options = array())
  82. {
  83. $explode = function($value)
  84. {
  85. return array_map('trim', explode(',', $value));
  86. };
  87. if (!is_array($inputs)) {
  88. $inputs = $explode($inputs);
  89. }
  90. if (!is_array($filters)) {
  91. $filters = $explode($filters);
  92. }
  93. if (!isset($options['debug'])) {
  94. $options['debug'] = $this->factory->isDebug();
  95. }
  96. if (!isset($options['combine'])) {
  97. $options['combine'] = !$options['debug'];
  98. }
  99. if (isset($options['single']) && $options['single'] && 1 < count($inputs)) {
  100. $inputs = array_slice($inputs, -1);
  101. }
  102. if (!isset($options['name'])) {
  103. $options['name'] = $this->factory->generateAssetName($inputs, $filters, $options);
  104. }
  105. $asset = $this->factory->createAsset($inputs, $filters, $options);
  106. $one = $this->getAssetUrl($asset, $options);
  107. $many = array();
  108. if ($options['combine']) {
  109. $many[] = $one;
  110. } else {
  111. $i = 0;
  112. foreach ($asset as $leaf) {
  113. $many[] = $this->getAssetUrl($leaf, array_replace($options, array(
  114. 'name' => $options['name'].'_'.$i++,
  115. )));
  116. }
  117. }
  118. return new TraversableString($one, $many);
  119. }
  120. /**
  121. * Returns an URL for the supplied asset.
  122. *
  123. * @param AssetInterface $asset An asset
  124. * @param array $options An array of options
  125. *
  126. * @return string An echo-ready URL
  127. */
  128. abstract protected function getAssetUrl(AssetInterface $asset, $options = array());
  129. public function getName()
  130. {
  131. return 'assetic';
  132. }
  133. }